Skip to content
Browse files

Add marshall() to DateType.

Date type fields have 00:00:00 for the time as they are dates, not
datetimes.
  • Loading branch information...
1 parent edd99b3 commit f69c1e5799cbdc96304cda8a24f10185911a2666 @markstory markstory committed
Showing with 116 additions and 1 deletion.
  1. +34 −1 src/Database/Type/DateType.php
  2. +82 −0 tests/TestCase/Database/Type/DateTypeTest.php
View
35 src/Database/Type/DateType.php
@@ -49,5 +49,38 @@ public function toPHP($value, Driver $driver) {
return DateTime::createFromFormat('Y-m-d', $value);
}
-}
+/**
+ * Convert request data into a datetime object.
+ *
+ * @param mixed $value Request data
+ * @return \DateTime
+ */
+ public function marshall($value) {
+ try {
+ if ($value === '' || $value === null || $value === false || $value === true) {
+ return $value;
+ } elseif (is_numeric($value)) {
+ $date = new DateTime('@' . $value);
+ } elseif (is_string($value)) {
+ $date = new DateTime($value);
+ }
+ if (isset($date)) {
+ $date->setTime(0, 0, 0);
+ return $date;
+ }
+ } catch (\Exception $e) {
+ return $value;
+ }
+ $date = new DateTime();
+ if (
+ isset($value['year'], $value['month'], $value['day']) &&
+ (is_numeric($value['year']) & is_numeric($value['month']) && is_numeric($value['day']))
+ ) {
+ $date->setDate($value['year'], $value['month'], $value['day']);
+ }
+ $date->setTime(0, 0, 0);
+ return $date;
+ }
+
+}
View
82 tests/TestCase/Database/Type/DateTypeTest.php
@@ -73,4 +73,86 @@ public function testToDatabase() {
$this->assertEquals('2013-08-12', $result);
}
+/**
+ * Data provider for marshall()
+ *
+ * @return array
+ */
+ public static function marshallProvider() {
+ $date = new \DateTime('@1392387900');
+ $date->setTime(0, 0, 0);
+
+ return [
+ // invalid types.
+ [null, null],
+ [false, false],
+ [true, true],
+ ['', ''],
+ ['derpy', 'derpy'],
+ ['2013-nope!', '2013-nope!'],
+
+ // valid string types
+ ['1392387900', $date],
+ [1392387900, $date],
+ ['2014-02-14', new \DateTime('2014-02-14')],
+ ['2014-02-14 13:14:15', new \DateTime('2014-02-14 00:00:00')],
+
+ // valid array types
+ [
+ ['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
+ new \DateTime('2014-02-14 00:00:00')
+ ],
+ [
+ [
+ 'year' => 2014, 'month' => 2, 'day' => 14,
+ 'hour' => 1, 'minute' => 14, 'second' => 15,
+ 'meridian' => 'am'
+ ],
+ new \DateTime('2014-02-14 00:00:00')
+ ],
+ [
+ [
+ 'year' => 2014, 'month' => 2, 'day' => 14,
+ 'hour' => 1, 'minute' => 14, 'second' => 15,
+ 'meridian' => 'pm'
+ ],
+ new \DateTime('2014-02-14 00:00:00')
+ ],
+ [
+ [
+ 'year' => 2014, 'month' => 2, 'day' => 14,
+ ],
+ new \DateTime('2014-02-14 00:00:00')
+ ],
+
+ // Invalid array types
+ [
+ ['year' => 'farts', 'month' => 'derp'],
+ new \DateTime(date('Y-m-d 00:00:00'))
+ ],
+ [
+ ['year' => 'farts', 'month' => 'derp', 'day' => 'farts'],
+ new \DateTime(date('Y-m-d 00:00:00'))
+ ],
+ [
+ [
+ 'year' => '2014', 'month' => '02', 'day' => '14',
+ 'hour' => 'farts', 'minute' => 'farts'
+ ],
+ new \DateTime('2014-02-14 00:00:00')
+ ],
+ ];
+ }
+
+/**
+ * test marshalling data.
+ *
+ * @dataProvider marshallProvider
+ * @return void
+ */
+ public function testMarshall($value, $expected) {
+ $result = $this->type->marshall($value);
+ $this->assertEquals($expected, $result);
+ }
+
}

0 comments on commit f69c1e5

Please sign in to comment.
Something went wrong with that request. Please try again.