Skip to content

Commit

Permalink
Merge branch 'hotfix/29'
Browse files Browse the repository at this point in the history
Close #29
Close #30
  • Loading branch information
michalbundyra committed Mar 20, 2020
2 parents 65057f5 + 4c9ab1a commit 3e43dfc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 12 additions & 4 deletions docs/book/view-helpers/date-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@ 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:

```php
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
Expand Down
13 changes: 7 additions & 6 deletions src/View/Helper/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/View/HelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/View/Helper/DateFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use DateTime;
use IntlDateFormatter;
use IntlGregorianCalendar;
use Laminas\I18n\View\Helper\DateFormat as DateFormatHelper;
use Locale;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -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'));
}
}

0 comments on commit 3e43dfc

Please sign in to comment.