Browse Source

Re-implemeting the Time helper to use the new Time library

Jose Lorenzo Rodriguez 12 years ago
parent
commit
ef1df55da0
1 changed files with 36 additions and 176 deletions
  1. 36 176
      src/View/Helper/TimeHelper.php

+ 36 - 176
src/View/Helper/TimeHelper.php

@@ -14,13 +14,9 @@
  */
 namespace Cake\View\Helper;
 
-use Cake\Core\App;
-use Cake\Core\Configure;
-use Cake\Error;
-use Cake\Utility\Hash;
 use Cake\View\Helper;
 use Cake\View\Helper\StringTemplateTrait;
-use Cake\View\View;
+use Cake\Utility\Time;
 
 /**
  * Time Helper class for easy use of time data.
@@ -35,209 +31,81 @@ class TimeHelper extends Helper {
 	use StringTemplateTrait;
 
 /**
- * Default config for this class
- *
- * @var array
- */
-	protected $_defaultConfig = [
-		'engine' => 'Cake\Utility\Time'
-	];
-
-/**
- * Cake\Utility\Time instance
- *
- * @var \Cake\Utility\Time
- */
-	protected $_engine = null;
-
-/**
- * Constructor
- *
- * ### Settings:
- *
- * - `engine` Class name to use to replace Cake\Utility\Time functionality
- *            The class needs to be placed in the `Utility` directory.
- *
- * @param View $View the view object the helper is attached to.
- * @param array $config Settings array
- * @throws \Cake\Error\Exception When the engine class could not be found.
- */
-	public function __construct(View $View, array $config = array()) {
-		parent::__construct($View, $config);
-
-		$config = $this->_config;
-
-		$engineClass = App::classname($config['engine'], 'Utility');
-		if ($engineClass) {
-			$this->_engine = new $engineClass($config);
-		} else {
-			throw new Error\Exception(sprintf('Class for %s could not be found', $config['engine']));
-		}
-	}
-
-/**
- * Call methods from Cake\Utility\Time utility class
- *
- * @param string $method Method to invoke
- * @param array $params Array of params for the method.
- * @return mixed Whatever is returned by called method, or false on failure
- */
-	public function __call($method, $params) {
-		return call_user_func_array(array($this->_engine, $method), $params);
-	}
-
-/**
- * Converts a string representing the format for the function strftime and returns a
- * windows safe and i18n aware format.
- *
- * @see \Cake\Utility\Time::convertSpecifiers()
- *
- * @param string $format Format with specifiers for strftime function.
- *    Accepts the special specifier %S which mimics the modifier S for date()
- * @param string $time UNIX timestamp
- * @return string windows safe and date() function compatible format for strftime
- * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
- */
-	public function convertSpecifiers($format, $time = null) {
-		return $this->_engine->convertSpecifiers($format, $time);
-	}
-
-/**
- * Converts given time (in server's time zone) to user's local time, given his/her timezone.
- *
- * @see \Cake\Utility\Time::convert()
- *
- * @param string $serverTime UNIX timestamp
- * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
- * @return int UNIX timestamp
- * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
- */
-	public function convert($serverTime, $timezone) {
-		return $this->_engine->convert($serverTime, $timezone);
-	}
-
-/**
  * Returns a UNIX timestamp, given either a UNIX timestamp or a valid strtotime() date string.
  *
- * @see \Cake\Utility\Time::fromString()
- *
  * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
- * @return string Parsed timestamp
+ * @return Cake\Utility\Time
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
 	public function fromString($dateString, $timezone = null) {
-		return $this->_engine->fromString($dateString, $timezone);
+		return (new Time($dateString))->timezone($timezone);
 	}
 
 /**
  * Returns a nicely formatted date string for given Datetime string.
  *
- * @see \Cake\Utility\Time::nice()
- *
  * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
  * @param string $format The format to use. If null, `CakeTime::$niceFormat` is used
  * @return string Formatted date string
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
-	public function nice($dateString = null, $timezone = null, $format = null) {
-		return $this->_engine->nice($dateString, $timezone, $format);
-	}
-
-/**
- * Returns a partial SQL string to search for all records between two dates.
- *
- * @see \Cake\Utility\Time::daysAsSql()
- *
- * @param int|string|\DateTime $begin UNIX timestamp, strtotime() valid string or DateTime object
- * @param int|string|\DateTime $end UNIX timestamp, strtotime() valid string or DateTime object
- * @param string $fieldName Name of database field to compare with
- * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
- * @return string Partial SQL string.
- * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
- */
-	public function daysAsSql($begin, $end, $fieldName, $timezone = null) {
-		return $this->_engine->daysAsSql($begin, $end, $fieldName, $timezone);
-	}
-
-/**
- * Returns a partial SQL string to search for all records between two times
- * occurring on the same day.
- *
- * @see \Cake\Utility\Time::dayAsSql()
- *
- * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
- * @param string $fieldName Name of database field to compare with
- * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
- * @return string Partial SQL string.
- * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
- */
-	public function dayAsSql($dateString, $fieldName, $timezone = null) {
-		return $this->_engine->dayAsSql($dateString, $fieldName, $timezone);
+	public function nice($dateString = null, $timezone = null, $locale = null) {
+		return (new Time($dateString))->nice($timezone, $locale);
 	}
 
 /**
  * Returns true if given datetime string is today.
  *
- * @see \Cake\Utility\Time::isToday()
- *
  * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
  * @return bool True if datetime string is today
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  */
 	public function isToday($dateString, $timezone = null) {
-		return $this->_engine->isToday($dateString, $timezone);
+		return (new Time($dateString, $timezone))->isToday();
 	}
 
 /**
  * Returns true if given datetime string is within this week.
  *
- * @see \Cake\Utility\Time::isThisWeek()
- *
  * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
  * @return bool True if datetime string is within current week
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  */
 	public function isThisWeek($dateString, $timezone = null) {
-		return $this->_engine->isThisWeek($dateString, $timezone);
+		return (new Time($dateString, $timezone))->isThisWeek();
 	}
 
 /**
  * Returns true if given datetime string is within this month
  *
- * @see \Cake\Utility\Time::isThisMonth()
- *
  * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
  * @return bool True if datetime string is within current month
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  */
 	public function isThisMonth($dateString, $timezone = null) {
-		return $this->_engine->isThisMonth($dateString, $timezone);
+		return (new Time($dateString, $timezone))->isThisMonth();
 	}
 
 /**
  * Returns true if given datetime string is within current year.
  *
- * @see \Cake\Utility\Time::isThisYear()
- *
  * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
  * @return bool True if datetime string is within current year
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  */
 	public function isThisYear($dateString, $timezone = null) {
-		return $this->_engine->isThisYear($dateString, $timezone);
+		return (new Time($dateString, $timezone))->isThisYear();
 	}
 
 /**
  * Returns true if given datetime string was yesterday.
  *
- * @see \Cake\Utility\Time::wasYesterday()
- *
  * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
  * @return bool True if datetime string was yesterday
@@ -245,21 +113,19 @@ class TimeHelper extends Helper {
  *
  */
 	public function wasYesterday($dateString, $timezone = null) {
-		return $this->_engine->wasYesterday($dateString, $timezone);
+		return (new Time($dateString, $timezone))->isYesterday();
 	}
 
 /**
  * Returns true if given datetime string is tomorrow.
  *
- * @see \Cake\Utility\Time::isTomorrow()
- *
  * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
  * @return bool True if datetime string was yesterday
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  */
 	public function isTomorrow($dateString, $timezone = null) {
-		return $this->_engine->isTomorrow($dateString, $timezone);
+		return (new Time($dateString, $timezone))->isTomorrow();
 	}
 
 /**
@@ -273,7 +139,7 @@ class TimeHelper extends Helper {
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
 	public function toQuarter($dateString, $range = false) {
-		return $this->_engine->toQuarter($dateString, $range);
+		return (new Time($dateString))->toQuarter($range);
 	}
 
 /**
@@ -287,7 +153,7 @@ class TimeHelper extends Helper {
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
 	public function toUnix($dateString, $timezone = null) {
-		return $this->_engine->toUnix($dateString, $timezone);
+		return (new Time($dateString, $timezone))->toUnixString();
 	}
 
 /**
@@ -301,21 +167,19 @@ class TimeHelper extends Helper {
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
 	public function toAtom($dateString, $timezone = null) {
-		return $this->_engine->toAtom($dateString, $timezone);
+		return (new Time($dateString, $timezone))->toATOMString();
 	}
 
 /**
  * Formats date for RSS feeds
  *
- * @see \Cake\Utility\Time::toRSS()
- *
  * @param int|string|\DateTime $dateString UNIX timestamp, strtotime() valid string or DateTime object
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
  * @return string Formatted date string
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
 	public function toRSS($dateString, $timezone = null) {
-		return $this->_engine->toRSS($dateString, $timezone);
+		return (new Time($dateString, $timezone))->toRSSString();
 	}
 
 /**
@@ -353,7 +217,7 @@ class TimeHelper extends Helper {
 			}
 			unset($options['element']);
 		}
-		$relativeDate = $this->_engine->timeAgoInWords($dateTime, $options);
+		$relativeDate = (new Time($dateTime))->timeAgoInWords($options);
 
 		if ($element) {
 			$relativeDate = sprintf(
@@ -369,7 +233,6 @@ class TimeHelper extends Helper {
 
 /**
  * Returns true if specified datetime was within the interval specified, else false.
- *
  * @see \Cake\Utility\Time::wasWithinLast()
  *
  * @param string|int $timeInterval the numeric value with space then time type.
@@ -380,7 +243,7 @@ class TimeHelper extends Helper {
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  */
 	public function wasWithinLast($timeInterval, $dateString, $timezone = null) {
-		return $this->_engine->wasWithinLast($timeInterval, $dateString, $timezone);
+		return (new Time($dateString, $timezone))->wasWithinLast($timeInterval);
 	}
 
 /**
@@ -396,7 +259,7 @@ class TimeHelper extends Helper {
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#testing-time
  */
 	public function isWithinNext($timeInterval, $dateString, $timezone = null) {
-		return $this->_engine->isWithinNext($timeInterval, $dateString, $timezone);
+		return (new Time($dateString, $timezone))->isWithinNext($timeInterval);
 	}
 
 /**
@@ -409,36 +272,25 @@ class TimeHelper extends Helper {
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
 	public function gmt($string = null) {
-		return $this->_engine->gmt($string);
+		return (new Time($string))->toUnixString();
 	}
 
 /**
  * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
  * This function also accepts a time string and a format string as first and second parameters.
- * In that case this function behaves as a wrapper for TimeHelper::i18nFormat()
- *
- * ## Examples
- *
- * Create localized & formatted time:
+ * In that case this function behaves as a wrapper for Time::i18nFormat()
  *
- * {{{
- *   $this->Time->format('2012-02-15', '%m-%d-%Y'); // returns 02-15-2012
- *   $this->Time->format('2012-02-15 23:01:01', '%c'); // returns preferred date and time based on configured locale
- *   $this->Time->format('0000-00-00', '%d-%m-%Y', 'N/A'); // return N/A becuase an invalid date was passed
- *   $this->Time->format('2012-02-15 23:01:01', '%c', 'N/A', 'America/New_York'); // converts passed date to timezone
- * }}}
- *
- * @see \Cake\Utility\Time::format()
+ * @see \Cake\Utility\Time::i18nFormat()
  *
- * @param int|string|\DateTime $format date format string (or a UNIX timestamp, strtotime() valid string or DateTime object)
  * @param int|string|\DateTime $date UNIX timestamp, strtotime() valid string or DateTime object (or a date format string)
- * @param bool $invalid flag to ignore results of fromString == false
+ * @param int|string $format date format string (or a UNIX timestamp, strtotime() valid string or DateTime object)
+ * @param bool|string $invalid Default value to display on invalid dates
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
- * @return string Formatted date string
+ * @return string Formatted and translated date string
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
-	public function format($format, $date = null, $invalid = false, $timezone = null) {
-		return $this->_engine->format($format, $date, $invalid, $timezone);
+	public function format($date, $format = null, $invalid = false, $timezone = null) {
+		return $this->i18nFormat($date, $format, $invalid, $timezone);
 	}
 
 /**
@@ -449,13 +301,21 @@ class TimeHelper extends Helper {
  *
  * @param int|string|\DateTime $date UNIX timestamp, strtotime() valid string or DateTime object
  * @param string $format strftime format string.
- * @param bool $invalid flag to ignore results of fromString == false
+ * @param bool|string $invalid Default value to display on invalid dates
  * @param string|\DateTimeZone $timezone User's timezone string or DateTimeZone object
  * @return string Formatted and translated date string
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#formatting
  */
 	public function i18nFormat($date, $format = null, $invalid = false, $timezone = null) {
-		return $this->_engine->i18nFormat($date, $format, $invalid, $timezone);
+		try {
+			$time = new Time($date, $timezone);
+			return $time->i18nFormat($format, $timezone);
+		} catch (\Exception $e) {
+			if ($invalid === false) {
+				throw $e;
+			}
+			return $invalid;
+		}
 	}
 
 /**