Skip to content
Permalink
Browse files

Implemented Collection::some()

  • Loading branch information...
lorenzo committed Dec 25, 2013
1 parent ca35f15 commit da666fb6920290a1fb775f2621f031605618cbfc
Showing with 72 additions and 1 deletion.
  1. +45 −0 Cake/Test/TestCase/Utility/CollectionTest.php
  2. +27 −1 Cake/Utility/Collection.php
@@ -143,4 +143,49 @@ public function testEveryReturnFalse() {
$this->assertFalse($collection->every($callable));
}
/**
* Tests some() when one of the calls return true
*
* @return void
*/
public function testSomeReturnTrue() {
$items = ['a' => 1, 'b' => 2, 'c' => 3];
$collection = new Collection($items);
$callable = $this->getMock('stdClass', ['__invoke']);
$callable->expects($this->at(0))
->method('__invoke')
->with(1, 'a')
->will($this->returnValue(false));
$callable->expects($this->at(1))
->method('__invoke')
->with(2, 'b')
->will($this->returnValue(true));
$callable->expects($this->exactly(2))->method('__invoke');
$this->assertTrue($collection->some($callable));
}
/**
* Tests some() when none of the calls return true
*
* @return void
*/
public function testSomeReturnFalse() {
$items = ['a' => 1, 'b' => 2, 'c' => 3];
$collection = new Collection($items);
$callable = $this->getMock('stdClass', ['__invoke']);
$callable->expects($this->at(0))
->method('__invoke')
->with(1, 'a')
->will($this->returnValue(false));
$callable->expects($this->at(1))
->method('__invoke')
->with(2, 'b')
->will($this->returnValue(false));
$callable->expects($this->at(2))
->method('__invoke')
->with(3, 'c')
->will($this->returnValue(false));
$this->assertFalse($collection->some($callable));
}
}
@@ -129,7 +129,7 @@ public function reject(callable $c) {
*
* {{{
* $legalAge = (new Collection([24, 45, 60, 15]))->every(function($value, $key) {
* return $value > 21;
* return $value >= 21;
* });
* }}}
*
@@ -146,7 +146,33 @@ public function every(callable $c) {
return true;
}
/**
* Returns true if any of the values in this collection pass the truth test
* provided in the callback.
*
* Each time the callback is executed it will receive the value of the element
* in the current iteration and the key of the element as arguments, in that
* order.
*
* ###Example:
*
* {{{
* $hasUnderAge = (new Collection([24, 45, 15]))->every(function($value, $key) {
* return $value < 21;
* });
* }}}
*
* @param callable $c a callback function
* @return boolean true if for all elements in this collection the provided
* callback returns true, false otherwise
*/
public function some(callable $c) {
foreach ($this as $key => $value) {
if ($c($value, $key) === true) {
return true;
}
}
return false;
}
public function contains(callable $c) {

0 comments on commit da666fb

Please sign in to comment.
You can’t perform that action at this time.