From decac0ffbc235dc2598362b5303f09f500d9df29 Mon Sep 17 00:00:00 2001 From: Etshy Date: Sun, 3 Mar 2019 02:30:27 +0100 Subject: [PATCH 1/4] Squashed commit of the following: commit 095b4494450f5354b501c9cdb10305b91ba5f735 Author: Etshy Date: Sun Mar 3 02:22:17 2019 +0100 return date to be able chain maxdiff filter with ago filter commit da2f0b14f18f0ea9db26ce122686064014443cae Author: Etshy Date: Sun Mar 3 02:08:00 2019 +0100 Fix first argument commit 084be7b761464299fa4e10da9c45e060f2557e75 Author: Etshy Date: Sun Mar 3 01:52:15 2019 +0100 Add a maxdiff filter to set a maximum to the "ago" formatting --- DateTimeFormatter.php | 78 +++++++++++++++++++++++++++++++- Templating/Helper/TimeHelper.php | 24 ++++++++++ Tests/DateTimeFormatterTest.php | 46 +++++++++++++++++++ Twig/Extension/TimeExtension.php | 21 +++++++++ 4 files changed, 168 insertions(+), 1 deletion(-) diff --git a/DateTimeFormatter.php b/DateTimeFormatter.php index 3861780..ce28f81 100644 --- a/DateTimeFormatter.php +++ b/DateTimeFormatter.php @@ -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 @@ -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) { @@ -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; + } + } diff --git a/Templating/Helper/TimeHelper.php b/Templating/Helper/TimeHelper.php index 68e9a07..36d55d3 100644 --- a/Templating/Helper/TimeHelper.php +++ b/Templating/Helper/TimeHelper.php @@ -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() { diff --git a/Tests/DateTimeFormatterTest.php b/Tests/DateTimeFormatterTest.php index ab709fc..cb386ec 100644 --- a/Tests/DateTimeFormatterTest.php +++ b/Tests/DateTimeFormatterTest.php @@ -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() @@ -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); + } } diff --git a/Twig/Extension/TimeExtension.php b/Twig/Extension/TimeExtension.php index e577cbc..61498b6 100644 --- a/Twig/Extension/TimeExtension.php +++ b/Twig/Extension/TimeExtension.php @@ -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) { From c88969e32cbc756e7e74d740688ace651493ab31 Mon Sep 17 00:00:00 2001 From: Etshy Date: Mon, 4 Mar 2019 13:47:39 +0100 Subject: [PATCH 2/4] Change tests to pass on PHP 5 --- Tests/DateTimeFormatterTest.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/Tests/DateTimeFormatterTest.php b/Tests/DateTimeFormatterTest.php index cb386ec..99c9a6f 100644 --- a/Tests/DateTimeFormatterTest.php +++ b/Tests/DateTimeFormatterTest.php @@ -84,12 +84,16 @@ public function testFormatDiffMaxDiffSet() $now = new DateTime(); - $from = (clone $now)->sub(new DateInterval('P2D')); + $newNow = clone $now; + + $from = $newNow->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')); + $newNow = clone $now; + + $from = $newNow->sub(new DateInterval('PT10H')); $to = clone $now; $result = $this->formatter->formatDiff($from, $to); @@ -99,17 +103,23 @@ public function testFormatDiffMaxDiffSet() $this->formatter->setMaxDiff(1); $this->formatter->setMaxDiffUnit('month'); - $from = (clone $now)->sub(new DateInterval('P2D')); + $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 = (clone $now)->sub(new DateInterval('P1M')); + $from = $newNow->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')); + $newNow = clone $now; + + $from = $newNow->sub(new DateInterval('P35D')); $to = clone $now; $result = $this->formatter->formatDiff($from, $to); $this->assertEquals($from->format($format), $result); From 3237fa945e58f4076dc3dd47d71102c55499b75a Mon Sep 17 00:00:00 2001 From: Etshy Date: Mon, 4 Mar 2019 19:12:44 +0100 Subject: [PATCH 3/4] Fix php 5.5/5.6 errors --- DateTimeFormatter.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DateTimeFormatter.php b/DateTimeFormatter.php index ce28f81..aa26493 100644 --- a/DateTimeFormatter.php +++ b/DateTimeFormatter.php @@ -72,7 +72,8 @@ public function formatDiff(DateTimeInterface $from, DateTimeInterface $to) if ($diff->invert) { //With the interval format we create the "maxDateTime" - $maxDiffDateTime = (clone $to)->sub(new DateInterval($formatInterval)); + $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) @@ -82,7 +83,8 @@ public function formatDiff(DateTimeInterface $from, DateTimeInterface $to) } else { //With the interval format we create the "maxDateTime" - $maxDiffDateTime = (clone $to)->add(new DateInterval($formatInterval)); + $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) { From 2c2d25fe6bf72b51920796be0e64799447a8b8d0 Mon Sep 17 00:00:00 2001 From: Etshy Date: Fri, 21 Feb 2020 19:21:33 +0100 Subject: [PATCH 4/4] Update composer.json --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 181e3ad..1d864f8 100644 --- a/composer.json +++ b/composer.json @@ -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": {