File tree Expand file tree Collapse file tree 2 files changed +101
-0
lines changed Expand file tree Collapse file tree 2 files changed +101
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ipl \Validator ;
4
+
5
+ use DateTime ;
6
+ use ipl \I18n \Translation ;
7
+
8
+ /**
9
+ * Validator for date-and-time input controls
10
+ */
11
+ class DateTimeValidator extends BaseValidator
12
+ {
13
+ use Translation;
14
+
15
+ /** @var string Default date time format */
16
+ const FORMAT = 'Y-m-d\TH:i:s ' ;
17
+
18
+ /** @var bool Whether to use the default date time format */
19
+ protected $ local ;
20
+
21
+ /**
22
+ * Create a new date-and-time input control validator
23
+ *
24
+ * @param bool $local
25
+ */
26
+ public function __construct ($ local = true )
27
+ {
28
+ $ this ->local = (bool ) $ local ;
29
+ }
30
+
31
+ /**
32
+ * Check whether the given date time is valid
33
+ *
34
+ * @param string|DateTime $value
35
+ *
36
+ * @return bool
37
+ */
38
+ public function isValid ($ value )
39
+ {
40
+ // Multiple isValid() calls must not stack validation messages
41
+ $ this ->clearMessages ();
42
+
43
+ if (! $ value instanceof DateTime && ! is_string ($ value )) {
44
+ $ this ->addMessage ($ this ->translate ('Invalid date/time given. ' ));
45
+
46
+ return false ;
47
+ }
48
+
49
+ if (! $ value instanceof DateTime) {
50
+ $ format = $ this ->local === true ? static ::FORMAT : DateTime::RFC3339 ;
51
+ $ dateTime = DateTime::createFromFormat ($ format , $ value );
52
+
53
+ if ($ dateTime === false || $ dateTime ->format ($ format ) !== $ value ) {
54
+ $ this ->addMessage (sprintf (
55
+ $ this ->translate ("Date/time string not in the expected format: %s " ),
56
+ $ format
57
+ ));
58
+
59
+ return false ;
60
+ }
61
+ }
62
+
63
+ return true ;
64
+ }
65
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ipl \Tests \Validator ;
4
+
5
+ use DateTime ;
6
+ use ipl \I18n \NoopTranslator ;
7
+ use ipl \I18n \StaticTranslator ;
8
+ use ipl \Validator \DateTimeValidator ;
9
+
10
+ class DateTimeValidatorTest extends TestCase
11
+ {
12
+ public function testDateTimeValidatorWithValidDateTime ()
13
+ {
14
+ StaticTranslator::$ instance = new NoopTranslator ();
15
+ $ this ->assertTrue ((new DateTimeValidator ())->isValid (new DateTime ()), 'current date is a valid date ' );
16
+ }
17
+
18
+ public function testDateTimeValidatorWithFalseAsDateTimeValue ()
19
+ {
20
+ StaticTranslator::$ instance = new NoopTranslator ();
21
+ $ validator = new DateTimeValidator ();
22
+
23
+ $ this ->assertFalse ($ validator ->isValid (false ), 'false is not a valid date ' );
24
+ }
25
+
26
+ public function testDateTimeValidatorWithStringAsDateTimeValue ()
27
+ {
28
+ StaticTranslator::$ instance = new NoopTranslator ();
29
+ $ validator = new DateTimeValidator ();
30
+
31
+ $ this ->assertTrue ($ validator ->isValid ('2021-02-15T15:03:01 ' ), '15th Feb is a valid date ' );
32
+ $ this ->assertFalse ($ validator ->isValid ('2021-02-31T15:03:01 ' ), '31st Feb is not a valid date ' );
33
+ $ this ->assertFalse ($ validator ->isValid ('2021-02-03T26:03:01 ' ), "26 o'clock is not a valid time " );
34
+ $ this ->assertFalse ($ validator ->isValid ('' ), 'Empty value is not a valid date ' );
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments