Skip to content

Commit

Permalink
Adding locale aware marshaller to DateType
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 18, 2015
1 parent e3ec6dd commit 903bd21
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Database/Type/DateType.php
Expand Up @@ -56,4 +56,13 @@ public function toPHP($value, Driver $driver)
}
return $date;
}

/**
* {@inheritDoc}
*/
protected function _parseValue($value)
{
$class = static::$dateTimeClass;
return $class::parseDate($value, $this->_localeFormat);
}
}
2 changes: 1 addition & 1 deletion src/I18n/Time.php
Expand Up @@ -734,7 +734,7 @@ public static function parseDateTime($time, $format = null)
* }}}
*
* @param string $date The date string to parse.
* @param string|array|int $format Any format accepted by IntlDateFormatter.
* @param string|int $format Any format accepted by IntlDateFormatter.
* @return static|null
*/
public static function parseDate($date, $format = null)
Expand Down
28 changes: 28 additions & 0 deletions tests/TestCase/Database/Type/DateTypeTest.php
Expand Up @@ -172,4 +172,32 @@ public function testMarshal($value, $expected)
$this->assertSame($expected, $result);
}
}

/**
* Tests marshalling dates using the locale aware parser
*
* @return void
*/
public function testMarshalWithLocaleParsing()
{
$this->type->useLocaleParser();
$expected = new Time('13-10-2013');
$result = $this->type->marshal('10/13/2013');
$this->assertEquals($expected->format('Y-m-d'), $result->format('Y-m-d'));

$this->assertNull($this->type->marshal('11/derp/2013'));
}

/**
* Tests marshalling dates using the locale aware parser and custom format
*
* @return void
*/
public function testMarshalWithLocaleParsingWithFormat()
{
$this->type->useLocaleParser()->setLocaleFormat('dd MMM, y');
$expected = new Time('13-10-2013');
$result = $this->type->marshal('13 Oct, 2013');
$this->assertEquals($expected->format('Y-m-d'), $result->format('Y-m-d'));
}
}

0 comments on commit 903bd21

Please sign in to comment.