Created
October 25, 2014 11:10
-
-
Save polc/b73e19cce1a2a6963f95 to your computer and use it in GitHub Desktop.
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 AppBundle\Form\DataTransformer; | |
use Symfony\Component\Form\DataTransformerInterface; | |
use Symfony\Component\Form\Exception\TransformationFailedException; | |
use AppBundle\Manager\CategoryManager; | |
use AppBundle\Entity\Category; | |
class CategoryToStringTransformer implements DataTransformerInterface | |
{ | |
private $categoryManager; | |
public function __construct(CategoryManager $categoryManager) | |
{ | |
$this->categoryManager = $categoryManager; | |
} | |
/** | |
* Transforms a Category to a string. | |
* | |
* @param Category|null $user | |
* @return string | |
*/ | |
public function transform(Category $category = null) | |
{ | |
if (null === $category) { | |
return ""; | |
} | |
return $category->getName(); | |
} | |
/** | |
* Transforms a string to a Category. | |
* | |
* @param string $email | |
* @return Category|null | |
* @throws TransformationFailedException if User is not found. | |
*/ | |
public function reverseTransform($name) | |
{ | |
if (!$name) { | |
return null; | |
} | |
$cateogry = $this->categoryManager->findOneByName($name); | |
if (null === $category) { | |
$category = new Category(); | |
$category->setName($name) | |
} | |
return $category; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment