Browse Source

Fixing fatal errors in PHP 5.4

Jose Lorenzo Rodriguez 12 years ago
parent
commit
093117bd5a
2 changed files with 35 additions and 2 deletions
  1. 0 1
      .travis.yml
  2. 35 1
      src/Utility/Time.php

+ 0 - 1
.travis.yml

@@ -25,7 +25,6 @@ matrix:
       env: HHVM=1 DB=mysql db_class='Cake\Database\Driver\Mysql' db_dsn='mysql:host=0.0.0.0;dbname=cakephp_test' db_database='cakephp_test' db_login='travis' db_password=''
 
 before_script:
-  - yes '' | pecl install -f intl
   - composer self-update
   - composer install --prefer-source --no-interaction --dev
   - sh -c "if [ '$PHPCS' != '1' ]; then sudo locale-gen de_DE; fi"

+ 35 - 1
src/Utility/Time.php

@@ -528,7 +528,41 @@ class Time extends Carbon {
 
 		$format = $format !== null ? $format : static::$_toStringFormat;
 		$locale = $locale ?: static::$defaultLocale;
-		return IntlDateFormatter::formatObject($time, $format, $locale);
+		return $this->_formatObject($time, $format, $locale);
+	}
+
+/**
+ * Returns a translated and localized date string.
+ * Implements what IntlDateFormatter::formatObject() is in PHP 5.5+
+ *
+ * @param \DateTime $date
+ * @param string|int|array $format
+ * @param string $locale
+ * @return string
+ */
+	protected function _formatObject($date, $format, $locale) {
+		$pattern = $dateFormat = $timeFormat = $calendar = null;
+
+		if (is_array($format)) {
+			list($dateFormat, $timeFormat) = $format;
+		} elseif (is_numeric($format)) {
+			$dateFormat = $format;
+		} else {
+			$dateFormat = $timeFormat = IntlDateFormatter::FULL;
+			$pattern = $format;
+		}
+
+		$timezone = $date->getTimezone();
+		$formatter = datefmt_create(
+			$locale,
+			$dateFormat,
+			$timeFormat,
+			$timezone,
+			$calendar,
+			$pattern
+		);
+
+		return $formatter->format($date);
 	}
 
 /**