Source of file CollectionToArrayTransformer.php
Size: 1,641 Bytes - Last Modified: 2013-07-17T08:22:21+02:00
/home/theseer/Downloads/Symfony/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/DataTransformer/CollectionToArrayTransformer.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 | <?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Bridge\Doctrine\Form\DataTransformer; use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\DataTransformerInterface; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; /** * @author Bernhard Schussek <bschussek@gmail.com> */ class CollectionToArrayTransformer implements DataTransformerInterface { /** * Transforms a collection into an array. * * @param Collection $collection A collection of entities * * @return mixed An array of entities * * @throws TransformationFailedException */ public function transform($collection) { if (null === $collection) { return array(); } if (!$collection instanceof Collection) { throw new TransformationFailedException('Expected a Doctrine\Common\Collections\Collection object.'); } return $collection->toArray(); } /** * Transforms choice keys into entities. * * @param mixed $array An array of entities * * @return Collection A collection of entities */ public function reverseTransform($array) { if ('' === $array || null === $array) { $array = array(); } else { $array = (array) $array; } return new ArrayCollection($array); } } |