This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
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#7277 from carnage/date…
…-select-filter Date select filter
- Loading branch information
150 parents
7683d5c
+
87e12cd
+
a349d33
+
7ea6197
+
eb8d23e
+
f47ca61
+
797cb3c
+
7df9cd3
+
bc13fad
+
90cc541
+
35ec740
+
acfbbcd
+
691454a
+
3de0bd7
+
700917d
+
f10c3b2
+
4f82a12
+
ba0d8a9
+
c140dfc
+
6c6a51d
+
473be1c
+
33a0274
+
56352fd
+
8119cdf
+
61d3103
+
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
+
d4694ac
+
56d8e16
+
07f05df
+
622531f
+
641a66a
+
4938b37
+
4bda1f3
+
b1e478c
+
bf4b889
+
fe09441
+
b5e7532
+
149b9b1
+
90ed0aa
+
e6d18f9
+
3417ce7
+
6fe2e03
+
c8263a5
+
163cb93
+
c5ac758
+
477bd0b
+
fda1abb
+
6b484d8
+
79a0d3d
+
6fdffb1
+
cf50c78
+
a2b31ea
+
f374b87
+
10d41ef
+
8933479
+
5bba450
+
163c322
+
e3472c4
+
5b271b3
+
87c9f5b
commit 72705fc
Showing
8 changed files
with
428 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,132 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Filter; | ||
|
||
/** | ||
* Class AbstractDateDropdown | ||
* @package Zend\Filter | ||
*/ | ||
abstract class AbstractDateDropdown extends AbstractFilter | ||
{ | ||
/** | ||
* If true, the filter will return null if any date field is empty | ||
* | ||
* @var bool | ||
*/ | ||
protected $nullOnEmpty = false; | ||
|
||
/** | ||
* If true, the filter will return null if all date fields are empty | ||
* | ||
* @var bool | ||
*/ | ||
protected $nullOnAllEmpty = false; | ||
|
||
/** | ||
* Sprintf format string to use for formatting the date, fields will be used in alphabetical order. | ||
* | ||
* @var string | ||
*/ | ||
protected $format = ''; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $expectedInputs; | ||
|
||
/** | ||
* @param boolean $nullOnAllEmpty | ||
* @return $this | ||
*/ | ||
public function setNullOnAllEmpty($nullOnAllEmpty) | ||
{ | ||
$this->nullOnAllEmpty = $nullOnAllEmpty; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return boolean | ||
*/ | ||
public function nullOnAllEmpty() | ||
{ | ||
return $this->nullOnAllEmpty; | ||
} | ||
|
||
/** | ||
* @param boolean $nullOnEmpty | ||
* @return $this | ||
*/ | ||
public function setNullOnEmpty($nullOnEmpty) | ||
{ | ||
$this->nullOnEmpty = $nullOnEmpty; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return boolean | ||
*/ | ||
public function nullOnEmpty() | ||
{ | ||
return $this->nullOnEmpty; | ||
} | ||
|
||
/** | ||
* Returns the result of filtering $value | ||
* | ||
* @param mixed $value | ||
* @throws Exception\RuntimeException If filtering $value is impossible | ||
* @return mixed | ||
*/ | ||
public function filter($value) | ||
{ | ||
// Convert the date to a specific format | ||
if (is_array($value)) { | ||
if ( | ||
$this->nullOnEmpty() && | ||
array_reduce($value, function ($soFar, $value) { return $soFar | empty($value); }, false) | ||
) { | ||
return; | ||
} | ||
|
||
if ( | ||
$this->nullOnAllEmpty() && | ||
array_reduce($value, function ($soFar, $value) { return $soFar & empty($value); }, true) | ||
) { | ||
return; | ||
} | ||
|
||
$this->filterable($value); | ||
|
||
ksort($value); | ||
$value = vsprintf($this->format, $value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Ensures there are enough inputs in the array to properly format the date. | ||
* | ||
* @param $value | ||
* @throws Exception\RuntimeException | ||
*/ | ||
protected function filterable($value) | ||
{ | ||
if (count($value) !== $this->expectedInputs) { | ||
throw new Exception\RuntimeException( | ||
sprintf( | ||
'There are not enough values in the array to filter this date (Required: %d, Received: %d)', | ||
$this->expectedInputs, | ||
count($value) | ||
) | ||
); | ||
} | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Filter; | ||
|
||
/** | ||
* Class DateSelect | ||
* @package Zend\Filter | ||
*/ | ||
class DateSelect extends AbstractDateDropdown | ||
{ | ||
/** | ||
* Year-Month-Day | ||
* | ||
* @var string | ||
*/ | ||
protected $format = '%3$s-%2$s-%1$s'; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $expectedInputs = 3; | ||
} |
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,76 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Filter; | ||
|
||
/** | ||
* Class DateTimeSelect | ||
* @package Zend\Filter | ||
*/ | ||
class DateTimeSelect extends AbstractDateDropdown | ||
{ | ||
/** | ||
* Year-Month-Day Hour:Min:Sec | ||
* | ||
* @var string | ||
*/ | ||
protected $format = '%6$s-%4$s-%1$s %2$s:%3$s:%5$s'; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $expectedInputs = 6; | ||
|
||
/** | ||
* @param mixed $value | ||
* @throws Exception\RuntimeException | ||
* @return array|mixed|null|string | ||
*/ | ||
public function filter($value) | ||
{ | ||
if (is_array($value)) { | ||
if ($this->nullOnEmpty() && ( | ||
empty($value['year']) || | ||
empty($value['month']) || | ||
empty($value['day']) || | ||
empty($value['hour']) || | ||
empty($value['minute']) || | ||
(isset($value['second']) && empty($value['second'])) | ||
) | ||
) { | ||
return; | ||
} | ||
|
||
if ($this->nullOnAllEmpty() && ( | ||
empty($value['year']) && | ||
empty($value['month']) && | ||
empty($value['day']) && | ||
empty($value['hour']) && | ||
empty($value['minute']) && | ||
(!isset($value['second']) || empty($value['second'])) | ||
|
||
) | ||
) { | ||
return; | ||
} | ||
|
||
if (!isset($value['second'])) { | ||
$value['second'] = '00'; | ||
} | ||
|
||
$this->filterable($value); | ||
|
||
ksort($value); | ||
|
||
$value = vsprintf($this->format, $value); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
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
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,29 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Filter; | ||
|
||
/** | ||
* Class MonthSelect | ||
* @package Zend\Filter | ||
*/ | ||
class MonthSelect extends AbstractDateDropdown | ||
{ | ||
/** | ||
* Year-Month | ||
* | ||
* @var string | ||
*/ | ||
protected $format = '%2$s-%1$s'; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $expectedInputs = 2; | ||
} |
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,49 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Filter; | ||
|
||
use Zend\Filter\DateSelect as DateSelectFilter; | ||
|
||
/** | ||
* @group Zend_Filter | ||
*/ | ||
class DateSelectTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider provideFilter | ||
* @param $options | ||
* @param $input | ||
* @param $expected | ||
*/ | ||
public function testFilter($options, $input, $expected) | ||
{ | ||
$sut = new DateSelectFilter(); | ||
$sut->setOptions($options); | ||
$this->assertEquals($expected, $sut->filter($input)); | ||
} | ||
|
||
public function provideFilter() | ||
{ | ||
return array( | ||
array(array(), array('year' => '2014', 'month' => '10', 'day' => '26'), '2014-10-26'), | ||
array(array('nullOnEmpty' => true), array('year' => null, 'month' => '10', 'day' => '26'), null), | ||
array(array('nullOnAllEmpty' => true), array('year' => null, 'month' => null, 'day' => null), null), | ||
); | ||
} | ||
|
||
/** | ||
* @expectedException \Zend\Filter\Exception\RuntimeException | ||
*/ | ||
public function testInvalidInput() | ||
{ | ||
$sut = new DateSelectFilter(); | ||
$sut->filter(array('year' => '2120', 'month' => '07')); | ||
} | ||
} |
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,61 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Filter; | ||
|
||
use Zend\Filter\DateTimeSelect as DateTimeSelectFilter; | ||
|
||
/** | ||
* @group Zend_Filter | ||
*/ | ||
class DateTimeSelectTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider provideFilter | ||
* @param $options | ||
* @param $input | ||
* @param $expected | ||
*/ | ||
public function testFilter($options, $input, $expected) | ||
{ | ||
$sut = new DateTimeSelectFilter(); | ||
$sut->setOptions($options); | ||
$this->assertEquals($expected, $sut->filter($input)); | ||
} | ||
|
||
public function provideFilter() | ||
{ | ||
return array( | ||
array( | ||
array(), | ||
array('year' => '2014', 'month' => '10', 'day' => '26', 'hour' => '12', 'minute' => '35'), | ||
'2014-10-26 12:35:00' | ||
), | ||
array( | ||
array('nullOnEmpty' => true), | ||
array('year' => null, 'month' => '10', 'day' => '26', 'hour' => '12', 'minute' => '35'), | ||
null | ||
), | ||
array( | ||
array('nullOnAllEmpty' => true), | ||
array('year' => null, 'month' => null, 'day' => null, 'hour' => null, 'minute' => null), | ||
null | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* @expectedException \Zend\Filter\Exception\RuntimeException | ||
*/ | ||
public function testInvalidInput() | ||
{ | ||
$sut = new DateTimeSelectFilter(); | ||
$sut->filter(array('year' => '2120', 'month' => '10', 'day' => '26', 'hour' => '12')); | ||
} | ||
} |
Oops, something went wrong.