Browse Source

Further integrate chronos into cakephp.

Finish integration with the database abstraction layer, and database
type mappers. The change to immutable datetimes may end up breaking some
applications, but hopefully not many.
Mark Story 10 years ago
parent
commit
c6d973ff62

+ 4 - 5
src/Database/Type/DateTimeType.php

@@ -16,7 +16,8 @@ namespace Cake\Database\Type;
 
 use Cake\Database\Driver;
 use Cake\Database\Type;
-use DateTime;
+use DateTimeInterface;
+use DateTimeImmutable;
 use Exception;
 use RuntimeException;
 
@@ -126,7 +127,7 @@ class DateTimeType extends Type
      */
     public function marshal($value)
     {
-        if ($value instanceof DateTime) {
+        if ($value instanceof DateTime || $value instanceof DateTimeImmutable) {
             return $value;
         }
 
@@ -196,9 +197,7 @@ class DateTimeType extends Type
             $this->_useLocaleParser = $enable;
             return $this;
         }
-        if (static::$dateTimeClass === 'Cake\I18n\Time' ||
-            is_subclass_of(static::$dateTimeClass, 'Cake\I18n\Time')
-        ) {
+        if (method_exists(static::$dateTimeClass, 'parseDateTime')) {
             $this->_useLocaleParser = $enable;
             return $this;
         }

+ 7 - 1
src/Database/Type/DateType.php

@@ -15,10 +15,16 @@
 namespace Cake\Database\Type;
 
 use Cake\Database\Driver;
-use DateTime;
+use DateTimeImmutable;
 
 class DateType extends DateTimeType
 {
+    /**
+     * The class to use for representing date objects
+     *
+     * @var string
+     */
+    public static $dateTimeClass = 'Cake\I18n\Date';
 
     /**
      * Date format for DateTime object

+ 11 - 2
src/I18n/Date.php

@@ -40,11 +40,20 @@ class Date extends BaseDate implements JsonSerializable
      * will be used to format the time part.
      *
      * @var string|array|int
-     * @see \Cake\I18n\Time::i18nFormat()
+     * @see \Cake\I18n\DateFormatTrait::i18nFormat()
      */
     protected static $_toStringFormat = [IntlDateFormatter::SHORT, -1];
 
     /**
+     * The format to use when formatting a time using `Cake\I18n\Time::timeAgoInWords()`
+     * and the difference is more than `Cake\I18n\Time::$wordEnd`
+     *
+     * @var string
+     * @see \Cake\I18n\DateFormatTrait::parseDate()
+     */
+    public static $wordFormat = [IntlDateFormatter::SHORT, -1];
+
+    /**
      * The format to use when formatting a time using `Cake\I18n\Time::nice()`
      *
      * The format should be either the formatting constants from IntlDateFormatter as
@@ -56,7 +65,7 @@ class Date extends BaseDate implements JsonSerializable
      * will be used to format the time part.
      *
      * @var string|array|int
-     * @see \Cake\I18n\Time::nice()
+     * @see \Cake\I18n\DateFormatTrait::nice()
      */
     public static $niceFormat = [IntlDateFormatter::MEDIUM, -1];
 }

+ 1 - 0
src/I18n/Time.php

@@ -61,6 +61,7 @@ class Time extends Chronos implements JsonSerializable
      * @see \Cake\I18n\Time::nice()
      */
     public static $niceFormat = [IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT];
+
     /**
      * The format to use when formatting a time using `Cake\I18n\Time::timeAgoInWords()`
      * and the difference is more than `Cake\I18n\Time::$wordEnd`

+ 11 - 11
tests/TestCase/Database/Type/DateTypeTest.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Test\TestCase\Database\Type;
 
+use Cake\Chronos\Date;
 use Cake\Database\Type;
 use Cake\Database\Type\DateType;
 use Cake\I18n\Time;
@@ -48,7 +49,7 @@ class DateTypeTest extends TestCase
         $this->assertNull($this->type->toPHP('0000-00-00', $this->driver));
 
         $result = $this->type->toPHP('2001-01-04', $this->driver);
-        $this->assertInstanceOf('DateTime', $result);
+        $this->assertInstanceOf('DateTimeImmutable', $result);
         $this->assertEquals('2001', $result->format('Y'));
         $this->assertEquals('01', $result->format('m'));
         $this->assertEquals('04', $result->format('d'));
@@ -81,8 +82,7 @@ class DateTypeTest extends TestCase
      */
     public function marshalProvider()
     {
-        $date = new Time('@1392387900');
-        $date->setTime(0, 0, 0);
+        $date = new Date('@1392387900');
 
         return [
             // invalid types.
@@ -98,7 +98,7 @@ class DateTypeTest extends TestCase
             // valid string types
             ['1392387900', $date],
             [1392387900, $date],
-            ['2014-02-14', new Time('2014-02-14')],
+            ['2014-02-14', new Date('2014-02-14')],
 
             // valid array types
             [
@@ -107,7 +107,7 @@ class DateTypeTest extends TestCase
             ],
             [
                 ['year' => 2014, 'month' => 2, 'day' => 14, 'hour' => 13, 'minute' => 14, 'second' => 15],
-                new Time('2014-02-14 00:00:00')
+                new Date('2014-02-14')
             ],
             [
                 [
@@ -115,7 +115,7 @@ class DateTypeTest extends TestCase
                     'hour' => 1, 'minute' => 14, 'second' => 15,
                     'meridian' => 'am'
                 ],
-                new Time('2014-02-14 00:00:00')
+                new Date('2014-02-14')
             ],
             [
                 [
@@ -123,30 +123,30 @@ class DateTypeTest extends TestCase
                     'hour' => 1, 'minute' => 14, 'second' => 15,
                     'meridian' => 'pm'
                 ],
-                new Time('2014-02-14 00:00:00')
+                new Date('2014-02-14')
             ],
             [
                 [
                     'year' => 2014, 'month' => 2, 'day' => 14,
                 ],
-                new Time('2014-02-14 00:00:00')
+                new Date('2014-02-14')
             ],
 
             // Invalid array types
             [
                 ['year' => 'farts', 'month' => 'derp'],
-                new Time(date('Y-m-d 00:00:00'))
+                new Date(date('Y-m-d'))
             ],
             [
                 ['year' => 'farts', 'month' => 'derp', 'day' => 'farts'],
-                new Time(date('Y-m-d 00:00:00'))
+                new Date(date('Y-m-d'))
             ],
             [
                 [
                     'year' => '2014', 'month' => '02', 'day' => '14',
                     'hour' => 'farts', 'minute' => 'farts'
                 ],
-                new Time('2014-02-14 00:00:00')
+                new Date('2014-02-14')
             ],
         ];
     }

+ 1 - 1
tests/TestCase/Database/Type/TimeTypeTest.php

@@ -53,7 +53,7 @@ class TimeTypeTest extends TestCase
         $this->assertEquals('15', $result->format('s'));
 
         $result = $this->type->toPHP('16:30:15', $this->driver);
-        $this->assertInstanceOf('DateTime', $result);
+        $this->assertInstanceOf('DateTimeImmutable', $result);
         $this->assertEquals('16', $result->format('H'));
         $this->assertEquals('30', $result->format('i'));
         $this->assertEquals('15', $result->format('s'));

+ 1 - 1
tests/TestCase/ORM/Behavior/TimestampBehaviorTest.php

@@ -225,7 +225,7 @@ class TimestampBehaviorTest extends TestCase
 
         $return = $this->Behavior->timestamp();
         $this->assertInstanceOf(
-            'DateTime',
+            'DateTimeImmutable',
             $return,
             'Should return a timestamp object'
         );

+ 1 - 1
tests/TestCase/ORM/MarshallerTest.php

@@ -2087,7 +2087,7 @@ class MarshallerTest extends TestCase
         ];
         $marshall = new Marshaller($this->comments);
         $result = $marshall->merge($entity, $data);
-        $this->assertInstanceOf('DateTime', $entity->created);
+        $this->assertInstanceOf('DateTimeImmutable', $entity->created);
         $this->assertEquals('2014-02-14', $entity->created->format('Y-m-d'));
     }