Constraints
7 months ago
Entity
7 months ago
Exception
7 months ago
Iterator
7 months ago
Uri
7 months ago
Rfc3339.php
7 months ago
SchemaStorage.php
7 months ago
SchemaStorageInterface.php
7 months ago
UriResolverInterface.php
7 months ago
UriRetrieverInterface.php
7 months ago
Validator.php
7 months ago
Rfc3339.php
31 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JsonSchema; |
| 4 | |
| 5 | class Rfc3339 |
| 6 | { |
| 7 | const REGEX = '/^(\d{4}-\d{2}-\d{2}[T ]{1}\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))$/'; |
| 8 | |
| 9 | /** |
| 10 | * Try creating a DateTime instance |
| 11 | * |
| 12 | * @param string $string |
| 13 | * |
| 14 | * @return \DateTime|null |
| 15 | */ |
| 16 | public static function createFromString($string) |
| 17 | { |
| 18 | if (!preg_match(self::REGEX, strtoupper($string), $matches)) { |
| 19 | return null; |
| 20 | } |
| 21 | |
| 22 | $dateAndTime = $matches[1]; |
| 23 | $microseconds = $matches[2] ?: '.000000'; |
| 24 | $timeZone = 'Z' !== $matches[3] ? $matches[4] . ':' . $matches[5] : '+00:00'; |
| 25 | $dateFormat = strpos($dateAndTime, 'T') === false ? 'Y-m-d H:i:s.uP' : 'Y-m-d\TH:i:s.uP'; |
| 26 | $dateTime = \DateTime::createFromFormat($dateFormat, $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC')); |
| 27 | |
| 28 | return $dateTime ?: null; |
| 29 | } |
| 30 | } |
| 31 |