Created
March 30, 2015 17:29
-
-
Save TomAdam/ec6e39998653d043adb6 to your computer and use it in GitHub Desktop.
Simple Entity Form Type
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 | |
use Symfony\Component\Form\DataTransformerInterface; | |
use Symfony\Component\Form\Exception\TransformationFailedException; | |
class IdentityToEntityTransformer implements DataTransformerInterface | |
{ | |
private $repository; | |
private $repositoryMethod; | |
private $identifierMethod; | |
/** | |
* @param object $repository | |
* @param string $repositoryMethod | |
* @param string $identifierMethod | |
*/ | |
public function __construct($repository, $repositoryMethod, $identifierMethod) | |
{ | |
$this->repository = $repository; | |
$this->repositoryMethod = $repositoryMethod; | |
$this->identifierMethod = $identifierMethod; | |
} | |
/** | |
* @param object $entity | |
* @return string | |
*/ | |
public function transform($entity) | |
{ | |
if (!$entity) { | |
return; | |
} | |
return (string) call_user_func([$entity, $this->identifierMethod]); | |
} | |
/** | |
* @param mixed $id | |
* @return null|object|void | |
*/ | |
public function reverseTransform($id) | |
{ | |
if (null !== $id && !is_scalar($id)) { | |
throw new TransformationFailedException('Expected a scalar.'); | |
} | |
if ('' === $id || null === $id) { | |
return; | |
} | |
$entity = call_user_func([$this->repository, $this->repositoryMethod], $id); | |
if (!$entity) { | |
throw new TransformationFailedException(sprintf('The entity identified by "%s" does not exist', $id)); | |
} | |
return $entity; | |
} | |
} |
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 | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class SimpleEntityType extends AbstractType | |
{ | |
/** | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder->addViewTransformer(new IdentityToEntityTransformer( | |
$options['repository'], | |
$options['repositoryMethod'], | |
$options['identifierMethod'] | |
)); | |
} | |
/** | |
* @param OptionsResolverInterface $resolver | |
*/ | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults([ | |
'compound' => false, | |
'repository' => null, | |
'identifierMethod' => 'getId', | |
'repositoryMethod' => 'get' | |
]); | |
$resolver->setRequired(['repository']); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return 'simple_entity'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment