| Total Complexity | 5 |
| Total Lines | 59 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 17 | class DateTimeValue extends AbstractAnySimpleType |
||
| 18 | { |
||
| 19 | public const string SCHEMA_TYPE = 'dateTime'; |
||
|
|
|||
| 20 | |||
| 21 | public const string DATETIME_FORMAT = 'Y-m-d\\TH:i:sP'; |
||
| 22 | |||
| 23 | |||
| 24 | /** |
||
| 25 | * Sanitize the value. |
||
| 26 | * |
||
| 27 | * @param string $value The unsanitized value |
||
| 28 | */ |
||
| 29 | protected function sanitizeValue(string $value): string |
||
| 30 | { |
||
| 31 | return static::collapseWhitespace(static::normalizeWhitespace($value)); |
||
| 32 | } |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Validate the value. |
||
| 37 | * |
||
| 38 | * @param string $value |
||
| 39 | * @throws \SimpleSAML\XMLSchema\Exception\SchemaViolationException on failure |
||
| 40 | */ |
||
| 41 | protected function validateValue(string $value): void |
||
| 42 | { |
||
| 43 | Assert::validDateTime($this->sanitizeValue($value), SchemaViolationException::class); |
||
| 44 | } |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | */ |
||
| 49 | public static function now(ClockInterface $clock): static |
||
| 50 | { |
||
| 51 | return static::fromDateTime($clock->now()); |
||
| 52 | } |
||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * @param \DateTimeInterface $value |
||
| 57 | */ |
||
| 58 | public static function fromDateTime(DateTimeInterface $value): static |
||
| 59 | { |
||
| 60 | return new static($value->format(static::DATETIME_FORMAT)); |
||
| 61 | } |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | */ |
||
| 66 | public function toDateTime(): DateTimeImmutable |
||
| 67 | { |
||
| 68 | return new DateTimeImmutable($this->getValue()); |
||
| 69 | } |
||
| 70 | } |
||
| 71 |