Browse Source

Allow passing array to AssociationCollection::type().

ADmad 10 years ago
parent
commit
0d612cd6ee

+ 5 - 2
src/ORM/AssociationCollection.php

@@ -111,14 +111,17 @@ class AssociationCollection implements IteratorAggregate
     /**
      * Get an array of associations matching a specific type.
      *
-     * @param string $class The type of associations you want. For example 'BelongsTo'
+     * @param string|array $class The type of associations you want.
+     *   For example 'BelongsTo' or array like ['BelongsTo', 'HasOne']
      * @return array An array of Association objects.
      */
     public function type($class)
     {
+        $class = (array)$class;
+
         $out = array_filter($this->_items, function ($assoc) use ($class) {
             list(, $name) = namespaceSplit(get_class($assoc));
-            return $class === $name;
+            return in_array($name, $class);
         });
         return array_values($out);
     }

+ 4 - 0
tests/TestCase/ORM/AssociationCollectionTest.php

@@ -147,6 +147,10 @@ class AssociationCollectionTest extends TestCase
         $this->assertSame([$belongsTo], $this->associations->type('BelongsTo'));
         $this->assertSame([$belongsToMany], $this->associations->type('BelongsToMany'));
         $this->assertSame([], $this->associations->type('HasMany'));
+        $this->assertSame(
+            [$belongsTo, $belongsToMany],
+            $this->associations->type(['BelongsTo', 'BelongsToMany'])
+        );
     }
 
     /**