DateTimeFormats.php
1 year ago
IriFormats.php
1 year ago
MiscFormats.php
1 year ago
UriFormats.php
1 year ago
DateTimeFormats.php
70 lines
| 1 | <?php |
| 2 | /* ============================================================================ |
| 3 | * Copyright 2020 Zindex Software |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * ============================================================================ */ |
| 17 | |
| 18 | namespace Opis\JsonSchema\Formats; |
| 19 | |
| 20 | class DateTimeFormats |
| 21 | { |
| 22 | const DATE_REGEX = '/^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/i'; |
| 23 | const TIME_REGEX = '/^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)([01][0-9]|2[0-3]):([0-5][0-9]))?$/i'; |
| 24 | const DATE_TIME_REGEX = '/^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)([01][0-9]|2[0-3]):([0-5][0-9]))?$/i'; |
| 25 | const DURATION_REGEX = '/^P?((((\d+D)|((\d+M(\d+D)?)|(\d+Y(\d+M(\d+D)?)?)))(T((\d+S)|(\d+M(\d+S)?)|(\d+H(\d+M(\d+S)?)?)))?)|(T((\d+S)|(\d+M(\d+S)?)|(\d+H(\d+M(\d+S)?)?)))|(\d+W))$/i'; |
| 26 | |
| 27 | /** |
| 28 | * @param string $value |
| 29 | * @return bool |
| 30 | */ |
| 31 | public static function date(string $value): bool |
| 32 | { |
| 33 | if (preg_match(self::DATE_REGEX, $value, $m)) { |
| 34 | return checkdate($m[2], $m[3], $m[1]); |
| 35 | } |
| 36 | |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param string $value |
| 42 | * @return bool |
| 43 | */ |
| 44 | public static function time(string $value): bool |
| 45 | { |
| 46 | return (bool)preg_match(self::TIME_REGEX, $value); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param string $value |
| 51 | * @return bool |
| 52 | */ |
| 53 | public static function dateTime(string $value): bool |
| 54 | { |
| 55 | if (preg_match(self::DATE_TIME_REGEX, $value, $m)) { |
| 56 | return checkdate($m[2], $m[3], $m[1]); |
| 57 | } |
| 58 | |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param string $value |
| 64 | * @return bool |
| 65 | */ |
| 66 | public static function duration(string $value): bool |
| 67 | { |
| 68 | return (bool) preg_match(self::DURATION_REGEX, $value); |
| 69 | } |
| 70 | } |