PluginProbe ʕ •ᴥ•ʔ
FAPI Member / trunk
FAPI Member vtrunk
2.2.33 2.2.32 trunk 1.9.47 2.1.18 2.2.24 2.2.25 2.2.26 2.2.28 2.2.29 2.2.30 2.2.31
fapi-member / src / Utils / DateTimesImmutable.php
fapi-member / src / Utils Last commit date
AlertProvider.php 1 year ago ApiClient.php 1 day ago DateTimeHelper.php 1 year ago DateTimes.php 2 years ago DateTimesImmutable.php 2 years ago Debugger.php 1 year ago DisplayHelper.php 1 year ago EmailHelper.php 1 year ago PostTypeHelper.php 2 years ago Random.php 4 years ago SecurityValidator.php 2 years ago ShortcodeSubstitutor.php 2 months ago TemplateProvider.php 2 years ago functions.php 3 years ago
DateTimesImmutable.php
102 lines
1 <?php
2
3 declare(strict_types = 1);
4
5 namespace FapiMember\Utils;
6
7
8 use FapiMember\Library\SmartEmailing\Types\DatesImmutable;
9 use FapiMember\Library\SmartEmailing\Types\ExtractableTypeInterface;
10
11 abstract class DateTimesImmutable implements ExtractableTypeInterface
12 {
13
14 final public static function from(
15 mixed $value
16 ): \DateTimeImmutable {
17 $dateTime = DateTimes::from($value);
18
19 return self::immutate($dateTime);
20 }
21
22 /**
23 * @param array<mixed>|\ArrayAccess<mixed, mixed> $data
24 */
25 final public static function extract(
26 $data,
27 string $key
28 ): \DateTimeImmutable {
29 $dateTime = DateTimes::extract(
30 $data,
31 $key
32 );
33
34 return self::immutate($dateTime);
35 }
36
37 /**
38 * @param array<mixed>|\ArrayAccess<mixed, mixed> $data
39 */
40 final public static function extractOrNull(
41 $data,
42 string $key,
43 bool $nullIfInvalid = false
44 ): ?\DateTimeImmutable {
45 $dateTime = DateTimes::extractOrNull(
46 $data,
47 $key,
48 $nullIfInvalid
49 );
50
51 if ($dateTime === null) {
52 return null;
53 }
54
55 return self::immutate($dateTime);
56 }
57
58 /**
59 * @param array<mixed> $data
60 * @deprecated Use DatesImmutable::extract
61 */
62 final public static function extractDate(
63 array &$data,
64 string $key
65 ): \DateTimeImmutable {
66 return DatesImmutable::extract($data, $key);
67 }
68
69 /**
70 * @param array<mixed> $data
71 * @deprecated Use DatesImmutable::extractDateOrNull
72 */
73 final public static function extractDateOrNull(
74 array &$data,
75 string $key
76 ): ?\DateTimeImmutable {
77 return DatesImmutable::extractOrNull($data, $key);
78 }
79
80 public static function fromOrNull(
81 mixed $value,
82 bool $nullIfInvalid = false
83 ): ?\DateTimeImmutable {
84 $dateTime = DateTimes::fromOrNull($value, $nullIfInvalid);
85
86 if ($dateTime === null) {
87 return null;
88 }
89
90 return self::immutate($dateTime);
91 }
92
93 private static function immutate(
94 \DateTime $dateTime
95 ): \DateTimeImmutable {
96 return \DateTimeImmutable::createFromMutable(
97 $dateTime
98 );
99 }
100
101 }
102