This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zendframework/zendframework#3632 from davidwindell…
…/zf3617 New DateTimeFormatter Filter (zendframework/zendframework#3617)
- Loading branch information
93 parents
78201e3
+
4a23da8
+
aed1a4d
+
d208606
+
3f667bd
+
617a96b
+
ddb5852
+
6e16921
+
9418fac
+
17b5ef4
+
846edfb
+
216ac0e
+
aecb8f4
+
ef44cf1
+
bc4610b
+
8c773ad
+
0481606
+
d1304e2
+
d3d5a56
+
533570c
+
6e30cb0
+
82c88d1
+
88c4f3b
+
c96ebb3
+
f0320b1
+
cb0afaa
+
128be4b
+
34cd6a3
+
ec2aab9
+
809bd88
+
77026e3
+
c2796da
+
0ee8602
+
bf4e728
+
cd33c2d
+
32497e5
+
79eb7d0
+
8cc5d58
+
869ab1c
+
436e613
+
ab7d56c
+
6dbeef6
+
84670b1
+
cccfba9
+
2717653
+
b4dd364
+
460818e
+
b5ac15d
+
2d218aa
+
046d669
+
5597f80
+
98dc299
+
01b3149
+
b0177b5
+
74f8270
+
62799e5
+
512426e
+
5a9a928
+
3b3e691
+
6f57ae9
+
116cf37
+
bb991fd
+
1ce1893
+
5000327
+
32beb38
+
fe88f0e
+
49921e6
+
6387996
+
9623b87
+
fef33e6
+
a55293c
+
37a7d0c
+
aeda378
+
1681df7
+
8557890
+
792de65
+
072f053
+
fbc1502
+
482716d
+
0da8a0e
+
7d9112c
+
ca312d7
+
cef4359
+
711d775
+
7826111
+
14b10f4
+
7b6700e
+
f304770
+
57e491b
+
c3425d2
+
d591073
+
2235288
+
e94a735
commit 0f73b43
Showing
2 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Filter; | ||
|
||
use DateTime; | ||
|
||
class DateTimeFormatter extends AbstractFilter | ||
{ | ||
/** | ||
* A valid format string accepted by date() | ||
* | ||
* @var string | ||
*/ | ||
protected $format = DateTime::ISO8601; | ||
|
||
/** | ||
* Sets filter options | ||
* | ||
* @param array|Traversable $options | ||
*/ | ||
public function __construct($options = null) | ||
{ | ||
if ($options) { | ||
$this->setOptions($options); | ||
} | ||
} | ||
|
||
/** | ||
* Set the format string accepted by date() to use when formatting a string | ||
* | ||
* @param string $format | ||
* @return \Zend\Filter\DateTimeFormatter | ||
*/ | ||
public function setFormat($format) | ||
{ | ||
$this->format = $format; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Filter a datetime string by normalizing it to the filters specified format | ||
* | ||
* @param string $value | ||
* @throws Exception\InvalidArgumentException | ||
* @return string | ||
*/ | ||
public function filter($value) | ||
{ | ||
try { | ||
$result = $this->normalizeDateTime($value); | ||
} catch (\Exception $ex) { | ||
// DateTime threw an exception, an invalid date string was provided | ||
throw new Exception\InvalidArgumentException($ex); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Normalize the provided value to a formatted string | ||
* | ||
* @param string|int|DateTime $value | ||
* @returns string | ||
*/ | ||
protected function normalizeDateTime($value) | ||
{ | ||
if (is_int($value)) { | ||
$dateTime = new DateTime('@' . $value); | ||
} elseif (!$value instanceof DateTime) { | ||
$dateTime = new DateTime($value); | ||
} | ||
|
||
return $dateTime->format($this->format); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Filter | ||
*/ | ||
|
||
namespace ZendTest\Filter; | ||
|
||
use DateTime; | ||
use Zend\Filter\DateTimeFormatter; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Filter | ||
* @subpackage UnitTests | ||
* @group Zend_Filter | ||
*/ | ||
class DateTimeFormatterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $defaultTimezone; | ||
|
||
public function setUp() | ||
{ | ||
$this->defaultTimezone = date_default_timezone_get(); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
date_default_timezone_set($this->defaultTimezone); | ||
} | ||
|
||
public function testDateTimeFormatted() | ||
{ | ||
date_default_timezone_set('UTC'); | ||
|
||
$filter = new DateTimeFormatter(); | ||
$result = $filter->filter('2012-01-01'); | ||
$this->assertEquals('2012-01-01T00:00:00+0000', $result); | ||
} | ||
|
||
public function testDateTimeFormattedWithAlternateTimezones() | ||
{ | ||
$filter = new DateTimeFormatter(); | ||
|
||
date_default_timezone_set('Europe/Paris'); | ||
|
||
$resultParis = $filter->filter('2012-01-01'); | ||
$this->assertEquals('2012-01-01T00:00:00+0100', $resultParis); | ||
|
||
date_default_timezone_set('America/New_York'); | ||
|
||
$resultNewYork = $filter->filter('2012-01-01'); | ||
$this->assertEquals('2012-01-01T00:00:00-0500', $resultNewYork); | ||
} | ||
|
||
public function testSetFormat() | ||
{ | ||
date_default_timezone_set('UTC'); | ||
|
||
$filter = new DateTimeFormatter(); | ||
$filter->setFormat(DateTime::RFC1036); | ||
$result = $filter->filter('2012-01-01'); | ||
$this->assertEquals('Sun, 01 Jan 12 00:00:00 +0000', $result); | ||
} | ||
|
||
public function testFormatDateTimeFromTimestamp() | ||
{ | ||
date_default_timezone_set('UTC'); | ||
|
||
$filter = new DateTimeFormatter(); | ||
$result = $filter->filter(1359739801); | ||
$this->assertEquals('2013-02-01T17:30:01+0000', $result); | ||
} | ||
|
||
public function testInvalidArgumentExceptionThrownOnInvalidInput() | ||
{ | ||
$this->setExpectedException('Zend\Filter\Exception\InvalidArgumentException'); | ||
|
||
$filter = new DateTimeFormatter(); | ||
$result = $filter->filter('2013-31-31'); | ||
} | ||
} |