Skip to content

Commit

Permalink
Add a maxdiff filter to set a maximum to the "ago" formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
etshy committed Mar 3, 2019
1 parent 8111778 commit 084be7b
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 1 deletion.
78 changes: 77 additions & 1 deletion DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

namespace Knp\Bundle\TimeBundle;

use DateInterval;
use DateTime;
use Symfony\Component\Translation\TranslatorInterface;
use DatetimeInterface;

class DateTimeFormatter
{
protected $translator;

protected $maxDiff;

protected $maxDiffUnit;

protected $dateFormat;

/**
* Constructor
Expand Down Expand Up @@ -39,7 +47,50 @@ public function formatDiff(DateTimeInterface $from, DateTimeInterface $to)
);

$diff = $to->diff($from);


if ($this->maxDiff && $this->maxDiffUnit && $this->dateFormat) {
//We have the "maxDiff" options set
$timeIntervalUnits = [
'hour' => 'H',
'minute' => 'M',
'second' => 'S',
];
$intervalUnits = [
'year' => 'Y',
'month' => 'M',
'day' => 'D',
] + $timeIntervalUnits;

if (!array_key_exists($this->maxDiffUnit, $intervalUnits)) {
throw new \InvalidArgumentException(sprintf('The unit \'%s\' is not supported.', $this->maxDiffUnit));
}

//We create the interval format
$formatInterval = 'P';
$formatInterval .= (array_key_exists($this->maxDiffUnit, $timeIntervalUnits)?'T':'');
$formatInterval .= $this->maxDiff . $intervalUnits[$this->maxDiffUnit];

if ($diff->invert) {
//With the interval format we create the "maxDateTime"
$maxDiffDateTime = (clone $to)->sub(new DateInterval($formatInterval));

//The tested DateTime is older than the "maxDateTime", we display the date with the passed format
if ($maxDiffDateTime > $from)
{
return $from->format($this->dateFormat);
}
}
else {
//With the interval format we create the "maxDateTime"
$maxDiffDateTime = (clone $to)->add(new DateInterval($formatInterval));
//The tested DateTime is "newer" than the "maxDateTime", we display the date with the passed format
if ($maxDiffDateTime < $from)
{
return $from->format($this->dateFormat);
}
}
}

foreach ($units as $attribute => $unit) {
$count = $diff->$attribute;
if (0 !== $count) {
Expand Down Expand Up @@ -91,4 +142,29 @@ public function getEmptyDiffMessage()
{
return $this->translator->trans('diff.empty', array(), 'time');
}

/**
* @param mixed $maxDiff
*/
public function setMaxDiff($maxDiff)
{
$this->maxDiff = $maxDiff;
}

/**
* @param mixed $maxDiffUnit
*/
public function setMaxDiffUnit($maxDiffUnit)
{
$this->maxDiffUnit = $maxDiffUnit;
}

/**
* @param mixed $dateFormat
*/
public function setDateFormat($dateFormat)
{
$this->dateFormat = $dateFormat;
}

}
24 changes: 24 additions & 0 deletions Templating/Helper/TimeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ public function getDatetimeObject($datetime = null)

return new DateTime($datetime);
}

/**
* @param $maxDiff
*/
public function setMaxDiff($maxDiff)
{
$this->formatter->setMaxDiff($maxDiff);
}

/**
* @param $maxDiffUnit
*/
public function setMaxDiffUnit($maxDiffUnit)
{
$this->formatter->setMaxDiffUnit($maxDiffUnit);
}

/**
* @param $dateFormat
*/
public function setDateFormat($dateFormat)
{
$this->formatter->setDateFormat($dateFormat);
}

public function getName()
{
Expand Down
46 changes: 46 additions & 0 deletions Tests/DateTimeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace Knp\Bundle\TimeBundle;

use DateInterval;
use DateTime;

class DateTimeFormatterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var DateTimeFormatter
*/
protected $formatter;

public function setUp()
Expand Down Expand Up @@ -68,4 +74,44 @@ public function testGetDiffMessageThrowsAnExceptionIfTheDiffUnitIsNotSupported()

$this->formatter->getDiffMessage(1, true, 'patate');
}

public function testFormatDiffMaxDiffSet()
{
$this->formatter->setMaxDiff(1);
$this->formatter->setMaxDiffUnit('day');
$format = 'd/m/Y';
$this->formatter->setDateFormat($format);

$now = new DateTime();

$from = (clone $now)->sub(new DateInterval('P2D'));
$to = clone $now;
$result = $this->formatter->formatDiff($from, $to);
$this->assertEquals($from->format($format), $result);

$from = (clone $now)->sub(new DateInterval('PT10H'));
$to = clone $now;

$result = $this->formatter->formatDiff($from, $to);
$this->assertEquals('diff.ago.hour', $result);

//Other tests with 1 month
$this->formatter->setMaxDiff(1);
$this->formatter->setMaxDiffUnit('month');

$from = (clone $now)->sub(new DateInterval('P2D'));
$to = clone $now;
$result = $this->formatter->formatDiff($from, $to);
$this->assertEquals('diff.ago.day', $result);

$from = (clone $now)->sub(new DateInterval('P1M'));
$to = clone $now;
$result = $this->formatter->formatDiff($from, $to);
$this->assertEquals('diff.ago.month', $result);

$from = (clone $now)->sub(new DateInterval('P35D'));
$to = clone $now;
$result = $this->formatter->formatDiff($from, $to);
$this->assertEquals($from->format($format), $result);
}
}
12 changes: 12 additions & 0 deletions Twig/Extension/TimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,20 @@ public function getFilters()
array($this, 'diff'),
array('is_safe' => array('html'))
),
new \Twig_SimpleFilter(
'diffmax',
array($this, 'diffmax'),
array('is_safe' => array('html'))
),
);
}

public function diffmax($maxDiff = null, $maxDiffUnit = 'day', $dateFormat = 'd/m/Y')
{
$this->helper->setMaxDiff($maxDiff);
$this->helper->setMaxDiffUnit($maxDiffUnit);
$this->helper->setDateFormat($dateFormat);
}

public function diff($since = null, $to = null)
{
Expand Down

0 comments on commit 084be7b

Please sign in to comment.