Skip to content

Commit 3f9347d

Browse files
committed
Add date type validator, closes #5
1 parent af32c06 commit 3f9347d

File tree

3 files changed

+201
-2
lines changed

3 files changed

+201
-2
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\Validator\ValidatorTestCase;
22+
23+
/**
24+
* Class DateValidatorTest
25+
* @package CloudCreativity\JsonApi
26+
*/
27+
class DateValidatorTest extends ValidatorTestCase
28+
{
29+
30+
public function testValid()
31+
{
32+
$validator = new DateValidator();
33+
$date = new \DateTime();
34+
35+
$this->assertTrue($validator->isValid($date->format(DateValidator::FORMAT_ISO_8601)));
36+
$this->assertTrue($validator->isValid($date->format(DateValidator::FORMAT_ISO_8601_MILLISECONDS)));
37+
38+
return $validator;
39+
}
40+
41+
/**
42+
* @depends testValid
43+
*/
44+
public function testInvalid(DateValidator $validator)
45+
{
46+
$date = new \DateTime();
47+
48+
$this->assertFalse($validator->isValid($date->format('Y-m-d')));
49+
$error = $this->getError($validator);
50+
$this->assertEquals(DateValidator::ERROR_INVALID_VALUE, $error->getCode());
51+
$this->assertEquals(400, $error->getStatus());
52+
}
53+
54+
public function testCustomFormat()
55+
{
56+
$format = 'H:i:s';
57+
$validator = new DateValidator([$format]);
58+
$date = new \DateTime();
59+
60+
$this->assertTrue($validator->isValid($date->format($format)));
61+
$this->assertFalse($validator->isValid($date->format(DateValidator::FORMAT_ISO_8601)));
62+
}
63+
}

test/Validator/ValidatorTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ValidatorTestCase extends \PHPUnit_Framework_TestCase
1010

1111
/**
1212
* @param ValidatorInterface $validator
13-
* @return mixed
13+
* @return ErrorObject
1414
*/
1515
protected function getError(ValidatorInterface $validator)
1616
{
@@ -28,4 +28,4 @@ protected function getError(ValidatorInterface $validator)
2828

2929
return $error;
3030
}
31-
}
31+
}

0 commit comments

Comments
 (0)