Skip to content

Commit ca35f15

Browse files
committed
Implemented Collection::every()
1 parent 4a2d24f commit ca35f15

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

Cake/Test/TestCase/Utility/CollectionTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,50 @@ public function testReject() {
9797
$this->assertEquals(['a' => 1, 'b' => 2], iterator_to_array($result));
9898
$this->assertInstanceOf('\Cake\Utility\Collection', $result);
9999
}
100+
101+
/**
102+
* Tests every when the callback returns true for all elements
103+
*
104+
* @return void
105+
*/
106+
public function testEveryReturnTrue() {
107+
$items = ['a' => 1, 'b' => 2, 'c' => 3];
108+
$collection = new Collection($items);
109+
$callable = $this->getMock('stdClass', ['__invoke']);
110+
$callable->expects($this->at(0))
111+
->method('__invoke')
112+
->with(1, 'a')
113+
->will($this->returnValue(true));
114+
$callable->expects($this->at(1))
115+
->method('__invoke')
116+
->with(2, 'b')
117+
->will($this->returnValue(true));
118+
$callable->expects($this->at(2))
119+
->method('__invoke')
120+
->with(3, 'c')
121+
->will($this->returnValue(true));
122+
$this->assertTrue($collection->every($callable));
123+
}
124+
125+
/**
126+
* Tests every when the callback returns false for one of the elements
127+
*
128+
* @return void
129+
*/
130+
public function testEveryReturnFalse() {
131+
$items = ['a' => 1, 'b' => 2, 'c' => 3];
132+
$collection = new Collection($items);
133+
$callable = $this->getMock('stdClass', ['__invoke']);
134+
$callable->expects($this->at(0))
135+
->method('__invoke')
136+
->with(1, 'a')
137+
->will($this->returnValue(true));
138+
$callable->expects($this->at(1))
139+
->method('__invoke')
140+
->with(2, 'b')
141+
->will($this->returnValue(false));
142+
$callable->expects($this->exactly(2))->method('__invoke');
143+
$this->assertFalse($collection->every($callable));
144+
}
145+
100146
}

Cake/Utility/Collection.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,36 @@ public function reject(callable $c) {
117117
});
118118
}
119119

120-
public function some(callable $c) {
120+
/**
121+
* Returns true if all values in this collection pass the truth test provided
122+
* in the callback.
123+
*
124+
* Each time the callback is executed it will receive the value of the element
125+
* in the current iteration and the key of the element as arguments, in that
126+
* order.
127+
*
128+
* ###Example:
129+
*
130+
* {{{
131+
* $legalAge = (new Collection([24, 45, 60, 15]))->every(function($value, $key) {
132+
* return $value > 21;
133+
* });
134+
* }}}
135+
*
136+
* @param callable $c a callback function
137+
* @return boolean true if for all elements in this collection the provided
138+
* callback returns true, false otherwise
139+
*/
140+
public function every(callable $c) {
141+
foreach ($this as $key => $value) {
142+
if (!$c($value, $key)) {
143+
return false;
144+
}
145+
}
146+
return true;
121147
}
122148

123-
public function every(callable $c) {
149+
public function some(callable $c) {
124150
}
125151

126152
public function contains(callable $c) {

0 commit comments

Comments
 (0)