-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
163 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/dt0. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/dt0 | ||
* This source file is licensed under the MIT license which you will | ||
* find in the LICENSE file or at https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace fab2s\Dt0\Caster; | ||
|
||
use Carbon\Carbon; | ||
use Carbon\CarbonImmutable; | ||
use DateTimeInterface; | ||
use DateTimeZone; | ||
use Exception; | ||
|
||
class CarbonCaster implements CasterInterface | ||
{ | ||
use DateTimeTrait; | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function __construct( | ||
DateTimeZone|string|null $timeZone = null, | ||
public readonly bool $immutable = true, | ||
) { | ||
$this->timeZone = $timeZone instanceof DateTimeZone ? $timeZone : ($timeZone ? new DateTimeZone($timeZone) : null); | ||
$this->dateTimeClass = $immutable ? CarbonImmutable::class : Carbon::class; | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function cast(mixed $value): ?DateTimeInterface | ||
{ | ||
return $this->resolve($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fab2s/dt0. | ||
* (c) Fabrice de Stefanis / https://github.com/fab2s/dt0 | ||
* This source file is licensed under the MIT license which you will | ||
* find in the LICENSE file or at https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
namespace fab2s\Dt0\Caster; | ||
|
||
use DateTimeInterface; | ||
use DateTimeZone; | ||
use Exception; | ||
|
||
trait DateTimeTrait | ||
{ | ||
public readonly ?DateTimeZone $timeZone; | ||
|
||
/** | ||
* @var class-string<DateTimeInterface> | ||
*/ | ||
protected readonly string $dateTimeClass; | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function resolve(mixed $value): ?DateTimeInterface | ||
{ | ||
$instance = match (true) { | ||
$value instanceof DateTimeInterface => $this->dateTimeClass::createFromInterface($value), | ||
is_string($value) => new $this->dateTimeClass($value), | ||
is_array($value) => $this->fromArray($value), | ||
is_int($value) => (new $this->dateTimeClass)->setTimestamp($value), | ||
default => null, | ||
}; | ||
|
||
if ($instance && $this->timeZone) { | ||
$instance = $instance->setTimezone($this->timeZone); | ||
} | ||
|
||
return $instance; | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function fromArray(array $date): ?DateTimeInterface | ||
{ | ||
$result = null; | ||
if (! empty($date['date'])) { | ||
$result = new $this->dateTimeClass($date['date']); | ||
if (! empty($date['timezone'])) { | ||
$result->setTimeZone($date['timezone'] instanceof DateTimeZone ? $date['timezone'] : new DateTimeZone($date['timezone'])); | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters