Advertisement
r1pp3rj4ck

Untitled

Feb 29th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Velvel\ZDFBundle\Form\DataTransformer;
  4.  
  5. use Symfony\Component\Form\Exception\TransformationFailedException;
  6. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  7. use Symfony\Component\Form\DataTransformerInterface;
  8. use Doctrine\Common\Persistence\ObjectManager;
  9.  
  10. /**
  11.  * Country to Distributed Video Country transformer
  12.  */
  13. class CountryToDVCTransformer implements DataTransformerInterface
  14. {
  15.     private $om;
  16.  
  17.     /**
  18.      * Constructor
  19.      *
  20.      * @param \Doctrine\Common\Persistence\ObjectManager $om Entity Manager
  21.      *
  22.      * @author r1pp3rj4ck <attila.bukor@velvel.hu>
  23.      */
  24.     public function __construct(ObjectManager $om)
  25.     {
  26.         $this->om = $om;
  27.     }
  28.  
  29.     /**
  30.      * Transforms DVC to Country array
  31.      *
  32.      * @param array $value DVC array
  33.      *
  34.      * @author   r1pp3rj4ck <attila.bukor@velvel.hu>
  35.      * @return array
  36.      */
  37.     public function reverseTransform($value)
  38.     {
  39.         var_dump('reverseTransform value:');
  40.         var_dump($value);
  41.         if (!$value)
  42.         {
  43.             return array();
  44.         }
  45.         if (!is_array($value))
  46.         {
  47.             throw new UnexpectedTypeException($value, 'array');
  48.         }
  49.  
  50.         $array = array();
  51.         foreach ($value as $val)
  52.         {
  53.             $array[] = is_object($val) ? $val->getCountry() : $val;
  54.         }
  55.         var_dump('reverseTransform final:');
  56.         var_dump($array);
  57.  
  58.         return $array;
  59.     }
  60.  
  61.     /**
  62.      * Transforms countries to DVC array
  63.      *
  64.      * @param array $value Country array
  65.      *
  66.      * @author r1pp3rj4ck <attila.bukor@velvel.hu>
  67.      * @return array
  68.      */
  69.     public function transform($value)
  70.     {
  71.         var_dump('transform value:');
  72.         var_dump($value);
  73.         if (!$value)
  74.         {
  75.             return array();
  76.         }
  77.         if (!is_array($value))
  78.         {
  79.             throw new UnexpectedTypeException($value, 'array');
  80.         }
  81.  
  82.         $array = new \Doctrine\Common\Collections\ArrayCollection();
  83.         foreach ($value as $v)
  84.         {
  85.             $country = $this->om->find('VelvelZDFBundle:Country', $v);
  86.             if (!$country)
  87.             {
  88.                 throw new TransformationFailedException;
  89.             }
  90.             $a = new \Velvel\ZDFBundle\Entity\DisabledVideoCountry();
  91.             $a->setCountry($country);
  92.             $array[] = $a;
  93.         }
  94.  
  95.         var_dump('transform final:');
  96.         var_dump($array);
  97. //        die();
  98.         return $array;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement