diff --git a/src/Bundle/ChillMainBundle/Form/NotificationType.php b/src/Bundle/ChillMainBundle/Form/NotificationType.php index fa06007a2..09d6a0341 100644 --- a/src/Bundle/ChillMainBundle/Form/NotificationType.php +++ b/src/Bundle/ChillMainBundle/Form/NotificationType.php @@ -12,10 +12,8 @@ declare(strict_types=1); namespace Chill\MainBundle\Form; use Chill\MainBundle\Entity\Notification; -use Chill\MainBundle\Entity\User; use Chill\MainBundle\Form\Type\ChillTextareaType; use Chill\MainBundle\Form\Type\PickUserDynamicType; -use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; diff --git a/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/UserToJsonTransformer.php b/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/UserToJsonTransformer.php index 497cbcdd9..df670f891 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/UserToJsonTransformer.php +++ b/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/UserToJsonTransformer.php @@ -1,5 +1,14 @@ denormalizer = $denormalizer; @@ -24,25 +34,11 @@ class UserToJsonTransformer implements DataTransformerInterface $this->multiple = $multiple; } - /** - * @param User|User[] $value - */ - public function transform($value): string - { - if (null === $value) { - return $this->multiple ? "null" : "[]"; - } - - return $this->serializer->serialize($value, 'json', [ - AbstractNormalizer::GROUPS => ['read'], - ]); - } - public function reverseTransform($value) { if ($this->multiple) { - return \array_map( - function($item) { return $this->denormalizeOne($item);}, + return array_map( + function ($item) { return $this->denormalizeOne($item); }, json_decode($value, true) ); } @@ -50,11 +46,26 @@ class UserToJsonTransformer implements DataTransformerInterface return $this->denormalizeOne(json_decode($value, true)); } + /** + * @param User|User[] $value + */ + public function transform($value): string + { + if (null === $value) { + return $this->multiple ? 'null' : '[]'; + } + + return $this->serializer->serialize($value, 'json', [ + AbstractNormalizer::GROUPS => ['read'], + ]); + } + private function denormalizeOne(array $item): User { if (!array_key_exists('type', $item)) { throw new TransformationFailedException('the key "type" is missing on element'); } + if (!array_key_exists('id', $item)) { throw new TransformationFailedException('the key "id" is missing on element'); } diff --git a/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php b/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php index 0ddff7b1e..52798608e 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php @@ -1,29 +1,35 @@ denormalizer = $denormalizer; @@ -35,15 +41,6 @@ class PickUserDynamicType extends AbstractType $builder->addViewTransformer(new UserToJsonTransformer($this->denormalizer, $this->serializer, $options['multiple'])); } - public function configureOptions(OptionsResolver $resolver) - { - $resolver - ->setDefault('multiple', false) - ->setAllowedTypes('multiple', ['bool']) - ->setDefault('compound', false) - ; - } - public function buildView(FormView $view, FormInterface $form, array $options) { $view->vars['multiple'] = $options['multiple']; @@ -51,9 +48,16 @@ class PickUserDynamicType extends AbstractType $view->vars['uniqid'] = uniqid('pick_user_dyn'); } + public function configureOptions(OptionsResolver $resolver) + { + $resolver + ->setDefault('multiple', false) + ->setAllowedTypes('multiple', ['bool']) + ->setDefault('compound', false); + } + public function getBlockPrefix() { return 'pick_user_dynamic'; } - }