Browse Source

Adding locale aware marshaller to DateType

Jose Lorenzo Rodriguez 11 years ago
parent
commit
903bd218f6
3 changed files with 38 additions and 1 deletions
  1. 9 0
      src/Database/Type/DateType.php
  2. 1 1
      src/I18n/Time.php
  3. 28 0
      tests/TestCase/Database/Type/DateTypeTest.php

+ 9 - 0
src/Database/Type/DateType.php

@@ -56,4 +56,13 @@ class DateType extends \Cake\Database\Type\DateTimeType
         }
         return $date;
     }
+
+    /**
+     * {@inheritDoc}
+     */
+    protected function _parseValue($value)
+    {
+        $class = static::$dateTimeClass;
+        return $class::parseDate($value, $this->_localeFormat);
+    }
 }

+ 1 - 1
src/I18n/Time.php

@@ -734,7 +734,7 @@ class Time extends Carbon implements JsonSerializable
      * }}}
      *
      * @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)

+ 28 - 0
tests/TestCase/Database/Type/DateTypeTest.php

@@ -172,4 +172,32 @@ class DateTypeTest extends TestCase
             $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'));
+    }
 }