Created
February 5, 2018 18:14
-
-
Save HeahDude/b04d4c386e9c5c684697796b817c259d to your computer and use it in GitHub Desktop.
Strict ChoicesToValuesTransformer
This file contains hidden or 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 App\Form\TypeExtension; | |
use App\Form\DataTransformer\StrictChoicesToValuesTransformer; | |
use Symfony\Component\Form\AbstractTypeExtension; | |
use Symfony\Compoennt\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer; | |
use Symfony\Compoennt\Form\Extension\Core\Type\ChoiceType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class ChoiceTypeExtension extends AbstractTypeExtension | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getParent() | |
{ | |
return ChoiceType::class; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefault('strict', false); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options = []) | |
{ | |
if (!$options['strict'] || !$options['multiple']) { | |
return; | |
} | |
$transformers = []; | |
foreach($builder->getViewTransformers() as $transformer) { | |
if ($transformer instanceof ChoicesToValuesTransformer) { | |
$transformer = new StrictChoicesToValuesTransformer($transformer); | |
} | |
$transformers[] = $transformer; | |
} | |
$builder->resetViewTransformers(); | |
foreach($transformers as $transformer) { | |
$builder->addViewTransformer($transformer); | |
} | |
} | |
} |
This file contains hidden or 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
services: | |
App\Form\TypeExtension\StrictChoiceTypeExtension: | |
tags: | |
- name: form.type_extension | |
extended_type: Symfony\Component\Form\Extension\Core\Type\ChoiceType |
This file contains hidden or 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 App\Form\DataTransformer; | |
use Symfony\Component\Form\DataTransformerInterface; | |
use Symfony\Component\Form\Exception\TransformationFailedException; | |
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer; | |
class StrictChoicesToValuesTransformer implements DataTransformerInterface | |
{ | |
private $choicesToValuesTransformer; | |
public function __construct(ChoicesToValuesTransformer $choicesToValuesTransformer) | |
{ | |
$this->choicesToValuesTransformer = $choicesToValuesTransformer; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function transform($data) | |
{ | |
if (null === $data || [] === $data) { | |
return []; | |
} | |
$values = $this->choicesToValuesTransformer->transform($data); | |
if (\count($data) !== \count($values)) { | |
throw new TransformationFailedException('Count of choices and values mismatch.'); | |
} | |
return $values; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function reverseTransform($data) | |
{ | |
if (null === $data || [] === $data) { | |
return []; | |
} | |
$choices = $this->choicesToValuesTransformer->reverseTransform($data); | |
if (\count($data) !== \count($choices)) { | |
throw new TransformationFailedException('Count of choices and values mismatch.'); | |
} | |
return $choices; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment