-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarbonCaster.php
39 lines (33 loc) · 992 Bytes
/
CarbonCaster.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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 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): Carbon|CarbonImmutable|null
{
return $this->resolve($value);
}
}