Created
May 20, 2015 08:23
-
-
Save nischalsource/854e7a5fd821aad16e5a to your computer and use it in GitHub Desktop.
ObjectToArrayTransformer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CampaignTool\CampaignToolBundle\Form\DataTransformer; | |
use Symfony\Component\Form\DataTransformerInterface; | |
use Symfony\Component\Form\Exception\TransformationFailedException; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use CampaignTool\CampaignToolBundle\Entity\Telephone; | |
class ObjectToArrayTransformer implements DataTransformerInterface | |
{ | |
/** | |
* @var ObjectManager | |
*/ | |
private $om; | |
/** | |
* @param ObjectManager $om | |
*/ | |
public function __construct(ObjectManager $om) | |
{ | |
$this->om = $om; | |
} | |
/** | |
* Transforms an object (telephoneNumbers) to a string (number). | |
* | |
* @param Issue|null $telephoneNumbers | |
* @return string | |
*/ | |
public function transform($telephoneNumbers) | |
{ | |
if (null === $telephoneNumbers) { | |
return ""; | |
} | |
foreach($telephoneNumbers as $telephoneNumber) | |
{ | |
$numbersArray[] = $telephoneNumber->getTelephoneNumber(); | |
} | |
return $numbersArray; | |
} | |
/** | |
* Transforms a string (number) to an object (telephoneNumbers). | |
* | |
* @param string $number | |
* | |
* @return Issue|null | |
* | |
* @throws TransformationFailedException if object (telephoneNumbers) is not found. | |
*/ | |
public function reverseTransform($number) | |
{ | |
if (!$number) { | |
return null; | |
} | |
foreach($number as $num) | |
{ | |
$telephoneId = $num->getId(); | |
$telephoneNumbers[] = $this->om->getRepository('CampaignToolBundle:Telephone')->findOneBy(array('Id' => $telephoneId)); | |
} | |
if (null === $telephoneNumbers) { | |
throw new TransformationFailedException(sprintf( | |
'An telephoneNumbers with number "%s" does not exist!', | |
$number | |
)); | |
} | |
return $telephoneNumbers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment