Skip to content

Commit

Permalink
Allow passing array to AssociationCollection::type().
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jun 7, 2015
1 parent 3953c1e commit 0d612cd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ORM/AssociationCollection.php
Expand Up @@ -111,14 +111,17 @@ public function keys()
/**
* 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);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase/ORM/AssociationCollectionTest.php
Expand Up @@ -147,6 +147,10 @@ public function testType()
$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'])
);
}

/**
Expand Down

0 comments on commit 0d612cd

Please sign in to comment.