Skip to content

Commit 6474265

Browse files
author
Benoît Burnichon
committed
Simplify FormatConstraint using helper class
1 parent b9ddba5 commit 6474265

File tree

1 file changed

+3
-18
lines changed

1 file changed

+3
-18
lines changed

src/JsonSchema/Constraints/FormatConstraint.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
namespace JsonSchema\Constraints;
11+
use JsonSchema\Rfc3339;
1112

1213
/**
1314
* Validates against the "format" property
@@ -40,11 +41,7 @@ public function check($element, $schema = null, $path = null, $i = null)
4041
break;
4142

4243
case 'date-time':
43-
if (!$this->validateDateTime($element, 'Y-m-d\TH:i:s\Z') &&
44-
!$this->validateDateTime($element, 'Y-m-d\TH:i:s.u\Z') &&
45-
!$this->validateDateTime($element, 'Y-m-d\TH:i:sP') &&
46-
!$this->validateDateTime($element, 'Y-m-d\TH:i:sO')
47-
) {
44+
if (null === Rfc3339::createFromString($element)) {
4845
$this->addError($path, sprintf('Invalid date-time %s, expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm', json_encode($element)), 'format', array('format' => $schema->format,));
4946
}
5047
break;
@@ -130,19 +127,7 @@ protected function validateDateTime($datetime, $format)
130127
return false;
131128
}
132129

133-
if ($datetime === $dt->format($format)) {
134-
return true;
135-
}
136-
137-
// handles the case where a non-6 digit microsecond datetime is passed
138-
// which will fail the above string comparison because the passed
139-
// $datetime may be '2000-05-01T12:12:12.123Z' but format() will return
140-
// '2000-05-01T12:12:12.123000Z'
141-
if ((strpos('u', $format) !== -1) && (intval($dt->format('u')) > 0)) {
142-
return true;
143-
}
144-
145-
return false;
130+
return $datetime === $dt->format($format);
146131
}
147132

148133
protected function validateRegex($regex)

0 commit comments

Comments
 (0)