Skip to content

Commit

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

Unserialize carbon interval created by v2
  • Loading branch information
kylekatarnls authored Jun 20, 2024
2 parents e2b9aeb + 44a47cd commit 39c8ef7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,10 @@ public function set($name, $value = null): static
break;

default:
if (str_starts_with($key, ' * ')) {
return $this->setSetting(substr($key, 3), $value);
}

if ($this->localStrictModeEnabled ?? Carbon::isStrictModeEnabled()) {
throw new UnknownSetterException($key);
}
Expand Down Expand Up @@ -3340,4 +3344,40 @@ private function checkStartAndEnd(): void
$this->rawInterval = null;
}
}

/** @return $this */
private function setSetting(string $setting, mixed $value): self
{
switch ($setting) {
case 'timezoneSetting':
return $value === null ? $this : $this->setTimezone($value);

case 'step':
$this->setStep($value);

return $this;

case 'localMonthsOverflow':
return $value === null ? $this : $this->settings(['monthOverflow' => $value]);

case 'localYearsOverflow':
return $value === null ? $this : $this->settings(['yearOverflow' => $value]);

case 'localStrictModeEnabled':
case 'localHumanDiffOptions':
case 'localToStringFormat':
case 'localSerializer':
case 'localMacros':
case 'localGenericMacros':
case 'localFormatFunction':
case 'localTranslator':
$this->$setting = $value;

return $this;

default:
// Drop unknown settings
return $this;
}
}
}
9 changes: 9 additions & 0 deletions tests/Language/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,13 @@ public function testSerializationKeepLocale()

$this->assertEquals('zh', unserialize(serialize($translator))->getLocale());
}

public function testUnserializeV2Object()
{
$interval = unserialize(<<<'EOS'
O:21:"Carbon\CarbonInterval":22:{s:1:"y";i:0;s:1:"m";i:2;s:1:"d";i:0;s:1:"h";i:0;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:5.4E-5;s:6:"invert";i:0;s:4:"days";b:0;s:11:"from_string";b:0;s:9:" * tzName";N;s:7:" * step";N;s:22:" * localMonthsOverflow";N;s:21:" * localYearsOverflow";N;s:25:" * localStrictModeEnabled";N;s:24:" * localHumanDiffOptions";N;s:22:" * localToStringFormat";N;s:18:" * localSerializer";N;s:14:" * localMacros";N;s:21:" * localGenericMacros";N;s:22:" * localFormatFunction";N;s:18:" * localTranslator";N;}
EOS);

$this->assertCarbonInterval($interval, 0, 2, 0, 0, 0, 0, 54);
}
}

0 comments on commit 39c8ef7

Please sign in to comment.