1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- *
- */
- namespace Sonata\AdminBundle\Form\DataTransformer;
- use Symfony\Component\Form\Exception\InvalidPropertyException;
- use Symfony\Component\Form\Exception\PropertyAccessDeniedException;
- use Symfony\Component\Form\DataTransformerInterface;
- use Symfony\Component\Form\Exception\UnexpectedTypeException;
- use Symfony\Component\Form\Exception\TransformationFailedException;
- use Sonata\AdminBundle\Model\ModelManagerInterface;
- /**
- *
- * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
- */
- class ModelToIdTransformer implements DataTransformerInterface
- {
- protected $modelManager;
- protected $className;
- /**
- * @param \Sonata\AdminBundle\Model\ModelManagerInterface $modelManager
- * @param string $className
- */
- public function __construct(ModelManagerInterface $modelManager, $className)
- {
- $this->modelManager = $modelManager;
- $this->className = $className;
- }
- /**
- * {@inheritDoc}
- */
- public function reverseTransform($newId)
- {
- if (empty($newId)) {
- return null;
- }
- return $this->modelManager->find($this->className, $newId);
- }
- /**
- * {@inheritDoc}
- */
- public function transform($entity)
- {
- if (empty($entity)) {
- return 0;
- }
- return current( $this->modelManager->getIdentifierValues($entity) );
- }
- }
|