1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Arp\DateTime; |
||
6 | |||
7 | use Arp\DateTime\Exception\DateTimeFactoryException; |
||
8 | |||
9 | class DateTimeImmutableFactory implements DateTimeFactoryInterface |
||
10 | { |
||
11 | private DateTimeFactory $dateTimeFactory; |
||
12 | |||
13 | /** |
||
14 | * @throws DateTimeFactoryException |
||
15 | */ |
||
16 | public function __construct( |
||
17 | ?string $dateTimeClassName = null, |
||
18 | ?DateTimeZoneFactoryInterface $dateTimeZoneFactory = null |
||
19 | ) { |
||
20 | $dateTimeClassName ??= \DateTimeImmutable::class; |
||
21 | if (!is_a($dateTimeClassName, \DateTimeImmutable::class, true)) { |
||
22 | throw new DateTimeFactoryException( |
||
23 | sprintf( |
||
24 | 'The \'dateTimeClassName\' parameter must be a class name that implements \'%s\'', |
||
25 | \DateTimeImmutable::class |
||
26 | ) |
||
27 | ); |
||
28 | } |
||
29 | |||
30 | $dateTimeZoneFactory ??= new DateTimeZoneFactory(); |
||
31 | $this->dateTimeFactory = new DateTimeFactory($dateTimeZoneFactory, $dateTimeClassName); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @throws DateTimeFactoryException |
||
36 | */ |
||
37 | public function createDateTime( |
||
38 | ?string $spec = null, |
||
39 | string|\DateTimeZone|null $timeZone = null |
||
40 | ): \DateTimeImmutable { |
||
41 | /** |
||
42 | * @phpstan-ignore-next-line |
||
43 | * @noinspection PhpIncompatibleReturnTypeInspection |
||
44 | */ |
||
45 | return $this->dateTimeFactory->createDateTime($spec, $timeZone); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @throws DateTimeFactoryException |
||
50 | */ |
||
51 | public function createFromFormat( |
||
52 | string $format, |
||
53 | string $spec, |
||
54 | string|\DateTimeZone|null $timeZone = null |
||
55 | ): \DateTimeImmutable { |
||
56 | /** |
||
57 | * @phpstan-ignore-next-line |
||
58 | * @noinspection PhpIncompatibleReturnTypeInspection |
||
59 | */ |
||
60 | return $this->dateTimeFactory->createFromFormat($format, $spec, $timeZone); |
||
0 ignored issues
–
show
|
|||
61 | } |
||
62 | } |
||
63 |