Skip to content

Commit 7e3a9e1

Browse files
committed
made mapping to a KeyValueContainer object optional (and false by default)
1 parent 6048a4d commit 7e3a9e1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Form/DataTransformer/HashToKeyValueArrayTransformer.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
class HashToKeyValueArrayTransformer implements DataTransformerInterface
99
{
1010

11+
private $useContainerObject;
12+
13+
/**
14+
* @param bool $useContainerObject Whether to return a KeyValueContainer object or simply an array
15+
*/
16+
public function __construct($useContainerObject)
17+
{
18+
$this->useContainerObject = $useContainerObject;
19+
}
20+
1121
/**
1222
* Doing the transformation here would be too late for the collection type to do it's resizing magic, so
1323
* instead it is done in the forms PRE_SET_DATA listener
@@ -17,9 +27,14 @@ public function transform($value)
1727
return $value;
1828
}
1929

30+
/**
31+
* @param mixed $value
32+
* @return KeyValueContainer|array
33+
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
34+
*/
2035
public function reverseTransform($value)
2136
{
22-
$return = new KeyValueContainer();
37+
$return = $this->useContainerObject ? new KeyValueContainer() : array();
2338

2439
foreach ($value as $data) {
2540
if (array('key', 'value') != array_keys($data)) {

Form/Type/KeyValueType.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class KeyValueType extends AbstractType
1414
{
1515
public function buildForm(FormBuilderInterface $builder, array $options)
1616
{
17-
$builder->addModelTransformer(new HashToKeyValueArrayTransformer());
17+
$builder->addModelTransformer(new HashToKeyValueArrayTransformer($options['use_container_object']));
1818

1919
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $e) {
2020
$input = $e->getData();
@@ -44,6 +44,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
4444
'allow_delete' => true,
4545
'value_options' => array(),
4646
'allowed_keys' => null,
47+
'use_container_object' => false,
4748
'options' => function(Options $options) {
4849
return array(
4950
'value_type' => $options['value_type'],

0 commit comments

Comments
 (0)