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
DateTimes.php
129 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types = 1); |
| 4 | |
| 5 | namespace FapiMember\Utils; |
| 6 | |
| 7 | use FapiMember\Library\SmartEmailing\Types\DateTimeFormatter; |
| 8 | use FapiMember\Library\SmartEmailing\Types\ExtractableTypeInterface; |
| 9 | use FapiMember\Library\SmartEmailing\Types\Helpers\ExtractableHelpers; |
| 10 | use FapiMember\Library\SmartEmailing\Types\InvalidTypeException; |
| 11 | use FapiMember\Model\Enums\Format; |
| 12 | |
| 13 | abstract class DateTimes implements ExtractableTypeInterface |
| 14 | { |
| 15 | |
| 16 | final public static function from( |
| 17 | mixed $value |
| 18 | ): \DateTime { |
| 19 | if ($value instanceof \DateTime) { |
| 20 | return $value; |
| 21 | } |
| 22 | |
| 23 | if ($value instanceof \DateTimeImmutable) { |
| 24 | $value = DateTimeFormatter::format($value, Format::DATE_TIME); |
| 25 | } |
| 26 | |
| 27 | if |
| 28 | (\is_string($value) && |
| 29 | (\preg_match('#^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(\.\d+)?\z#', $value, $matches) || |
| 30 | \preg_match('#^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\z#', $value, $matches)) |
| 31 | ) { |
| 32 | if (\count($matches) > 1) { |
| 33 | $value = \substr($value, 0, \strlen($value) - \strlen($matches[1])); |
| 34 | } |
| 35 | |
| 36 | $date = \DateTime::createFromFormat(Format::DATE_TIME, $value); |
| 37 | |
| 38 | if ($date instanceof \DateTime && DateTimeFormatter::format($date, Format::DATE_TIME) === $value) { |
| 39 | return $date; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | throw new InvalidTypeException( |
| 44 | 'Value ' . $value . ' must be string in ' . Format::DATE_TIME . ' format' |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param array<mixed>|\ArrayAccess<mixed, mixed> $data |
| 50 | * @throws \SmartEmailing\Types\InvalidTypeException |
| 51 | */ |
| 52 | final public static function extract( |
| 53 | $data, |
| 54 | string $key |
| 55 | ): \DateTime { |
| 56 | $value = ExtractableHelpers::extractValue($data, $key); |
| 57 | |
| 58 | try { |
| 59 | return self::from($value); |
| 60 | } catch (InvalidTypeException $exception) { |
| 61 | throw $exception->wrap($key); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param array<mixed>|\ArrayAccess<mixed, mixed> $data |
| 67 | */ |
| 68 | final public static function extractOrNull( |
| 69 | $data, |
| 70 | string $key, |
| 71 | bool $nullIfInvalid = false |
| 72 | ): ?\DateTime { |
| 73 | $value = ExtractableHelpers::extractValueOrNull($data, $key); |
| 74 | |
| 75 | if ($value === null) { |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | try { |
| 80 | return self::fromOrNull($value, $nullIfInvalid); |
| 81 | } catch (InvalidTypeException $exception) { |
| 82 | throw $exception->wrap($key); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param array<mixed> $data |
| 88 | * @throws \SmartEmailing\Types\InvalidTypeException |
| 89 | * @deprecated Use Dates::extract |
| 90 | */ |
| 91 | final public static function extractDate( |
| 92 | array &$data, |
| 93 | string $key |
| 94 | ): \DateTime { |
| 95 | return Dates::extract($data, $key); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @param array<mixed> $data |
| 100 | * @deprecated Use Dates::extractDateOrNull |
| 101 | */ |
| 102 | final public static function extractDateOrNull( |
| 103 | array &$data, |
| 104 | string $key |
| 105 | ): ?\DateTime { |
| 106 | return Dates::extractOrNull($data, $key); |
| 107 | } |
| 108 | |
| 109 | public static function fromOrNull( |
| 110 | mixed $value, |
| 111 | bool $nullIfInvalid = false |
| 112 | ): ?\DateTime { |
| 113 | if ($value === null) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | try { |
| 118 | return self::from($value); |
| 119 | } catch (InvalidTypeException $e) { |
| 120 | if ($nullIfInvalid) { |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | throw $e; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | } |
| 129 |