Skip to content

Commit

Permalink
made mapping to a KeyValueContainer object optional (and false by def…
Browse files Browse the repository at this point in the history
…ault)
  • Loading branch information
Burgov committed May 2, 2014
1 parent 6048a4d commit 7e3a9e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
17 changes: 16 additions & 1 deletion Form/DataTransformer/HashToKeyValueArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
class HashToKeyValueArrayTransformer implements DataTransformerInterface
{

private $useContainerObject;

/**
* @param bool $useContainerObject Whether to return a KeyValueContainer object or simply an array
*/
public function __construct($useContainerObject)
{
$this->useContainerObject = $useContainerObject;
}

/**
* Doing the transformation here would be too late for the collection type to do it's resizing magic, so
* instead it is done in the forms PRE_SET_DATA listener
Expand All @@ -17,9 +27,14 @@ public function transform($value)
return $value;
}

/**
* @param mixed $value
* @return KeyValueContainer|array
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function reverseTransform($value)
{
$return = new KeyValueContainer();
$return = $this->useContainerObject ? new KeyValueContainer() : array();

foreach ($value as $data) {
if (array('key', 'value') != array_keys($data)) {
Expand Down
3 changes: 2 additions & 1 deletion Form/Type/KeyValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class KeyValueType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addModelTransformer(new HashToKeyValueArrayTransformer());
$builder->addModelTransformer(new HashToKeyValueArrayTransformer($options['use_container_object']));

$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $e) {
$input = $e->getData();
Expand Down Expand Up @@ -44,6 +44,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'allow_delete' => true,
'value_options' => array(),
'allowed_keys' => null,
'use_container_object' => false,
'options' => function(Options $options) {
return array(
'value_type' => $options['value_type'],
Expand Down

0 comments on commit 7e3a9e1

Please sign in to comment.