Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

namespace JsonSchema\Constraints;
use JsonSchema\Rfc3339;

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

case 'date-time':
if (!$this->validateDateTime($element, 'Y-m-d\TH:i:s\Z') &&
!$this->validateDateTime($element, 'Y-m-d\TH:i:s.u\Z') &&
!$this->validateDateTime($element, 'Y-m-d\TH:i:sP') &&
!$this->validateDateTime($element, 'Y-m-d\TH:i:sO')
) {
if (null === Rfc3339::createFromString($element)) {
$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,));
}
break;
Expand Down Expand Up @@ -130,19 +127,7 @@ protected function validateDateTime($datetime, $format)
return false;
}

if ($datetime === $dt->format($format)) {
return true;
}

// handles the case where a non-6 digit microsecond datetime is passed
// which will fail the above string comparison because the passed
// $datetime may be '2000-05-01T12:12:12.123Z' but format() will return
// '2000-05-01T12:12:12.123000Z'
if ((strpos('u', $format) !== -1) && (intval($dt->format('u')) > 0)) {
return true;
}

return false;
return $datetime === $dt->format($format);
}

protected function validateRegex($regex)
Expand Down
29 changes: 29 additions & 0 deletions src/JsonSchema/Rfc3339.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace JsonSchema;

class Rfc3339
{
const REGEX = '/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))/';

/**
* Try creating a DateTime instance
*
* @param string $string
* @return \DateTime|null
*/
public static function createFromString($string)
{
if (!preg_match(self::REGEX, strtoupper($string), $matches)) {
return null;
}

$dateAndTime = $matches[1];
$microseconds = $matches[2] ?: '.000000';
$timeZone = 'Z' !== $matches[3] ? $matches[4] . ':' . $matches[5] : '+00:00';

$dateTime = \DateTime::createFromFormat('Y-m-d\TH:i:s.uP', $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC'));

return $dateTime ?: null;
}
}
59 changes: 59 additions & 0 deletions tests/JsonSchema/Tests/Rfc3339Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace JsonSchema\Tests;

use JsonSchema\Rfc3339;

class Rfc3339Test extends \PHPUnit_Framework_TestCase
{
/**
* @param string $string
* @param \DateTime|null $expected
* @dataProvider provideValidFormats
*/
public function testCreateFromValidString($string, \DateTime $expected)
{
$actual = Rfc3339::createFromString($string);

$this->assertInstanceOf('DateTime', $actual);
$this->assertEquals($expected->format('U.u'), $actual->format('U.u'));
}

/**
* @param string $string
* @dataProvider provideInvalidFormats
*/
public function testCreateFromInvalidString($string)
{
$this->assertNull(Rfc3339::createFromString($string), sprintf('String "%s" should not be converted to DateTime', $string));
}

public function provideValidFormats()
{
return array(
array(
'2000-05-01T12:12:12Z',
\DateTime::createFromFormat('Y-m-d\TH:i:s', '2000-05-01T12:12:12', new \DateTimeZone('UTC'))
),
array('2000-05-01T12:12:12+0100', \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')),
array('2000-05-01T12:12:12+01:00', \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')),
array(
'2000-05-01T12:12:12.123456Z',
\DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123456', new \DateTimeZone('UTC'))
),
array(
'2000-05-01T12:12:12.123Z',
\DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123000', new \DateTimeZone('UTC'))
),
);
}

public function provideInvalidFormats()
{
return array(
array('1999-1-11T00:00:00Z'),
array('1999-01-11T00:00:00+100'),
array('1999-01-11T00:00:00+1:00'),
);
}
}