diff --git a/CHANGELOG.md b/CHANGELOG.md index 867b312a..3cfa9f2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed -- Nothing. +- [#29](https://github.com/laminas/laminas-i18n/pull/29) fixes typehint in `DateFormat` view helper. ## 2.10.1 - 2019-12-12 diff --git a/docs/book/view-helpers/date-format.md b/docs/book/view-helpers/date-format.md index e204cfe7..fc919f88 100755 --- a/docs/book/view-helpers/date-format.md +++ b/docs/book/view-helpers/date-format.md @@ -7,10 +7,12 @@ internationalization extension (`ext/intl`). ## Basic Usage -The value for the date must be a -[`DateTimeInterface`](https://www.php.net/DateTimeInterface) object, an integer -representing a Unix timestamp value, or an array in the format returned by -[`localtime()` function](https://www.php.net/localtime). +The value for the date must be: + +* an object which implements the [`DateTimeInterface`](https://www.php.net/DateTimeInterface), +* an [`IntlCalendar`](https://www.php.net/IntlCalendar) object, +* an integer representing a Unix timestamp value +* or an array in the format returned by [`localtime()` function](https://www.php.net/localtime) Example with a `DateTime` instance: @@ -18,6 +20,12 @@ Example with a `DateTime` instance: echo $this->dateFormat(new DateTime()); // '20190222 09:07 PM' ``` +Example with an `IntlCalendar` instance: + +```php +echo $this->dateFormat(IntlCalendar::createInstance()); // '20190222 09:07 PM' +``` + Example with an Unix timestamp: ```php diff --git a/src/View/Helper/DateFormat.php b/src/View/Helper/DateFormat.php index fb0f75fe..25c44e17 100644 --- a/src/View/Helper/DateFormat.php +++ b/src/View/Helper/DateFormat.php @@ -8,7 +8,8 @@ namespace Laminas\I18n\View\Helper; -use DateTime; +use DateTimeInterface; +use IntlCalendar; use IntlDateFormatter; use Laminas\I18n\Exception; use Laminas\View\Helper\AbstractHelper; @@ -56,11 +57,11 @@ public function __construct() /** * Format a date * - * @param DateTime|int|array $date - * @param int $dateType - * @param int $timeType - * @param string|null $locale - * @param string|null $pattern + * @param DateTimeInterface|IntlCalendar|int|array $date + * @param int $dateType + * @param int $timeType + * @param string|null $locale + * @param string|null $pattern * @return string */ public function __invoke( diff --git a/src/View/HelperTrait.php b/src/View/HelperTrait.php index 1d132f7c..3595cbf4 100644 --- a/src/View/HelperTrait.php +++ b/src/View/HelperTrait.php @@ -26,7 +26,7 @@ * @example @var \Laminas\View\Renderer\PhpRenderer|\Laminas\I18n\View\HelperTrait $this * * @method string currencyFormat(float $number, string|null $currencyCode = null, bool|null $showDecimals = null, string|null $locale = null, string|null $pattern = null) - * @method string dateFormat(\DateTime|int|array $date, int $dateType = IntlDateFormatter::NONE, int $timeType = IntlDateFormatter::NONE, string|null $locale = null, string|null $pattern = null) + * @method string dateFormat(\DateTimeInterface|\IntlCalendar|int|array $date, int $dateType = IntlDateFormatter::NONE, int $timeType = IntlDateFormatter::NONE, string|null $locale = null, string|null $pattern = null) * @method string numberFormat(int|float $number, int|null $formatStyle = null, int|null $formatType = null, string|null $locale = null, int|null $decimals = null, array|null $textAttributes = null) * @method string plural(array|string $strings, int $number) * @method string translate(string $message, string|null $textDomain = null, string|null $locale = null) diff --git a/test/View/Helper/DateFormatTest.php b/test/View/Helper/DateFormatTest.php index 71a743e1..5c1b3199 100644 --- a/test/View/Helper/DateFormatTest.php +++ b/test/View/Helper/DateFormatTest.php @@ -10,6 +10,7 @@ use DateTime; use IntlDateFormatter; +use IntlGregorianCalendar; use Laminas\I18n\View\Helper\DateFormat as DateFormatHelper; use Locale; use PHPUnit\Framework\TestCase; @@ -300,4 +301,13 @@ public function testDifferentTimezone() self::assertSame('Jan 1, 2018', $helper($date, IntlDateFormatter::MEDIUM)); } + + public function testIntlCalendarIsHandledAsWell() + { + $calendar = new IntlGregorianCalendar(2013, 6, 1); + + $helper = new DateFormatHelper(); + $helper->setTimezone('Europe/Berlin'); + $this->assertEquals('01-07-2013', $helper->__invoke($calendar, null, null, 'it_IT', 'dd-MM-Y')); + } }