-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7726def
commit 8484dc8
Showing
9 changed files
with
421 additions
and
3 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
37 changes: 37 additions & 0 deletions
37
src/MpaCustomDoctrineHydrator/Factory/DateTimeElementFactory.php
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,37 @@ | ||
<?php | ||
/* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
namespace MpaCustomDoctrineHydrator\Factory; | ||
|
||
use Locale; | ||
use MpaCustomDoctrineHydrator\Form\Element\DateTime; | ||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
|
||
class DateTimeElementFactory implements FactoryInterface | ||
{ | ||
/** | ||
* Create service | ||
* | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @return DateTime | ||
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
$parentLocator = $serviceLocator->getServiceLocator(); | ||
$config = $parentLocator->get('Config')['mpacustomdoctrinehydrator']['formats'][Locale::getDefault()]; | ||
|
||
$formElement = new DateTime(); | ||
$formElement->setFormat($config['datetime_format']); | ||
$formElement->setAttribute('placeholder', $config['datetime_placeholder']); | ||
|
||
return $formElement; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/MpaCustomDoctrineHydrator/Factory/DateTimeToDateTimeFilterFactory.php
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,36 @@ | ||
<?php | ||
/* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
namespace MpaCustomDoctrineHydrator\Factory; | ||
|
||
use Locale; | ||
use MpaCustomDoctrineHydrator\Filter\DateTimeToDateTime; | ||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
|
||
class DateTimeToDateTimeFilterFactory implements FactoryInterface | ||
{ | ||
/** | ||
* Create service | ||
* | ||
* @param ServiceLocatorInterface $serviceLocator | ||
* @return DateTimeToDateTime | ||
*/ | ||
public function createService(ServiceLocatorInterface $serviceLocator) | ||
{ | ||
$parentLocator = $serviceLocator->getServiceLocator(); | ||
|
||
$filter = new DateTimeToDateTime( | ||
$parentLocator->get('Config')['mpacustomdoctrinehydrator']['formats'][Locale::getDefault()] | ||
); | ||
|
||
return $filter; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/MpaCustomDoctrineHydrator/Filter/DateTimeToDateTime.php
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,30 @@ | ||
<?php | ||
/* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
namespace MpaCustomDoctrineHydrator\Filter; | ||
|
||
class DateTimeToDateTime extends DateToDateTime | ||
{ | ||
protected $format = 'Y.m.d H:i:s'; | ||
|
||
/** | ||
* Allow the format key to be format and datetime_format | ||
* For consistency with the ZF2 Date Element | ||
* | ||
* @param array $options | ||
* @return self | ||
*/ | ||
public function setOptions($options) | ||
{ | ||
$this->format = isset($options['datetime_format']) ? $options['datetime_format'] : $options['format']; | ||
|
||
return $this; | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
/* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
namespace MpaCustomDoctrineHydrator\Form\Element; | ||
|
||
use Zend\Filter\StringTrim; | ||
|
||
class DateTime extends Date | ||
{ | ||
/** | ||
* Accepted options for DateTime: | ||
* - format: A \DateTime compatible string | ||
* | ||
* @param array|\Traversable $options | ||
* @return \DateTime | ||
*/ | ||
public function setOptions($options) | ||
{ | ||
parent::setOptions($options); | ||
|
||
if (isset($this->options['datetime_format'])) { | ||
$this->setFormat($this->options['datetime_format']); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Provide default input rules for this element | ||
* Attaches default validators for the Date input. | ||
* | ||
* @return array | ||
*/ | ||
public function getInputSpecification() | ||
{ | ||
return [ | ||
'name' => $this->getName(), | ||
'required' => true, | ||
'filters' => [ | ||
['name' => StringTrim::class], | ||
[ | ||
'name' => 'MpaCustomDoctrineHydrator\Filter\DateTimeToDateTime', | ||
'options' => [ | ||
'datetime_format' => $this->getFormat(), | ||
] | ||
], | ||
], | ||
'validators' => [ | ||
$this->getDateValidator() | ||
], | ||
]; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
tests/MpaCustomDoctrineHydratorTest/Factory/DateTimeElementFactoryTest.php
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,71 @@ | ||
<?php | ||
/* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
namespace MpaCustomDoctrineHydratorTest\Factory; | ||
|
||
use MpaCustomDoctrineHydrator\Form\Element\DateTime; | ||
use MpaCustomDoctrineHydratorTest\Util\ServiceManagerFactory; | ||
use Zend\Form\Element\DateTime as ZendDateTime; | ||
|
||
class DateTimeElementFactoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $serviceManager; | ||
|
||
protected function setUp() | ||
{ | ||
\Locale::setDefault('fr-CH'); | ||
$this->serviceManager = ServiceManagerFactory::getServiceManager(); | ||
} | ||
|
||
public function testWeCanGrabTheElementByItsFactory() | ||
{ | ||
$formElementManager = $this->serviceManager->get('FormElementManager'); | ||
|
||
$this->assertInstanceOf( | ||
DateTime::class, | ||
$formElementManager->get('MpaCustomDoctrineHydrator\Form\Element\DateTime') | ||
); | ||
} | ||
|
||
public function testZendFormElementDateTimeIsOverriddenByAlias() | ||
{ | ||
$formElementManager = $this->serviceManager->get('FormElementManager'); | ||
|
||
$this->assertInstanceOf( | ||
DateTime::class, | ||
$formElementManager->get('Zend\Form\Element\DateTime') | ||
); | ||
} | ||
|
||
public function testElementNamesOverrideZfOnes() | ||
{ | ||
$formElementManager = $this->serviceManager->get('FormElementManager'); | ||
|
||
$this->assertInstanceOf(DateTime::class, $formElementManager->get('DateTime')); | ||
$this->assertInstanceOf(DateTime::class, $formElementManager->get('Zend\Form\Element\DateTime')); | ||
$this->assertInstanceOf(DateTime::class, $formElementManager->get(ZendDateTime::class)); | ||
} | ||
|
||
public function testHasAPlaceholderAsAttribute() | ||
{ | ||
$formElementManager = $this->serviceManager->get('FormElementManager'); | ||
$element = $formElementManager->get('DateTime'); | ||
|
||
$this->assertEquals('jj.mm.aaaa hh:mm:ss', $element->getAttribute('placeholder')); | ||
} | ||
|
||
public function testFactorySetsAFormatForDateTime() | ||
{ | ||
$formElementManager = $this->serviceManager->get('FormElementManager'); | ||
$element = $formElementManager->get('DateTime'); | ||
|
||
$this->assertEquals('d.m.Y H:i:s', $element->getFormat()); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/MpaCustomDoctrineHydratorTest/Factory/DateTimeToDateTimeFilterFactoryTest.php
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,31 @@ | ||
<?php | ||
/* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
namespace MpaCustomDoctrineHydratorTest\Factory; | ||
|
||
use MpaCustomDoctrineHydrator\Filter\DateTimeToDateTime; | ||
use MpaCustomDoctrineHydratorTest\Util\ServiceManagerFactory; | ||
|
||
class DateTimeToDateTimeFilterFactoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $serviceManager; | ||
|
||
protected function setUp() | ||
{ | ||
\Locale::setDefault('fr-CH'); | ||
$this->serviceManager = ServiceManagerFactory::getServiceManager(); | ||
} | ||
|
||
public function testWeCanGrabTheFilterByItsFactoryShortName() | ||
{ | ||
$filterManager = $this->serviceManager->get('FilterManager'); | ||
$this->assertInstanceOf(DateTimeToDateTime::class, $filterManager->get('DateTimeToDateTime')); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
tests/MpaCustomDoctrineHydratorTest/Filter/DateTimeToDateTimeTest.php
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,62 @@ | ||
<?php | ||
/* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
namespace MpaCustomDoctrineHydratorTest\Filter; | ||
|
||
use MpaCustomDoctrineHydrator\Filter\DateTimeToDateTime; | ||
use MpaCustomDoctrineHydratorTest\Util\ServiceManagerFactory; | ||
|
||
class DateTimeToDateTimeTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $serviceManager; | ||
|
||
protected function setUp() | ||
{ | ||
\Locale::setDefault('fr-CH'); | ||
$this->serviceManager = ServiceManagerFactory::getServiceManager(); | ||
} | ||
|
||
public function testStringWellFormattedDateAndTimeReturnsADateTimeObject() | ||
{ | ||
$serviceConfig = $this->serviceManager | ||
->get('Config')['mpacustomdoctrinehydrator']['formats'][\Locale::getDefault()]; | ||
$filter = new DateTimeToDateTime($serviceConfig); | ||
|
||
$this->assertEquals('DateTime', get_class($filter('10.12.2012 08:05:48'))); | ||
} | ||
|
||
public function testStringWronglyFormattedDateAndTimeReturnsTheSameValue() | ||
{ | ||
$serviceConfig = $this->serviceManager | ||
->get('Config')['mpacustomdoctrinehydrator']['formats'][\Locale::getDefault()]; | ||
$filter = new DateTimeToDateTime($serviceConfig); | ||
|
||
$this->assertEquals('100.102.20102', $filter('100.102.20102')); | ||
} | ||
|
||
public function testCanManuallySetformat() | ||
{ | ||
$filter = new DateTimeToDateTime(); | ||
$filter->setFormat('d/m/Y H:i:s'); | ||
|
||
$this->assertEquals('DateTime', get_class($filter('10/12/2012 07:05:48'))); | ||
} | ||
|
||
public function testFormatAndDatetimeFormatAreValidConfigKeysForFilterOptions() | ||
{ | ||
$filter = new DateTimeToDateTime(); | ||
|
||
$filter->setOptions(['datetime_format' => 'datetime_format']); | ||
$this->assertEquals($filter->getFormat(), 'datetime_format'); | ||
|
||
$filter->setOptions(['format' => 'format']); | ||
$this->assertEquals($filter->getFormat(), 'format'); | ||
} | ||
} |
Oops, something went wrong.