File tree Expand file tree Collapse file tree 3 files changed +105
-0
lines changed Expand file tree Collapse file tree 3 files changed +105
-0
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,7 @@ Currently the following types are supported:
6969 * String
7070 * Object
7171 * List
72+ * Datetime
7273
7374There are some open issues with ideas for more types. Feel free to send pull requests.
7475
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments