<?php namespace App\CommonBundle\Form\Type; use App\CommonBundle\Form\DataTransformer\EntityDataTransformer; use Doctrine\ORM\EntityManager; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class EntityType extends AbstractType { private $em; public function __construct(EntityManager $em) { $this->em = $em; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults([ 'field' => 'id', 'class' => null, 'compound' => false ]); $resolver->setRequired([ 'class', ]); } public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addModelTransformer(new EntityDataTransformer($this->em, $options['class'], $options['field'])); } public function getName() { return 'entity'; } }
<?php namespace App\CommonBundle\Form\DataTransformer; use Doctrine\ORM\EntityManager; use Symfony\Component\Form\DataTransformerInterface; class EntityDataTransformer implements DataTransformerInterface { private $em; private $entityName; private $fieldName; public function __construct(EntityManager $em, $entityName, $fieldName) { $this->em = $em; $this->entityName = $entityName; $this->fieldName = $fieldName; } public function transform($value) { return null; } public function reverseTransform($value) { if (!$value) { return null; } return $this->em->getRepository($this->entityName)->findOneBy([$this->fieldName => $value]); } }
common.form.type.entity: class: App\CommonBundle\Form\Type\EntityType arguments: [@doctrine.orm.entity_manager] tags: - { name: form.type, alias: entity }
<?php namespace App\CommonBundle\Form\Type; use App\CommonBundle\Form\DataTransformer\BooleanDataTransformer; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; class BooleanType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addViewTransformer(new BooleanDataTransformer()); } public function getParent() { return 'text'; } public function getName() { return 'boolean'; } }
<?php namespace App\CommonBundle\Form\DataTransformer; use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\Exception\TransformationFailedException; class BooleanDataTransformer implements DataTransformerInterface { public function transform($value) { return null; } public function reverseTransform($value) { if ($value === "false" || $value === "0" || $value === "" || $value === 0) { return false; } return true; } }
common.form.type.boolean: class: App\CommonBundle\Form\Type\BooleanType tags: - { name: form.type, alias: boolean }
<?php use JMS\Serializer\Annotation as Serialization; class News { /** * @Serialization\Groups({"details", "list"}) */ protected $id; /** * @Serialization\Groups({"details", "list"}) */ protected $title; /** * @Serialization\Groups({"details", "list"}) */ protected $text; /** * User * * @Serialization\Groups({"details", "list"}) */ protected $author; }
<?php use JMS\Serializer\Annotation as Serialization; class User { /** * @Serialization\Groups({"details", "list"}) */ protected $id; /** * @Serialization\Groups({"details", "list"}) */ protected $name; /** list details */ }
<?php class NewsController extends BaseController { /** * @SerializationGroups({"details"}) * @Route("/news/{id}", requirements={"id": "\d+"}) */ public function detailsAction(Common\Entity\News $entity) { return $entity; } }
<?php use JMS\Serializer\Annotation as Serialization; class News { /** * @Serialization\Groups({"news_details", "news_list"}) */ protected $id; /** * @Serialization\Groups({"news_details", "news_list"}) */ protected $title; /** * @Serialization\Groups({"news_details", "news_list"}) */ protected $text; /** * User * * @Serialization\Groups({"news_details", "news_list"}) */ protected $author; }
<?php use JMS\Serializer\Annotation as Serialization; class User { /** * @Serialization\Groups({"user_details", "user_list", "user_embed"}) */ protected $id; /** * @Serialization\Groups({"user_details", "user_list", "user_embed"}) */ protected $name; /** , user_list user_details */ }
<?php class NewsController extends BaseController { /** * @SerializationGroups({"news_details", "user_embed"}) * @Route("/news/{id}", requirements={"id": "\d+"}) */ public function detailsAction(Common\Entity\News $entity) { return $entity; } }
Source: https://habr.com/ru/post/257991/
All Articles