Skip to content

Commit

Permalink
Merge pull request #2884 from briannesbitt/feature/carbon-period-date…
Browse files Browse the repository at this point in the history
…-class

Use relevant class to synchronize CarbonPeriod mock
  • Loading branch information
kylekatarnls authored Nov 18, 2023
2 parents 93f36e7 + f37d3c2 commit 9051283
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ class CarbonPeriod implements Iterator, Countable, JsonSerializable
*/
public const END_MAX_ATTEMPTS = 10000;

/**
* Default date class of iteration items.
*
* @var string
*/
protected const DEFAULT_DATE_CLASS = Carbon::class;

/**
* The registered macros.
*
Expand Down Expand Up @@ -497,15 +504,16 @@ protected static function parseIso8601($iso)
$interval = null;
$start = null;
$end = null;
$dateClass = static::DEFAULT_DATE_CLASS;

foreach (explode('/', $iso) as $key => $part) {
if ($key === 0 && preg_match('/^R(\d*|INF)$/', $part, $match)) {
$parsed = \strlen($match[1]) ? (($match[1] !== 'INF') ? (int) $match[1] : INF) : null;
} elseif ($interval === null && $parsed = CarbonInterval::make($part)) {
$interval = $part;
} elseif ($start === null && $parsed = Carbon::make($part)) {
} elseif ($start === null && $parsed = $dateClass::make($part)) {
$start = $part;
} elseif ($end === null && $parsed = Carbon::make(static::addMissingParts($start ?? '', $part))) {
} elseif ($end === null && $parsed = $dateClass::make(static::addMissingParts($start ?? '', $part))) {
$end = $part;
} else {
throw new InvalidPeriodParameterException("Invalid ISO 8601 specification: $iso.");
Expand Down Expand Up @@ -701,7 +709,8 @@ public function __construct(...$arguments)
}

if ($this->startDate === null) {
$this->setStartDate(Carbon::now());
$dateClass = $this->dateClass;
$this->setStartDate($dateClass::now());
}

if ($this->dateInterval === null) {
Expand Down Expand Up @@ -1826,7 +1835,9 @@ public function __call($method, $parameters)
)(...$parameters));
}

if ($this->localStrictModeEnabled ?? Carbon::isStrictModeEnabled()) {
$dateClass = $this->dateClass;

if ($this->localStrictModeEnabled ?? $dateClass::isStrictModeEnabled()) {
throw new UnknownMethodException($method);
}

Expand Down Expand Up @@ -2685,7 +2696,9 @@ private function makeDateTime($value): ?DateTimeInterface
!preg_match('/^R\d/', $value) &&
preg_match('/[a-z\d]/i', $value)
) {
return Carbon::parse($value, $this->tzName);
$dateClass = $this->dateClass;

return $dateClass::parse($value, $this->tzName);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Carbon/CarbonPeriodImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

class CarbonPeriodImmutable extends CarbonPeriod
{
/**
* Default date class of iteration items.
*
* @var string
*/
protected const DEFAULT_DATE_CLASS = CarbonImmutable::class;

/**
* Date class of iteration items.
*
Expand Down

0 comments on commit 9051283

Please sign in to comment.