Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"diffMax" filter additon #116

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 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,52 @@ 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"
$clonedTo = clone $to;
$maxDiffDateTime = $clonedTo->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"
$clonedTo = clone $to;
$maxDiffDateTime = $clonedTo->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 +144,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
56 changes: 56 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,54 @@ 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();

$newNow = clone $now;

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

$newNow = clone $now;

$from = $newNow->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');

$newNow = clone $now;

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

$newNow = clone $now;

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

$newNow = clone $now;

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

/**
* @param $date \DateTime first argument, the value we want to display
* @param null $maxDiff
* @param string $dateFormat
* @param string $maxDiffUnit
*
* @return \DateTime
*/
public function diffmax($date, $maxDiff = null, $dateFormat = 'd/m/Y', $maxDiffUnit = 'day')
{
$this->helper->setMaxDiff($maxDiff);
$this->helper->setMaxDiffUnit($maxDiffUnit);
$this->helper->setDateFormat($dateFormat);
return $date;
}

public function diff($since = null, $to = null)
{
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

"require": {
"php": ">=5.5",
"symfony/dependency-injection": "~2.3|~3.0|~4.0",
"symfony/templating": "~2.3|~3.0|~4.0",
"symfony/translation": "~2.3|~3.0|~4.0",
"symfony/config": "~2.3|~3.0|~4.0"
"symfony/dependency-injection": "~2.3|~3.0|~4.0|~5.0",
"symfony/templating": "~2.3|~3.0|~4.0|~5.0",
"symfony/translation": "~2.3|~3.0|~4.0|~5.0",
"symfony/config": "~2.3|~3.0|~4.0|~5.0"
},

"require-dev": {
Expand Down