Skip to content

Commit

Permalink
EZP-28216: Change data format to a timestamp for DateFieldType (ezsys…
Browse files Browse the repository at this point in the history
  • Loading branch information
webhdx authored and Łukasz Serwatka committed Nov 17, 2017
1 parent ee625dc commit 6fc18ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
35 changes: 28 additions & 7 deletions lib/FieldType/DataTransformer/DateValueTransformer.php
Expand Up @@ -5,13 +5,13 @@
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/
namespace EzSystems\RepositoryForms\FieldType\DataTransformer;

use DateTime;
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\FieldType\Date\Value;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

/**
* DataTransformer for Date\Value.
Expand All @@ -21,28 +21,49 @@ class DateValueTransformer implements DataTransformerInterface
/**
* @param mixed $value
*
* @return DateTime|null
* @return int|null
*
* @throws TransformationFailedException
*/
public function transform($value)
{
if (null === $value) {
return null;
}

if (!$value instanceof Value) {
throw new TransformationFailedException(
sprintf('Expected a %s, got %s instead', Value::class, gettype($value))
);
}

if (null === $value->date) {
return null;
}

return $value->date;
return $value->date->getTimestamp();
}

/**
* @param mixed $value
* @param int|mixed $value
*
* @return Value|null
*
* @throws InvalidArgumentException
* @throws TransformationFailedException
*/
public function reverseTransform($value)
{
if ($value === null || !$value instanceof DateTime) {
if (empty($value)) {
return null;
}

return new Value($value);
if (!is_numeric($value)) {
throw new TransformationFailedException(
sprintf('Expected a numeric, got %s instead', gettype($value))
);
}

return Value::fromTimestamp($value);
}
}
17 changes: 4 additions & 13 deletions lib/Form/Type/FieldType/DateFieldType.php
Expand Up @@ -7,9 +7,8 @@

use EzSystems\RepositoryForms\FieldType\DataTransformer\DateValueTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Form Type representing ezdate field type.
Expand All @@ -28,20 +27,12 @@ public function getBlockPrefix()

public function getParent()
{
return DateType::class;
return IntegerType::class;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(new DateValueTransformer());
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'input' => 'datetime',
'widget' => 'single_text',
'html5' => false,
]);
$builder
->addModelTransformer(new DateValueTransformer());
}
}

0 comments on commit 6fc18ce

Please sign in to comment.