Browse Source

Merge pull request #7231 from segy/master

added option to set json_encode formatting using setJsonEncodeFormat
José Lorenzo Rodríguez 10 years ago
parent
commit
6e6495c655
2 changed files with 33 additions and 4 deletions
  1. 30 3
      src/I18n/Time.php
  2. 3 1
      tests/TestCase/I18n/TimeTest.php

+ 30 - 3
src/I18n/Time.php

@@ -39,12 +39,28 @@ class Time extends Carbon implements JsonSerializable
      * will be used for formatting the date part of the object and the second position
      * will be used to format the time part.
      *
-     * @var mixed
+     * @var string|array|int
      * @see \Cake\I18n\Time::i18nFormat()
      */
     protected static $_toStringFormat = [IntlDateFormatter::SHORT, IntlDateFormatter::SHORT];
 
     /**
+     * The format to use when when converting this object to json
+     *
+     * The format should be either the formatting constants from IntlDateFormatter as
+     * described in (http://www.php.net/manual/en/class.intldateformatter.php) or a pattern
+     * as specified in (http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details)
+     *
+     * It is possible to provide an array of 2 constants. In this case, the first position
+     * will be used for formatting the date part of the object and the second position
+     * will be used to format the time part.
+     *
+     * @var string|array|int
+     * @see \Cake\I18n\Time::i18nFormat()
+     */
+    protected static $_jsonEncodeFormat = "yyyy-MM-dd'T'HH:mm:ssZ";
+
+    /**
      * The format to use when formatting a time using `Cake\I18n\Time::nice()`
      *
      * The format should be either the formatting constants from IntlDateFormatter as
@@ -55,7 +71,7 @@ class Time extends Carbon implements JsonSerializable
      * will be used for formatting the date part of the object and the second position
      * will be used to format the time part.
      *
-     * @var mixed
+     * @var string|array|int
      * @see \Cake\I18n\Time::nice()
      */
     public static $niceFormat = [IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT];
@@ -681,6 +697,17 @@ class Time extends Carbon implements JsonSerializable
     }
 
     /**
+     * Sets the default format used when converting this object to json
+     *
+     * @param string|array|int $format Format.
+     * @return void
+     */
+    public static function setJsonEncodeFormat($format)
+    {
+        static::$_jsonEncodeFormat = $format;
+    }
+
+    /**
      * Returns a new Time object after parsing the provided time string based on
      * the passed or configured date time format. This method is locale dependent,
      * Any string that is passed to this function will be interpreted as a locale
@@ -799,7 +826,7 @@ class Time extends Carbon implements JsonSerializable
      */
     public function jsonSerialize()
     {
-        return $this->format(static::ISO8601);
+        return $this->i18nFormat(static::$_jsonEncodeFormat);
     }
 
     /**

+ 3 - 1
tests/TestCase/I18n/TimeTest.php

@@ -191,7 +191,7 @@ class TimeTest extends TestCase
         );
         $this->assertEquals('on 31-07-1990 13:33:00', $result);
     }
-     
+
     /**
      * test the end option for timeAgoInWords
      *
@@ -688,6 +688,8 @@ class TimeTest extends TestCase
     {
         $time = new Time('2014-04-20 10:10:10');
         $this->assertEquals('"2014-04-20T10:10:10+0000"', json_encode($time));
+        Time::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss');
+        $this->assertEquals('"2014-04-20 10:10:10"', json_encode($time));
     }
 
     /**