From 7630ec1e2b744a086db412fb9dcd19f3143cbf1c Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Fri, 8 Mar 2019 11:38:43 +0100 Subject: [PATCH 1/2] Provide minimal support of Carbon 2 --- composer.json | 2 +- src/Date.php | 29 ++++++++++++++++++++--------- tests/AutomaticTest.php | 1 + tests/DateTest.php | 12 ++++++++---- tests/TranslationElTest.php | 1 + tests/TranslationHuTest.php | 1 + tests/TranslationJaTest.php | 1 + tests/TranslationKaTest.php | 1 + tests/TranslationTaTest.php | 1 + tests/TranslationTest.php | 1 + tests/TranslationThTest.php | 1 + 11 files changed, 37 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index b6096db..aa1ee97 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "require": { "php": ">=5.6", - "nesbot/carbon": "^1.0", + "nesbot/carbon": "^1.0|^2.0", "symfony/translation": "^2.7|^3.0|^4.0" }, "require-dev": { diff --git a/src/Date.php b/src/Date.php index c2efe7a..39097c3 100644 --- a/src/Date.php +++ b/src/Date.php @@ -103,23 +103,30 @@ public static function createFromFormat($format, $time, $timezone = null) * Alias for diffForHumans. * * @param Date $since - * @param bool $absolute Removes time difference modifiers ago, after, etc + * @param bool $syntax Removes time difference modifiers ago, after, etc + * @param bool $short (Carbon 2 only) displays short format of time units + * @param int $parts (Carbon 2 only) maximum number of parts to display (default value: 1: single unit) + * @param int $options (Carbon 2 only) human diff options * @return string */ - public function ago($since = null, $absolute = false) + public function ago($since = null, $syntax = null, $short = false, $parts = 1, $options = null) { - return $this->diffForHumans($since, $absolute); + return $this->diffForHumans($since, $syntax, $short, $parts, $options); } /** * Alias for diffForHumans. * * @param Date $since + * @param bool $syntax Removes time difference modifiers ago, after, etc + * @param bool $short (Carbon 2 only) displays short format of time units + * @param int $parts (Carbon 2 only) maximum number of parts to display (default value: 1: single unit) + * @param int $options (Carbon 2 only) human diff options * @return string */ - public function until($since = null) + public function until($since = null, $syntax = null, $short = false, $parts = 1, $options = null) { - return $this->ago($since); + return $this->ago($since, $syntax, $short, $parts, $options); } /** @@ -247,9 +254,11 @@ public function timespan($time = null, $timezone = null) * Adds an amount of days, months, years, hours, minutes and seconds to a Date object. * * @param DateInterval|string $interval + * @param int $value (only effective if using Carbon 2) + * @param bool|null $overflow (only effective if using Carbon 2) * @return Date|bool */ - public function add($interval) + public function add($interval, $value = 1, $overflow = null) { if (is_string($interval)) { // Check for ISO 8601 @@ -260,16 +269,18 @@ public function add($interval) } } - return parent::add($interval) ? $this : false; + return parent::add($interval, $value, $overflow) ? $this : false; } /** * Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object. * * @param DateInterval|string $interval + * @param int $value (only effective if using Carbon 2) + * @param bool|null $overflow (only effective if using Carbon 2) * @return Date|bool */ - public function sub($interval) + public function sub($interval, $value = 1, $overflow = null) { if (is_string($interval)) { // Check for ISO 8601 @@ -280,7 +291,7 @@ public function sub($interval) } } - return parent::sub($interval) ? $this : false; + return parent::sub($interval, $value, $overflow) ? $this : false; } /** diff --git a/tests/AutomaticTest.php b/tests/AutomaticTest.php index a3f7005..8c5cb4b 100644 --- a/tests/AutomaticTest.php +++ b/tests/AutomaticTest.php @@ -8,6 +8,7 @@ class AutomaticTest extends TestCase { public function setUp() { + Date::setTestNow(Date::now()); $this->languages = array_slice(scandir('src/Lang'), 2); } diff --git a/tests/DateTest.php b/tests/DateTest.php index 513c415..c458765 100644 --- a/tests/DateTest.php +++ b/tests/DateTest.php @@ -6,9 +6,13 @@ class DateTest extends TestCase { + protected $time; + public function setUp() { date_default_timezone_set('UTC'); + Date::setTestNow(Date::now()); + $this->time = Date::now()->getTimestamp(); Date::setLocale('en'); } @@ -22,7 +26,7 @@ public function testStaticNow() { $date = Date::now(); $this->assertInstanceOf('Jenssegers\Date\Date', $date); - $this->assertEquals(time(), $date->getTimestamp()); + $this->assertEquals($this->time, $date->getTimestamp()); } public function testConstructFromString() @@ -31,20 +35,20 @@ public function testConstructFromString() $this->assertSame(1359590400, $date->getTimestamp()); $date = new Date('1 day ago'); - $this->assertSame(time() - 86400, $date->getTimestamp()); + $this->assertSame($this->time - 86400, $date->getTimestamp()); } public function testConstructWithTimezone() { $date = new Date('now', 'Europe/Paris'); date_default_timezone_set('Europe/Paris'); - $this->assertSame(time(), $date->getTimestamp()); + $this->assertSame($this->time, $date->getTimestamp()); date_default_timezone_set('Europe/Brussels'); $date = new Date(null, 'Europe/Paris'); date_default_timezone_set('Europe/Paris'); - $this->assertSame(time(), $date->getTimestamp()); + $this->assertSame($this->time, $date->getTimestamp()); } public function testConstructTimestamp() diff --git a/tests/TranslationElTest.php b/tests/TranslationElTest.php index 1acea0c..ac71a84 100644 --- a/tests/TranslationElTest.php +++ b/tests/TranslationElTest.php @@ -8,6 +8,7 @@ class TranslationElTest extends TestCase public function setUp() { date_default_timezone_set('UTC'); + Date::setTestNow(Date::now()); Date::setLocale('el'); } diff --git a/tests/TranslationHuTest.php b/tests/TranslationHuTest.php index 69617ae..e9f5a4a 100644 --- a/tests/TranslationHuTest.php +++ b/tests/TranslationHuTest.php @@ -10,6 +10,7 @@ class TranslationHuTest extends TestCase public function setUp() { date_default_timezone_set('UTC'); + Date::setTestNow(Date::now()); Date::setLocale('hu'); } diff --git a/tests/TranslationJaTest.php b/tests/TranslationJaTest.php index 7014a90..1191eba 100644 --- a/tests/TranslationJaTest.php +++ b/tests/TranslationJaTest.php @@ -11,6 +11,7 @@ class TranslationJaTest extends TestCase public function setUp() { date_default_timezone_set('UTC'); + Date::setTestNow(Date::now()); Date::setLocale('ja'); } diff --git a/tests/TranslationKaTest.php b/tests/TranslationKaTest.php index 3baa900..ce5719f 100644 --- a/tests/TranslationKaTest.php +++ b/tests/TranslationKaTest.php @@ -8,6 +8,7 @@ class TranslationKaTest extends TestCase public function setUp() { date_default_timezone_set('UTC'); + Date::setTestNow(Date::now()); Date::setLocale('ka'); } diff --git a/tests/TranslationTaTest.php b/tests/TranslationTaTest.php index 2c58cda..66ccb18 100644 --- a/tests/TranslationTaTest.php +++ b/tests/TranslationTaTest.php @@ -8,6 +8,7 @@ class TranslationTaTest extends TestCase public function setUp() { date_default_timezone_set('UTC'); + Date::setTestNow(Date::now()); Date::setLocale('ta'); } diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 0dad4ac..ef39060 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -10,6 +10,7 @@ class TranslationTest extends TestCase public function setUp() { date_default_timezone_set('UTC'); + Date::setTestNow(Date::now()); } public function testGetsAndSetsTranslator() diff --git a/tests/TranslationThTest.php b/tests/TranslationThTest.php index 5deef2a..477806a 100644 --- a/tests/TranslationThTest.php +++ b/tests/TranslationThTest.php @@ -8,6 +8,7 @@ class TranslationThTest extends TestCase public function setUp() { date_default_timezone_set('UTC'); + Date::setTestNow(Date::now()); Date::setLocale('th'); } From ff4a92bb196515913e2b300ed06f38de0c8e72f5 Mon Sep 17 00:00:00 2001 From: KyleK Date: Sat, 9 Mar 2019 23:17:41 +0100 Subject: [PATCH 2/2] Fix Carbon 1 compatibility --- src/Date.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Date.php b/src/Date.php index 39097c3..28ea347 100644 --- a/src/Date.php +++ b/src/Date.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use DateInterval; use DateTimeZone; +use ReflectionMethod; use Symfony\Component\Translation\Loader\ArrayLoader; use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\TranslatorInterface; @@ -269,7 +270,12 @@ public function add($interval, $value = 1, $overflow = null) } } - return parent::add($interval, $value, $overflow) ? $this : false; + $method = new ReflectionMethod(parent::class, 'add'); + $result = $method->getNumberOfRequiredParameters() === 1 + ? parent::add($interval) + : parent::add($interval, $value, $overflow); + + return $result ? $this : false; } /** @@ -291,7 +297,12 @@ public function sub($interval, $value = 1, $overflow = null) } } - return parent::sub($interval, $value, $overflow) ? $this : false; + $method = new ReflectionMethod(parent::class, 'sub'); + $result = $method->getNumberOfRequiredParameters() === 1 + ? parent::sub($interval) + : parent::sub($interval, $value, $overflow); + + return $result ? $this : false; } /** @@ -388,7 +399,7 @@ public static function setTranslator(TranslatorInterface $translator) * @param string $time * @return string */ - public static function translateTimeString($time) + public static function translateTimeString($time, $from = null, $to = null, $mode = -1) { // Don't run translations for english. if (static::getLocale() == 'en') {