Skip to content

Commit

Permalink
Add marshall() to DateType.
Browse files Browse the repository at this point in the history
Date type fields have 00:00:00 for the time as they are dates, not
datetimes.
  • Loading branch information
markstory committed Mar 2, 2014
1 parent edd99b3 commit f69c1e5
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/Database/Type/DateType.php
Expand Up @@ -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;
}

}
82 changes: 82 additions & 0 deletions tests/TestCase/Database/Type/DateTypeTest.php
Expand Up @@ -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.