Skip to content

Commit

Permalink
Merge pull request #3016 from briannesbitt/feature/issue-3015-unseria…
Browse files Browse the repository at this point in the history
…lize-v2

Unserialize CarbonInterval from v2
  • Loading branch information
kylekatarnls committed May 2, 2024
2 parents 8ff64b9 + 7138e71 commit 7c12593
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
40 changes: 40 additions & 0 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,12 @@ public function set($name, $value = null): static
break;

case 'day':
if ($value === false) {
$this->days = false;

break;
}

$this->checkIntegerValue($key, $value);
$this->d = $value;
$this->handleDecimalPart('day', $value, $this->d);
Expand Down Expand Up @@ -3037,6 +3043,40 @@ public function ceil(DateInterval|string|float|int $precision = 1): static
return $this->round($precision, 'ceil');
}

public function __unserialize(array $data): void
{
$properties = array_combine(
array_map(
static fn (mixed $key) => \is_string($key)
? str_replace('tzName', 'timezoneSetting', $key)
: $key,
array_keys($data),
),
$data,
);

if (method_exists(parent::class, '__unserialize')) {
// PHP >= 8.2
parent::__unserialize($properties);

return;
}

// PHP <= 8.1
// @codeCoverageIgnoreStart
foreach ($properties as $property => $value) {
$name = preg_replace('/^\0.+\0/', '', $property);
$localStrictMode = $this->localStrictModeEnabled;
$this->localStrictModeEnabled = false;
$this->$name = $value;

if ($name !== 'localStrictModeEnabled') {
$this->localStrictModeEnabled = $localStrictMode;
}
}
// @codeCoverageIgnoreEnd
}

/**
* @template T
*
Expand Down
18 changes: 0 additions & 18 deletions src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2636,24 +2636,6 @@ protected function resolveCarbon(DateTimeInterface|string|null $date): self
return $date instanceof self ? $date : $this->transmitFactory(static fn () => static::instance($date));
}

/**
* Return the Carbon instance passed through, a now instance in UTC
* if null given or parse the input if string given (using current timezone
* then switching to UTC).
*/
protected function resolveUTC(DateTimeInterface|string|null $date = null): self
{
if (!$date) {
return $this->transmitFactory(static fn () => static::now('UTC'));
}

if (\is_string($date)) {
return $this->transmitFactory(fn () => static::parse($date, $this->getTimezone())->utc());
}

return $date instanceof self ? $date : $this->transmitFactory(static fn () => static::instance($date))->utc();
}

protected static function weekRotate(int $day, int $rotation): int
{
return (static::DAYS_PER_WEEK + $rotation % static::DAYS_PER_WEEK + $day) % static::DAYS_PER_WEEK;
Expand Down
10 changes: 10 additions & 0 deletions tests/CarbonInterval/ConstructTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,14 @@ public function testFromSerialization()

$this->assertEquals($interval, $copy);
}

public function testFromV2SerializedInterval()
{
$serializedData = trim(file_get_contents(__DIR__.'/../Fixtures/serialized-interval-from-v2.txt'));
$interval = unserialize($serializedData);

$this->assertInstanceOf(CarbonInterval::class, $interval);
$this->assertSame(2, $interval->m);
$this->assertSame(5.4e-5, $interval->f);
}
}
Binary file added tests/Fixtures/serialized-interval-from-v2.txt
Binary file not shown.

0 comments on commit 7c12593

Please sign in to comment.