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