Commit da43a2e8 authored by Laurent's avatar Laurent

Merge branch 'add_reject' into 'master'

Add reject

See merge request !17
parents 49e0bebc 4834ba61
......@@ -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);
}
......
......@@ -157,17 +157,17 @@ class Storm_Model_Loader {
public function getPersistenceStrategy() {
if (isset($this->_persistence_stategy))
return $this->_persistence_stategy;
if (isset($this->_persistence_strategy))
return $this->_persistence_strategy;
$class_name = 'Storm_Model_PersistenceStrategy_' . static::$_default_strategy;
return $this->_persistence_stategy = new $class_name($this);
return $this->_persistence_strategy = new $class_name($this);
}
public function beVolatile() {
if (!$this->getPersistenceStrategy()->isVolatile())
$this->_persistence_stategy = new Storm_Model_PersistenceStrategy_Volatile($this);
$this->_persistence_strategy = new Storm_Model_PersistenceStrategy_Volatile($this);
return $this;
}
......
......@@ -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