Skip to content

Commit 3172a32

Browse files
author
Christian Blank
authored
3 datetime type (#12)
* Add datetime type * Update type list in readme
1 parent f1a4ee6 commit 3172a32

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Currently the following types are supported:
6969
* String
7070
* Object
7171
* List
72+
* Datetime
7273

7374
There are some open issues with ideas for more types. Feel free to send pull requests.
7475

spec/Type/DatetimeTypeSpec.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace spec\StructureCheck\Type;
4+
5+
use StructureCheck\Type\DatetimeType;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
9+
class DatetimeTypeSpec extends ObjectBehavior
10+
{
11+
function it_is_initializable()
12+
{
13+
$this->beConstructedWith('d-m-Y h:m:s', 'UTC');
14+
$this->shouldHaveType(DatetimeType::class);
15+
}
16+
17+
function it_should_return_valid_for_correct_values()
18+
{
19+
$this->beConstructedWith('d-m-Y h:m:s', 'Europe/Berlin');
20+
21+
$this->check('12-12-2012 12:12:10')->isValid()->shouldBe(true);
22+
}
23+
24+
function it_should_return_invalid_for_others()
25+
{
26+
$this->beConstructedWith('d-m-Y h:m:s', 'Europe/Berlin');
27+
28+
$this->check(null)->isValid()->shouldBe(false);
29+
$this->check('foo')->isValid()->shouldBe(false);
30+
$this->check([])->isValid()->shouldBe(false);
31+
$this->check(1.234)->isValid()->shouldBe(false);
32+
$this->check(true)->isValid()->shouldBe(false);
33+
$this->check(false)->isValid()->shouldBe(false);
34+
}
35+
}

src/Type/DatetimeType.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace StructureCheck\Type;
4+
5+
use DateTime;
6+
use DateTimeZone;
7+
use StructureCheck\Result;
8+
use StructureCheck\ResultInterface;
9+
10+
/**
11+
* Class DatetimeType
12+
* @package StructureCheck\Type
13+
*/
14+
class DatetimeType
15+
{
16+
/**
17+
* @var string
18+
*/
19+
private static $errorMessage = 'The value %s is not a valid datetime.';
20+
21+
/**
22+
* @var string
23+
*/
24+
private $datetimeFormat;
25+
26+
/**
27+
* @var string
28+
*/
29+
private $datetimeZone;
30+
31+
/**
32+
* DatetimeType constructor.
33+
*
34+
* @param string $format
35+
* @param string $datetimeZone
36+
*/
37+
public function __construct($format, $datetimeZone)
38+
{
39+
$this->datetimeFormat = $format;
40+
$this->datetimeZone = $datetimeZone;
41+
}
42+
43+
/**
44+
* @param mixed $value
45+
*
46+
* @return ResultInterface
47+
*/
48+
public function check($value)
49+
{
50+
$checkResult = is_string($value) && $this->isValidDatetime($value);
51+
52+
return new Result(
53+
$checkResult,
54+
!$checkResult ? [sprintf(self::$errorMessage, json_encode($value))] : []
55+
);
56+
}
57+
58+
/**
59+
* @param string $value
60+
*
61+
* @return bool
62+
*/
63+
private function isValidDatetime($value) {
64+
$date = DateTime::createFromFormat($this->datetimeFormat, $value, new DateTimeZone($this->datetimeZone));
65+
$errors = DateTime::getLastErrors()["warning_count"];
66+
67+
return $date && $errors["warning_count"] == 0 && $errors["error_count"] == 0;
68+
}
69+
}

0 commit comments

Comments
 (0)