From: Raphaël Gertz Date: Wed, 21 Feb 2024 11:12:40 +0000 (+0100) Subject: Add dance and user collection transformer X-Git-Tag: 0.3.0~84 X-Git-Url: https://git.rapsys.eu/airbundle/commitdiff_plain/4d24b7426da325a32cdc5bd54714d16bfba7c378 Add dance and user collection transformer --- diff --git a/Transformer/DanceTransformer.php b/Transformer/DanceTransformer.php new file mode 100644 index 0000000..16673c4 --- /dev/null +++ b/Transformer/DanceTransformer.php @@ -0,0 +1,87 @@ + + * + * 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 index 0000000..c507063 --- /dev/null +++ b/Transformer/SubscriptionTransformer.php @@ -0,0 +1,87 @@ + + * + * 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); + } +}