Skip to content

Commit

Permalink
Make RelativeTimeFormatter static.
Browse files Browse the repository at this point in the history
This will result in fewer object constructions and hopefully improved
performance.
  • Loading branch information
markstory committed Dec 2, 2015
1 parent 34d7259 commit fb0848e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/I18n/Date.php
Expand Up @@ -130,6 +130,6 @@ class Date extends MutableDate implements JsonSerializable
*/
public function timeAgoInWords(array $options = [])
{
return (new RelativeTimeFormatter($this))->dateAgoInWords($options);
return RelativeTimeFormatter::dateAgoInWords($this, $options);
}
}
2 changes: 1 addition & 1 deletion src/I18n/FrozenDate.php
Expand Up @@ -132,6 +132,6 @@ class FrozenDate extends ChronosDate implements JsonSerializable
*/
public function timeAgoInWords(array $options = [])
{
return (new RelativeTimeFormatter($this))->dateAgoInWords($options);
return RelativeTimeFormatter::dateAgoInWords($this, $options);
}
}
2 changes: 1 addition & 1 deletion src/I18n/FrozenTime.php
Expand Up @@ -154,7 +154,7 @@ public function __construct($time = null, $tz = null)
*/
public function timeAgoInWords(array $options = [])
{
return (new RelativeTimeFormatter($this))->timeAgoInWords($options);
return RelativeTimeFormatter::timeAgoInWords($this, $options);
}

/**
Expand Down
39 changes: 12 additions & 27 deletions src/I18n/RelativeTimeFormatter.php
Expand Up @@ -14,41 +14,26 @@
*/
namespace Cake\I18n;

use DatetimeInterface;

/**
* Helper class for formatting relative dates & times.
*
* @internal
*/
class RelativeTimeFormatter
{
/**
* The datetime instance being formatted.
*
* @var \DateTime
*/
protected $_time;

/**
* Constructor
*
* @param \DateTime $time The DateTime instance to format.
*/
public function __construct($time)
{
$this->_time = $time;
}

/**
* Format a into a relative timestring.
*
* @param \DateTimeInterface $time The time instance to format.
* @param array $options Array of options.
* @return string Relative time string.
* @see Cake\I18n\Time::timeAgoInWords()
*/
public function timeAgoInWords(array $options = [])
public static function timeAgoInWords(DatetimeInterface $time, array $options = [])
{
$time = $this->_time;
$options = $this->_options($options, FrozenTime::class);
$options = static::_options($options, FrozenTime::class);
if ($options['timezone']) {
$time = $time->timezone($options['timezone']);
}
Expand All @@ -73,7 +58,7 @@ public function timeAgoInWords(array $options = [])
return sprintf($options['absoluteString'], $time->i18nFormat($options['format']));
}

$diffData = $this->_diffData($futureTime, $pastTime, $backwards, $options);
$diffData = static::_diffData($futureTime, $pastTime, $backwards, $options);
list($fNum, $fWord, $years, $months, $weeks, $days, $hours, $minutes, $seconds) = array_values($diffData);

$relativeDate = [];
Expand Down Expand Up @@ -137,7 +122,7 @@ public function timeAgoInWords(array $options = [])
* @param array $options An array of options.
* @return array An array of values.
*/
protected function _diffData($futureTime, $pastTime, $backwards, $options)
protected static function _diffData($futureTime, $pastTime, $backwards, $options)
{
$diff = $futureTime - $pastTime;

Expand Down Expand Up @@ -226,14 +211,14 @@ protected function _diffData($futureTime, $pastTime, $backwards, $options)
/**
* Format a into a relative date string.
*
* @param \DatetimeInterface $date The date to format.
* @param array $options Array of options.
* @return string Relative date string.
* @see Cake\I18n\Date::timeAgoInWords()
*/
public function dateAgoInWords(array $options = [])
public static function dateAgoInWords(DatetimeInterface $date, array $options = [])
{
$date = $this->_time;
$options = $this->_options($options, FrozenDate::class);
$options = static::_options($options, FrozenDate::class);
if ($options['timezone']) {
$date = $date->timezone($options['timezone']);
}
Expand All @@ -258,7 +243,7 @@ public function dateAgoInWords(array $options = [])
return sprintf($options['absoluteString'], $date->i18nFormat($options['format']));
}

$diffData = $this->_diffData($futureTime, $pastTime, $backwards, $options);
$diffData = static::_diffData($futureTime, $pastTime, $backwards, $options);
list($fNum, $fWord, $years, $months, $weeks, $days, $hours, $minutes, $seconds) = array_values($diffData);

$relativeDate = [];
Expand Down Expand Up @@ -307,7 +292,7 @@ public function dateAgoInWords(array $options = [])
* @param string $class The class name to use for defaults.
* @return array Options with defaults applied.
*/
protected function _options($options, $class)
protected static function _options($options, $class)
{
$options += [
'from' => $class::now(),
Expand Down
2 changes: 1 addition & 1 deletion src/I18n/Time.php
Expand Up @@ -153,7 +153,7 @@ public function __construct($time = null, $tz = null)
*/
public function timeAgoInWords(array $options = [])
{
return (new RelativeTimeFormatter($this))->timeAgoInWords($options);
return RelativeTimeFormatter::timeAgoInWords($this, $options);
}

/**
Expand Down

0 comments on commit fb0848e

Please sign in to comment.