From 36c3e33cfa16316587db81ec8894dbd254192e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 17 Apr 2018 11:32:39 +0200 Subject: [PATCH] add DateIntervalType --- .../DateIntervalTransformer.php | 92 +++++++++++++++++++ Form/Type/DateIntervalType.php | 57 ++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 Form/Type/DataTransformer/DateIntervalTransformer.php create mode 100644 Form/Type/DateIntervalType.php diff --git a/Form/Type/DataTransformer/DateIntervalTransformer.php b/Form/Type/DataTransformer/DateIntervalTransformer.php new file mode 100644 index 000000000..29a81447c --- /dev/null +++ b/Form/Type/DataTransformer/DateIntervalTransformer.php @@ -0,0 +1,92 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +namespace Chill\MainBundle\Form\Type\DataTransformer; + +use Symfony\Component\Form\DataTransformerInterface; +use Symfony\Component\Form\Exception\TransformationFailedException; +use Symfony\Component\Form\Exception\UnexpectedTypeException; + +/** + * + * + * @author Julien Fastré + */ +class DateIntervalTransformer implements DataTransformerInterface +{ + /** + * + * @param \DateInterval $value + * @throws UnexpectedTypeException + */ + public function transform($value) + { + if (empty($value)) { + return null; + } + + if (!$value instanceof \DateInterval) { + throw new UnexpectedTypeException($value, \DateInterval::class); + } + + if ($value->d > 0) { + // we check for weeks (weeks are converted to 7 days) + if ($value->d % 7 === 0) { + return [ + 'n' => $value->d / 7, + 'unit' => 'W' + ]; + } else { + return [ + 'n' => $value->d, + 'unit' => 'D' + ]; + } + } elseif ($value->m > 0) { + return [ + 'n' => $value->m, + 'unit' => 'M' + ]; + } elseif ($value->y > 0) { + return [ + 'n' => $value->y, + 'unit' => 'Y' + ]; + } + + throw new TransformationFailedException('the date interval does not ' + . 'contains any days, months or years'); + } + + public function reverseTransform($value) + { + if (empty($value) or empty($value['n'])) { + return null; + } + + $string = 'P'.$value['n'].$value['unit']; + + try { + return new \DateInterval($string); + } catch (\Exception $e) { + throw new TransformationFailedException("Could not transform value " + . "into DateInterval", 1542, $e); + } + + + } +} diff --git a/Form/Type/DateIntervalType.php b/Form/Type/DateIntervalType.php new file mode 100644 index 000000000..499c61f93 --- /dev/null +++ b/Form/Type/DateIntervalType.php @@ -0,0 +1,57 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +namespace Chill\MainBundle\Form\Type; + +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\Extension\Core\Type\IntegerType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Chill\MainBundle\Form\Type\DataTransformer\DateIntervalTransformer; +use Symfony\Component\Validator\Constraints\GreaterThan; + +/** + * + * + */ +class DateIntervalType extends AbstractType +{ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('n', IntegerType::class, [ + 'scale' => 0, + 'constraints' => [ + new GreaterThan([ + 'value' => 0 + ]) + ] + ]) + ->add('unit', ChoiceType::class, [ + 'choices' => [ + 'Days' => 'D', + 'Weeks' => 'W', + 'Months' => 'M', + 'Years' => 'Y' + ], + 'choices_as_values' => true + ]) + ; + + $builder->addModelTransformer(new DateIntervalTransformer()); + } +}