Commit 4ad90f73 authored by Laurent's avatar Laurent

add Storm_Model_Collection::reject + refactoring

parent 49e0bebc
......@@ -32,9 +32,15 @@ class Storm_Collection extends ArrayObject {
return $collection;
}
public function newInstance($elements) {
$classname = get_class($this);
return new $classname($elements);
}
public function collect($closure) {
return new Storm_Collection(array_map($closure,
(array)$this));
return $this->newInstance(array_map($closure,
(array)$this));
}
......@@ -44,7 +50,7 @@ class Storm_Collection extends ArrayObject {
public function select($closure) {
return new Storm_Collection(array_values(array_filter((array)$this, $closure)));
return $this->newInstance(array_values(array_filter((array)$this, $closure)));
}
......
......@@ -34,8 +34,7 @@ abstract class Storm_Model_Collection_Abstract extends Storm_Collection {
function($e) use ($closure_or_string) {
return $e->callGetterByAttributeName($closure_or_string);
})
: new Storm_Model_Collection(array_map($closure_or_string,
(array)$this));
: parent::collect($closure_or_string);
}
......@@ -46,7 +45,18 @@ abstract class Storm_Model_Collection_Abstract extends Storm_Collection {
function($e) use ($closure_or_string) {
return call_user_func([$e, $closure_or_string]);
})
: new Storm_Model_Collection(array_values(array_filter((array)$this, $closure_or_string)));
: parent::select($closure_or_string);
}
public function reject($closure_or_string) {
return
is_string($closure_or_string)
? $this->select(
function($e) use ($closure_or_string) {
return false == call_user_func([$e, $closure_or_string]);
})
: parent::reject($closure_or_string);
}
......
......@@ -423,6 +423,14 @@ class Storm_Test_LoaderVolatileTest extends Storm_Test_ModelTestCase {
}
/** @test */
public function rejectInvitesBySelectorNameShouldReturnAlbertAndZoe() {
$users = new Storm_Model_Collection(Storm_Test_VolatileUser::findAll());
$this->assertEquals([$this->albert, $this->zoe],
$users->reject('isInvite')->getArrayCopy());
}
/** @test */
public function collectAllLoginsShouldReturnHubert() {
$users = new Storm_Collection(Storm_Test_VolatileUser::findAll());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment