|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2015 Cloud Creativity Limited |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace CloudCreativity\JsonApi\Validator\Type; |
| 20 | + |
| 21 | +use CloudCreativity\JsonApi\Error\ErrorObject; |
| 22 | + |
| 23 | +/** |
| 24 | + * Class DateValidator |
| 25 | + * @package CloudCreativity\JsonApi |
| 26 | + */ |
| 27 | +class DateValidator extends TypeValidator |
| 28 | +{ |
| 29 | + |
| 30 | + const FORMATS = 'formats'; |
| 31 | + |
| 32 | + const FORMAT_ISO_8601 = \DateTime::ISO8601; |
| 33 | + const FORMAT_ISO_8601_MILLISECONDS = 'Y-m-d\TH:i:s.uO'; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + protected $_formats = []; |
| 39 | + |
| 40 | + /** |
| 41 | + * @param array $formats |
| 42 | + * @param bool|false $nullable |
| 43 | + */ |
| 44 | + public function __construct(array $formats = [], $nullable = false) |
| 45 | + { |
| 46 | + parent::__construct($nullable); |
| 47 | + |
| 48 | + if (empty($formats)) { |
| 49 | + $formats = [ |
| 50 | + static::FORMAT_ISO_8601, |
| 51 | + static::FORMAT_ISO_8601_MILLISECONDS, |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + $this->setFormats($formats); |
| 56 | + |
| 57 | + $this->updateTemplate(static::ERROR_INVALID_VALUE, [ |
| 58 | + ErrorObject::DETAIL => 'Expecting a valid date value.', |
| 59 | + ]); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param array $formats |
| 64 | + * @return $this |
| 65 | + */ |
| 66 | + public function setFormats(array $formats) |
| 67 | + { |
| 68 | + foreach ($formats as $format) { |
| 69 | + $this->addFormat($format); |
| 70 | + } |
| 71 | + |
| 72 | + return $this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param $format |
| 77 | + * @return $this |
| 78 | + */ |
| 79 | + public function addFormat($format) |
| 80 | + { |
| 81 | + if (!is_string($format) || empty($format)) { |
| 82 | + throw new \InvalidArgumentException('Expecting a non-empty string format.'); |
| 83 | + } |
| 84 | + |
| 85 | + $this->_formats[] = $format; |
| 86 | + |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @return array |
| 92 | + */ |
| 93 | + public function getFormats() |
| 94 | + { |
| 95 | + return $this->_formats; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param array $config |
| 100 | + * @return $this |
| 101 | + */ |
| 102 | + public function configure(array $config) |
| 103 | + { |
| 104 | + parent::configure($config); |
| 105 | + |
| 106 | + if (isset($config[static::FORMATS]) && is_array($config[static::FORMATS])) { |
| 107 | + $this->setFormats($config[static::FORMATS]); |
| 108 | + } elseif (isset($config[static::FORMATS]) && is_string($config[static::FORMATS])) { |
| 109 | + $this->addFormat($config[static::FORMATS]); |
| 110 | + } |
| 111 | + |
| 112 | + return $this; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param $value |
| 117 | + * @return bool |
| 118 | + */ |
| 119 | + protected function isType($value) |
| 120 | + { |
| 121 | + if (!is_string($value) || empty($value)) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + foreach ($this->getFormats() as $format) { |
| 126 | + |
| 127 | + $date = \DateTime::createFromFormat($format, $value); |
| 128 | + |
| 129 | + if ($date instanceof \DateTime) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return false; |
| 135 | + } |
| 136 | +} |
0 commit comments