Skip to content

Commit 9a07f3c

Browse files
committed
Make isIso8601DateString private
1 parent 11529c4 commit 9a07f3c

File tree

2 files changed

+53
-44
lines changed

2 files changed

+53
-44
lines changed

src/GraphNode/GraphNode.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -248,33 +248,6 @@ public function uncastFields()
248248
}, $fields);
249249
}
250250

251-
/**
252-
* Detects an ISO 8601 formatted string.
253-
*
254-
* @param string $string
255-
*
256-
* @return bool
257-
*
258-
* @see https://developers.facebook.com/docs/graph-api/using-graph-api/#readmodifiers
259-
* @see http://www.cl.cam.ac.uk/~mgk25/iso-time.html
260-
* @see http://en.wikipedia.org/wiki/ISO_8601
261-
*/
262-
public function isIso8601DateString($string)
263-
{
264-
// This insane regex was yoinked from here:
265-
// http://www.pelagodesign.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
266-
// ...and I'm all like:
267-
// http://thecodinglove.com/post/95378251969/when-code-works-and-i-dont-know-why
268-
$crazyInsaneRegexThatSomehowDetectsIso8601 = '/^([\+-]?\d{4}(?!\d{2}\b))'
269-
. '((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?'
270-
. '|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d'
271-
. '|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])'
272-
. '((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d'
273-
. '([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/';
274-
275-
return preg_match($crazyInsaneRegexThatSomehowDetectsIso8601, $string) === 1;
276-
}
277-
278251
/**
279252
* Casts a date value from Graph to DateTime.
280253
*
@@ -337,4 +310,31 @@ private function shouldCastAsDateTime($key)
337310
'publish_time'
338311
], true);
339312
}
313+
314+
/**
315+
* Detects an ISO 8601 formatted string.
316+
*
317+
* @param string $string
318+
*
319+
* @return bool
320+
*
321+
* @see https://developers.facebook.com/docs/graph-api/using-graph-api/#readmodifiers
322+
* @see http://www.cl.cam.ac.uk/~mgk25/iso-time.html
323+
* @see http://en.wikipedia.org/wiki/ISO_8601
324+
*/
325+
private function isIso8601DateString($string)
326+
{
327+
// This insane regex was yoinked from here:
328+
// http://www.pelagodesign.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/
329+
// ...and I'm all like:
330+
// http://thecodinglove.com/post/95378251969/when-code-works-and-i-dont-know-why
331+
$crazyInsaneRegexThatSomehowDetectsIso8601 = '/^([\+-]?\d{4}(?!\d{2}\b))'
332+
. '((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?'
333+
. '|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d'
334+
. '|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])'
335+
. '((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d'
336+
. '([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/';
337+
338+
return preg_match($crazyInsaneRegexThatSomehowDetectsIso8601, $string) === 1;
339+
}
340340
}

tests/GraphNode/GraphNodeTest.php

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,38 @@ public static function provideDateTimeFieldNames()
6666
yield ['publish_time'];
6767
}
6868

69-
public function testDatesThatShouldBeCastAsDateTimeObjectsAreDetected()
69+
/**
70+
* @dataProvider provideValidDateTimeFieldValues
71+
*/
72+
public function testIsCastDateTimeFieldValueToDateTime($value, $message)
7073
{
71-
$graphNode = new GraphNode();
74+
$graphNode = new GraphNode(['created_time' => $value]);
7275

73-
// Should pass
74-
$shouldPass = $graphNode->isIso8601DateString('1985-10-26T01:21:00+0000');
75-
$this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date from Back To The Future to pass.');
76-
77-
$shouldPass = $graphNode->isIso8601DateString('1999-12-31');
78-
$this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date to party like it\'s 1999.');
76+
$this->assertInstanceOf(\DateTime::class, $graphNode->getField('created_time'), $message);
77+
}
7978

80-
$shouldPass = $graphNode->isIso8601DateString('2009-05-19T14:39Z');
81-
$this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date to pass.');
79+
public static function provideValidDateTimeFieldValues()
80+
{
81+
yield ['1985-10-26T01:21:00+0000', 'Expected the valid ISO 8601 formatted date from Back To The Future to pass.'];
82+
yield ['1999-12-31', 'Expected the valid ISO 8601 formatted date to party like it\'s 1999.'];
83+
yield ['2009-05-19T14:39Z', 'Expected the valid ISO 8601 formatted date to pass.'];
84+
yield ['2014-W36', 'Expected the valid ISO 8601 formatted date to pass.'];
85+
}
8286

83-
$shouldPass = $graphNode->isIso8601DateString('2014-W36');
84-
$this->assertTrue($shouldPass, 'Expected the valid ISO 8601 formatted date to pass.');
87+
/**
88+
* @dataProvider provideInvalidDateTimeFieldValues
89+
*/
90+
public function testIsNotCastDateTimeFieldValueToDateTime($value, $message)
91+
{
92+
$graphNode = new GraphNode(['created_time' => $value]);
8593

86-
// Should fail
87-
$shouldFail = $graphNode->isIso8601DateString('2009-05-19T14a39r');
88-
$this->assertFalse($shouldFail, 'Expected the invalid ISO 8601 format to fail.');
94+
$this->assertNotInstanceOf(\DateTime::class, $graphNode->getField('created_time'), $message);
95+
}
8996

90-
$shouldFail = $graphNode->isIso8601DateString('foo_time');
91-
$this->assertFalse($shouldFail, 'Expected the invalid ISO 8601 format to fail.');
97+
public static function provideInvalidDateTimeFieldValues()
98+
{
99+
yield ['2009-05-19T14a39r', 'Expected the invalid ISO 8601 format to fail.'];
100+
yield ['foo_time', 'Expected the invalid ISO 8601 format to fail.'];
92101
}
93102

94103
public function testATimeStampCanBeConvertedToADateTimeObject()

0 commit comments

Comments
 (0)