Skip to content

Commit

Permalink
Implemented Collection::contains()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 25, 2013
1 parent da666fb commit a8cd64c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Cake/Test/TestCase/Utility/CollectionTest.php
Expand Up @@ -188,4 +188,18 @@ public function testSomeReturnFalse() {
$this->assertFalse($collection->some($callable));
}

/**
* Tests contains
*
* @return void
*/
public function testContains() {
$items = ['a' => 1, 'b' => 2, 'c' => 3];
$collection = new Collection($items);
$this->assertTrue($collection->contains(2));
$this->assertTrue($collection->contains(1));
$this->assertFalse($collection->contains(10));
$this->assertFalse($collection->contains('2'));
}

}
15 changes: 14 additions & 1 deletion Cake/Utility/Collection.php
Expand Up @@ -175,7 +175,20 @@ public function some(callable $c) {
return false;
}

public function contains(callable $c) {
/**
* Returns true if $value is present in this collection. Comparisons are made
* both by value and type.
*
* @param mixed $value the value to check for
* @return boolean true if $value is present in this collection
*/
public function contains($value) {
foreach ($this as $v) {
if ($value === $v) {
return true;
}
}
return false;
}

public function mapReduce(callable $map, callable $reduce) {
Expand Down

0 comments on commit a8cd64c

Please sign in to comment.