Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace SlashCreation\Bundles\Compta\Form\DataTransformer;
- use Doctrine\Common\Persistence\ObjectManager;
- use Symfony\Component\Form\DataTransformerInterface;
- use Symfony\Component\Form\Exception\TransformationFailedException;
- /**
- * Transformer l'entité Client pour obtenir son identité(string)
- * A partir de son identité(string) retrouver l'entité Client
- */
- class identityClientTransformer implements DataTransformerInterface {
- private $manager;
- public function __construct(ObjectManager $manager) {
- $this->manager = $manager;
- }
- /**
- * Transforme l'entité client en retournant son identité
- * Return String
- */
- public function transform($entity) {
- if (null === $entity) {
- return '';
- }
- return $entity->getIdentity();
- }
- /**
- * La valeur de l'input du formulaire(string) est utilisé pour retrouver l'entité.
- * Return Entity
- */
- public function reverseTransform($value) {
- if (!$value) {
- return;
- }
- // Recherche du numéro de référence du client contenu dans l'input, le numéro se trouvant entre les ( #number )
- $output = array();
- preg_match('/\((.*?)\)/', $value, $output);
- $entity = $this->manager
- ->getRepository('Compta:Client')
- ->findOneBy(array('ref' => $output[1]))
- ;
- if (null === $entity) {
- throw new TransformationFailedException(sprintf(
- 'Le numéro de référence "%s" n\'existe pas!', $output
- ));
- }
- return $entity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement