]> Raphaël G. Git Repositories - airbundle/commitdiff
Add dance and user collection transformer
authorRaphaël Gertz <git@rapsys.eu>
Wed, 21 Feb 2024 11:12:40 +0000 (12:12 +0100)
committerRaphaël Gertz <git@rapsys.eu>
Wed, 21 Feb 2024 11:12:40 +0000 (12:12 +0100)
Transformer/DanceTransformer.php [new file with mode: 0644]
Transformer/SubscriptionTransformer.php [new file with mode: 0644]

diff --git a/Transformer/DanceTransformer.php b/Transformer/DanceTransformer.php
new file mode 100644 (file)
index 0000000..16673c4
--- /dev/null
@@ -0,0 +1,87 @@
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys AirBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Rapsys\AirBundle\Transformer;
+
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
+use Doctrine\ORM\EntityManagerInterface;
+
+use Symfony\Component\Form\DataTransformerInterface;
+use Symfony\Component\Form\Exception\TransformationFailedException;
+
+use Rapsys\AirBundle\Entity\Dance;
+
+/**
+ * {@inheritdoc}
+ */
+class DanceTransformer implements DataTransformerInterface {
+       /**
+        * Public constructor
+        *
+        * @param EntityManagerInterface $manager The entity manager
+        */
+       public function __construct(private EntityManagerInterface $manager) {
+       }
+
+       /**
+        * Transforms a dance object array or collection to an int array
+        *
+        * @param Dance $dances The dance instances array
+        * @return array The dance ids
+        */
+       public function transform($dances) {
+               //Without dances
+               if (null === $dances) {
+                       return [];
+               }
+
+               //With collection instance
+               if ($dances instanceof Collection) {
+                       $dances = $dances->toArray();
+               }
+
+               //Return dance ids
+               return array_map(function ($d) { return $d->getId(); }, $dances);
+       }
+
+       /**
+        * Transforms an int array to a dance object collection
+        *
+        * @param array $ids
+        * @throws TransformationFailedException when object (dance) is not found
+        * @return array The dance instances array
+        */
+       public function reverseTransform($ids) {
+               //Without ids
+               if ('' === $ids || null === $ids) {
+                       $ids = [];
+                       //With ids
+               } else {
+                       $ids = (array) $ids;
+               }
+
+               //Iterate on ids
+               foreach($ids as $k => $id) {
+                       //Replace with dance instance
+                       $ids[$k] = $this->manager->getRepository(Dance::class)->findOneById($id);
+
+                       //Without dance
+                       if (null === $ids[$k]) {
+                               //Throw exception
+                               throw new TransformationFailedException(sprintf('Dance with id "%d" does not exist!', $id));
+                       }
+               }
+
+               //Return collection
+               return new ArrayCollection($ids);
+       }
+}
diff --git a/Transformer/SubscriptionTransformer.php b/Transformer/SubscriptionTransformer.php
new file mode 100644 (file)
index 0000000..c507063
--- /dev/null
@@ -0,0 +1,87 @@
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys AirBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Rapsys\AirBundle\Transformer;
+
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
+use Doctrine\ORM\EntityManagerInterface;
+
+use Symfony\Component\Form\DataTransformerInterface;
+use Symfony\Component\Form\Exception\TransformationFailedException;
+
+use Rapsys\AirBundle\Entity\User;
+
+/**
+ * {@inheritdoc}
+ */
+class SubscriptionTransformer implements DataTransformerInterface {
+       /**
+        * Public constructor
+        *
+        * @param EntityManagerInterface $manager The entity manager
+        */
+       public function __construct(private EntityManagerInterface $manager) {
+       }
+
+       /**
+        * Transforms a subscription object array or collection to an int array
+        *
+        * @param Subscription $subscriptions The subscription instances array
+        * @return array The subscription ids
+        */
+       public function transform($subscriptions) {
+               //Without subscriptions
+               if (null === $subscriptions) {
+                       return [];
+               }
+
+               //With collection instance
+               if ($subscriptions instanceof Collection) {
+                       $subscriptions = $subscriptions->toArray();
+               }
+
+               //Return subscription ids
+               return array_map(function ($d) { return $d->getId(); }, $subscriptions);
+       }
+
+       /**
+        * Transforms an int array to a subscription object collection
+        *
+        * @param array $ids
+        * @throws TransformationFailedException when object (subscription) is not found
+        * @return array The subscription instances array
+        */
+       public function reverseTransform($ids) {
+               //Without ids
+               if ('' === $ids || null === $ids) {
+                       $ids = [];
+                       //With ids
+               } else {
+                       $ids = (array) $ids;
+               }
+
+               //Iterate on ids
+               foreach($ids as $k => $id) {
+                       //Replace with subscription instance
+                       $ids[$k] = $this->manager->getRepository(User::class)->findOneById($id);
+
+                       //Without subscription
+                       if (null === $ids[$k]) {
+                               //Throw exception
+                               throw new TransformationFailedException(sprintf('User with id "%d" does not exist!', $id));
+                       }
+               }
+
+               //Return collection
+               return new ArrayCollection($ids);
+       }
+}