Browse Source

Array support for property transformer when using $multiple = true

- Check if $entityOrCollection is array
- Add test case for transforming an array
Mihai Stancu 9 years ago
parent
commit
96db742a8d

+ 4 - 2
Form/DataTransformer/ModelToIdPropertyTransformer.php

@@ -83,10 +83,12 @@ class ModelToIdPropertyTransformer implements DataTransformerInterface
         if (!$entityOrCollection) {
         if (!$entityOrCollection) {
             return $result;
             return $result;
         }
         }
+
         if ($this->multiple) {
         if ($this->multiple) {
-            if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
+            $isArray = is_array($entityOrCollection);
+            if (!$isArray && substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
                 throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
                 throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
-            } elseif ($entityOrCollection instanceof \ArrayAccess) {
+            } elseif ($isArray || ($entityOrCollection instanceof \ArrayAccess)) {
                 $collection = $entityOrCollection;
                 $collection = $entityOrCollection;
             } else {
             } else {
                 throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
                 throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');

+ 4 - 2
Tests/Form/DataTransformer/ModelToIdPropertyTransformerTest.php

@@ -195,7 +195,7 @@ class ModelToIdPropertyTransformerTest extends \PHPUnit_Framework_TestCase
         $collection[] = $entity2;
         $collection[] = $entity2;
         $collection[] = $entity3;
         $collection[] = $entity3;
 
 
-        $this->modelManager->expects($this->exactly(3))
+        $this->modelManager->expects($this->exactly(6))
             ->method('getIdentifierValues')
             ->method('getIdentifierValues')
             ->will($this->returnCallback(function ($value) use ($entity1, $entity2, $entity3) {
             ->will($this->returnCallback(function ($value) use ($entity1, $entity2, $entity3) {
                 if ($value == $entity1) {
                 if ($value == $entity1) {
@@ -220,7 +220,9 @@ class ModelToIdPropertyTransformerTest extends \PHPUnit_Framework_TestCase
         $this->assertSame(array('identifiers' => array(), 'labels' => array()), $transformer->transform(0));
         $this->assertSame(array('identifiers' => array(), 'labels' => array()), $transformer->transform(0));
         $this->assertSame(array('identifiers' => array(), 'labels' => array()), $transformer->transform('0'));
         $this->assertSame(array('identifiers' => array(), 'labels' => array()), $transformer->transform('0'));
 
 
-        $this->assertSame(array('identifiers' => array(123, 456, 789), 'labels' => array('foo', 'bar', 'baz')), $transformer->transform($collection));
+        $expected = array('identifiers' => array(123, 456, 789), 'labels' => array('foo', 'bar', 'baz'));
+        $this->assertSame($expected, $transformer->transform($collection));
+        $this->assertSame($expected, $transformer->transform($collection->toArray()));
     }
     }
 
 
     /**
     /**