1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys AirBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\AirBundle\Transformer
; 
  14 use Doctrine\Common\Collections\ArrayCollection
; 
  15 use Doctrine\Common\Collections\Collection
; 
  16 use Doctrine\ORM\EntityManagerInterface
; 
  18 use Symfony\Component\Form\DataTransformerInterface
; 
  19 use Symfony\Component\Form\Exception\TransformationFailedException
; 
  21 use Rapsys\AirBundle\Entity\Dance
; 
  26 class DanceTransformer 
implements DataTransformerInterface 
{ 
  30          * @param EntityManagerInterface $manager The entity manager 
  32         public function __construct(private EntityManagerInterface 
$manager) { 
  36          * Transforms a dance object array or collection to an int array 
  38          * @param Dance $dances The dance instances array 
  39          * @return array The dance ids 
  41         public function transform($dances) { 
  43                 if (null === $dances) { 
  47                 //With collection instance 
  48                 if ($dances instanceof Collection
) { 
  49                         $dances = $dances->toArray(); 
  53                 return array_map(function ($d) { return $d
->getId(); }, $dances); 
  57          * Transforms an int array to a dance object collection 
  60          * @throws TransformationFailedException when object (dance) is not found 
  61          * @return array The dance instances array 
  63         public function reverseTransform($ids) { 
  65                 if ('' === $ids || null === $ids) { 
  73                 foreach($ids as $k => $id) { 
  74                         //Replace with dance instance 
  75                         $ids[$k] = $this->manager
->getRepository(Dance
::class)->findOneById($id); 
  78                         if (null === $ids[$k]) { 
  80                                 throw new TransformationFailedException(sprintf('Dance with id "%d" does not exist!', $id)); 
  85                 return new ArrayCollection($ids);