|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2017 DarkWeb Design |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 13 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 16 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 18 | + * SOFTWARE. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace DarkWebDesign\SymfonyAddon\Transformer; |
| 22 | + |
| 23 | +use Doctrine\Common\Persistence\ObjectManager; |
| 24 | +use Doctrine\Common\Util\ClassUtils; |
| 25 | +use Symfony\Component\Form\DataTransformerInterface; |
| 26 | +use Symfony\Component\Form\Exception\InvalidArgumentException; |
| 27 | +use Symfony\Component\Form\Exception\TransformationFailedException; |
| 28 | + |
| 29 | +/** |
| 30 | + * Transforms between an identifier and a Doctrine entity. |
| 31 | + * |
| 32 | + * @author Raymond Schouten |
| 33 | + * |
| 34 | + * @since 2.3 |
| 35 | + */ |
| 36 | +class EntityToIdentifierTransformer implements DataTransformerInterface |
| 37 | +{ |
| 38 | + /** @var \Doctrine\Common\Persistence\ObjectManager */ |
| 39 | + private $entityManager; |
| 40 | + |
| 41 | + /** @var string */ |
| 42 | + private $className; |
| 43 | + |
| 44 | + /** @var \Doctrine\Common\Persistence\ObjectRepository */ |
| 45 | + private $repository; |
| 46 | + |
| 47 | + /** @var \Doctrine\Common\Persistence\Mapping\ClassMetadata */ |
| 48 | + private $metadata; |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructor. |
| 52 | + * |
| 53 | + * @param \Doctrine\Common\Persistence\ObjectManager $entityManager |
| 54 | + * @param string $className |
| 55 | + * |
| 56 | + * @throws \Symfony\Component\Form\Exception\InvalidArgumentException |
| 57 | + */ |
| 58 | + public function __construct(ObjectManager $entityManager, $className) |
| 59 | + { |
| 60 | + $this->entityManager = $entityManager; |
| 61 | + $this->className = $className; |
| 62 | + $this->repository = $this->entityManager->getRepository($this->className); |
| 63 | + $this->metadata = $this->entityManager->getClassMetadata($this->className); |
| 64 | + |
| 65 | + if ($this->metadata->isIdentifierComposite) { |
| 66 | + throw new InvalidArgumentException('Expected an entity with a single identifier.'); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Transforms a value from the original representation to a transformed representation. |
| 72 | + * |
| 73 | + * @param object $value |
| 74 | + * |
| 75 | + * @return mixed |
| 76 | + * |
| 77 | + * @throws \Symfony\Component\Form\Exception\TransformationFailedException |
| 78 | + */ |
| 79 | + public function transform($value) |
| 80 | + { |
| 81 | + if (null === $value) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + if (!is_object($value) || is_callable($value)) { |
| 86 | + throw new TransformationFailedException('Expected an object.'); |
| 87 | + } |
| 88 | + |
| 89 | + $class = ClassUtils::getClass($value); |
| 90 | + |
| 91 | + if ($class !== $this->className) { |
| 92 | + throw new TransformationFailedException(sprintf('Expected entity %s.', $this->className)); |
| 93 | + } |
| 94 | + |
| 95 | + $identifierValues = $this->metadata->getIdentifierValues($value); |
| 96 | + |
| 97 | + return reset($identifierValues); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Transforms a value from the transformed representation to its original representation. |
| 102 | + * |
| 103 | + * @param mixed $value |
| 104 | + * |
| 105 | + * @return object |
| 106 | + * |
| 107 | + * @throws \Symfony\Component\Form\Exception\TransformationFailedException |
| 108 | + */ |
| 109 | + public function reverseTransform($value) |
| 110 | + { |
| 111 | + if (null === $value || '' === $value) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + if (!is_scalar($value)) { |
| 116 | + throw new TransformationFailedException('Expected a scalar.'); |
| 117 | + } |
| 118 | + |
| 119 | + $entity = $this->repository->find($value); |
| 120 | + |
| 121 | + if (null === $entity) { |
| 122 | + throw new TransformationFailedException( |
| 123 | + sprintf('Entity %s with identifier "%s" not found.', $this->className, $value) |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + return $entity; |
| 128 | + } |
| 129 | +} |
0 commit comments