This commit is contained in:
Julien Fastré 2022-01-04 11:09:59 +01:00
parent 45dd21e02a
commit 0edd5667e0
3 changed files with 49 additions and 36 deletions

View File

@ -12,10 +12,8 @@ declare(strict_types=1);
namespace Chill\MainBundle\Form; namespace Chill\MainBundle\Form;
use Chill\MainBundle\Entity\Notification; use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\ChillTextareaType; use Chill\MainBundle\Form\Type\ChillTextareaType;
use Chill\MainBundle\Form\Type\PickUserDynamicType; use Chill\MainBundle\Form\Type\PickUserDynamicType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;

View File

@ -1,5 +1,14 @@
<?php <?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Form\Type\DataTransformer; namespace Chill\MainBundle\Form\Type\DataTransformer;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
@ -8,15 +17,16 @@ use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
use function array_key_exists;
class UserToJsonTransformer implements DataTransformerInterface class UserToJsonTransformer implements DataTransformerInterface
{ {
private SerializerInterface $serializer;
private DenormalizerInterface $denormalizer; private DenormalizerInterface $denormalizer;
private bool $multiple; private bool $multiple;
private SerializerInterface $serializer;
public function __construct(DenormalizerInterface $denormalizer, SerializerInterface $serializer, bool $multiple) public function __construct(DenormalizerInterface $denormalizer, SerializerInterface $serializer, bool $multiple)
{ {
$this->denormalizer = $denormalizer; $this->denormalizer = $denormalizer;
@ -24,25 +34,11 @@ class UserToJsonTransformer implements DataTransformerInterface
$this->multiple = $multiple; $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) public function reverseTransform($value)
{ {
if ($this->multiple) { if ($this->multiple) {
return \array_map( return array_map(
function($item) { return $this->denormalizeOne($item);}, function ($item) { return $this->denormalizeOne($item); },
json_decode($value, true) json_decode($value, true)
); );
} }
@ -50,11 +46,26 @@ class UserToJsonTransformer implements DataTransformerInterface
return $this->denormalizeOne(json_decode($value, true)); 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 private function denormalizeOne(array $item): User
{ {
if (!array_key_exists('type', $item)) { if (!array_key_exists('type', $item)) {
throw new TransformationFailedException('the key "type" is missing on element'); throw new TransformationFailedException('the key "type" is missing on element');
} }
if (!array_key_exists('id', $item)) { if (!array_key_exists('id', $item)) {
throw new TransformationFailedException('the key "id" is missing on element'); throw new TransformationFailedException('the key "id" is missing on element');
} }

View File

@ -1,29 +1,35 @@
<?php <?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Form\Type; namespace Chill\MainBundle\Form\Type;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\DataTransformer\UserToJsonTransformer; use Chill\MainBundle\Form\Type\DataTransformer\UserToJsonTransformer;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
/** /**
* Pick user dymically, using vuejs module "AddPerson" * Pick user dymically, using vuejs module "AddPerson".
*/ */
class PickUserDynamicType extends AbstractType class PickUserDynamicType extends AbstractType
{ {
private SerializerInterface $serializer;
private DenormalizerInterface $denormalizer; private DenormalizerInterface $denormalizer;
private SerializerInterface $serializer;
public function __construct(DenormalizerInterface $denormalizer, SerializerInterface $serializer) public function __construct(DenormalizerInterface $denormalizer, SerializerInterface $serializer)
{ {
$this->denormalizer = $denormalizer; $this->denormalizer = $denormalizer;
@ -35,15 +41,6 @@ class PickUserDynamicType extends AbstractType
$builder->addViewTransformer(new UserToJsonTransformer($this->denormalizer, $this->serializer, $options['multiple'])); $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) public function buildView(FormView $view, FormInterface $form, array $options)
{ {
$view->vars['multiple'] = $options['multiple']; $view->vars['multiple'] = $options['multiple'];
@ -51,9 +48,16 @@ class PickUserDynamicType extends AbstractType
$view->vars['uniqid'] = uniqid('pick_user_dyn'); $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() public function getBlockPrefix()
{ {
return 'pick_user_dynamic'; return 'pick_user_dynamic';
} }
} }