Skip to content

Commit

Permalink
Add datetime support
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalourdio committed May 20, 2014
1 parent 7726def commit 8484dc8
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 3 deletions.
11 changes: 8 additions & 3 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use MpaCustomDoctrineHydrator\Factory\AnnotationBuilderFactory;
use MpaCustomDoctrineHydrator\Factory\DateElementFactory;
use MpaCustomDoctrineHydrator\Factory\DateTimeElementFactory;
use MpaCustomDoctrineHydrator\Factory\DateTimeToDateTimeFilterFactory;
use MpaCustomDoctrineHydrator\Factory\DateToDateTimeFilterFactory;
use MpaCustomDoctrineHydrator\Factory\EntityAttacherFactory;

Expand All @@ -22,15 +24,18 @@
],
'filters' => [
'factories' => [
'DateToDateTime' => DateToDateTimeFilterFactory::class,
'DateToDateTime' => DateToDateTimeFilterFactory::class,
'DateTimeToDateTime' => DateTimeToDateTimeFilterFactory::class,
],
],
'form_elements' => [
'factories' => [
'Date' => DateElementFactory::class,
'Date' => DateElementFactory::class,
'DateTime' => DateTimeElementFactory::class,
],
'aliases' => [
'Zend\Form\Element\Date' => 'Date',
'Zend\Form\Element\Date' => 'Date',
'Zend\Form\Element\DateTime' => 'DateTime',
]
],
];
37 changes: 37 additions & 0 deletions src/MpaCustomDoctrineHydrator/Factory/DateTimeElementFactory.php
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;
}
}
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 src/MpaCustomDoctrineHydrator/Filter/DateTimeToDateTime.php
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;
}
}
59 changes: 59 additions & 0 deletions src/MpaCustomDoctrineHydrator/Form/Element/DateTime.php
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()
],
];
}
}
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());
}
}
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'));
}
}
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');
}
}
Loading

0 comments on commit 8484dc8

Please sign in to comment.