From 9e80bc0b99eec8f3f892d2c578ee424c4dc30e53 Mon Sep 17 00:00:00 2001 From: henderjon Date: Tue, 9 Aug 2016 08:33:57 -0500 Subject: [PATCH] all registries are now by reference, removes set & adds stack --- src/Reference.php | 21 +++++++-- src/Registry.php | 53 +++++---------------- src/RegistryInterface.php | 35 ++++++++++++-- src/Set.php | 84 --------------------------------- src/SetInterface.php | 41 ---------------- src/Stack.php | 45 ++++++++++++++++++ src/StackInterface.php | 23 +++++++++ src/Traits/FilterTrait.php | 19 ++++++++ tests/PHPUnit/ReferenceTest.php | 30 ------------ tests/PHPUnit/RegistryTest.php | 64 +++++++++++++++---------- tests/PHPUnit/SetTest.php | 53 --------------------- tests/PHPUnit/StackTest.php | 28 +++++++++++ 12 files changed, 214 insertions(+), 282 deletions(-) delete mode 100644 src/Set.php delete mode 100644 src/SetInterface.php create mode 100644 src/Stack.php create mode 100644 src/StackInterface.php create mode 100644 src/Traits/FilterTrait.php delete mode 100644 tests/PHPUnit/ReferenceTest.php delete mode 100644 tests/PHPUnit/SetTest.php create mode 100644 tests/PHPUnit/StackTest.php diff --git a/src/Reference.php b/src/Reference.php index f92a45d..cb99818 100644 --- a/src/Reference.php +++ b/src/Reference.php @@ -7,12 +7,12 @@ * * @package Chevron\Container */ -class Reference extends Registry implements RegistryInterface { +abstract class Reference implements \Countable{ /** * The internal data array */ - // protected $map = array(); + protected $map; /** * If an array is passed to the constructor, it is assigned by REFERENCE * This is useful for a generic API to interact with the SESSION array @@ -22,11 +22,26 @@ class Reference extends Registry implements RegistryInterface { */ public function __construct( &$map = null ) { if( $map === null ) { - $map = array(); + $map = []; } $this->map =& $map; } + /** + * Method to implement \Countable on the registry + * @link http://php.net/manual/en/countable.count.php + * @return int + */ + public function count() { + return count($this->map); + } + + /** + * is the current set empty + */ + public function isEmpty(){ + return $this->count() <= 0; + } } diff --git a/src/Registry.php b/src/Registry.php index c986e85..3a2c793 100644 --- a/src/Registry.php +++ b/src/Registry.php @@ -7,38 +7,22 @@ * * @package Chevron\Container */ -class Registry implements RegistryInterface, \Countable, \IteratorAggregate { - /** - * The underlying storage array - */ - protected $map = array(); +class Registry extends Reference implements RegistryInterface { - /** - * Method to set a single value in the registry - * @param scalar $key The key at which to store the value - * @param mixed $value The value to store - * @return - */ + + /** inheritdoc */ public function set($key, $value){ $this->map[$key] = $value; } - /** - * Method to set many values in the registry - * @param array $map The map of key => values - * @return - */ + /** inheritdoc */ public function setMany(array $map){ foreach($map as $key => $value){ $this->set($key, $value); } } - /** - * Method to retrieve the value stored at key - * @param string $key The key of the value to retrieve - * @return mixed - */ + /** inheritdoc */ public function get($key){ if(array_key_exists($key, $this->map)){ return $this->map[$key]; @@ -46,40 +30,25 @@ public function get($key){ return null; } - /** - * Method to determine if the registry has a key - * @param string $key The key to check - * @return bool - */ + /** inheritdoc */ public function has($key){ return array_key_exists($key, $this->map); } - /** - * Method to get an Iterator for the registry, allows looping - * @deprecated - * @return \ArrayIterator - */ - public function getIterator(){ - return new \ArrayIterator($this->map); + /** inheritdoc */ + public function del($key){ + unset($this->map[$key]); } /** * allow access via generator * @return mixed, mixed */ - public function iter(){ + public function range(){ foreach($this->map as $key => $value){ yield $key => $value; } } - /** - * Method to implement \Countable on the registry - * @link http://php.net/manual/en/countable.count.php - * @return int - */ - public function count() { - return count($this->map); - } + } diff --git a/src/RegistryInterface.php b/src/RegistryInterface.php index 704ef21..7c5d411 100644 --- a/src/RegistryInterface.php +++ b/src/RegistryInterface.php @@ -8,6 +8,19 @@ */ interface RegistryInterface { + /** + * Method to retrieve the value stored at key + * @param scalar $key The key of the value to retrieve + * @return mixed + */ + public function get($key); + + /** + * check to see if the current registry is empty + * @return bool description + */ + public function isEmpty(); + /** * Method to set a single value in the registry * @param scalar $key The key at which to store the value @@ -17,10 +30,24 @@ interface RegistryInterface { public function set($key, $value); /** - * Method to retrieve the value stored at key - * @param scalar $key The key of the value to retrieve - * @return mixed + * set many items at once + * @param array $map the map of key=>value pairs to set into the underlying map + * @return */ - public function get($key); + public function setMany(array $map); + + /** + * Method to determine if the registry has a key + * @param string $key The key to check + * @return bool + */ + public function has($key); + + /** + * remove a key from the registry + * @param string $key The key to remove + * @return + */ + public function del($key); } diff --git a/src/Set.php b/src/Set.php deleted file mode 100644 index 2946383..0000000 --- a/src/Set.php +++ /dev/null @@ -1,84 +0,0 @@ -map, $value); - } - - /** - * push values to our list - * @param mixed $value - */ - public function rpush($value){ - // array_push - $this->map[] = $value; - } - - /** - * shift values off our list - * @return mixed - */ - public function lpop(){ - if($this->count() <= 0) return null; - return array_shift($this->map); - } - - /** - * pop values off our array - * @return mixed - */ - public function rpop(){ - if($this->count() <= 0) return null; - return array_pop($this->map); - } - - /** - * \Countable -- how big is our list - * @return int - */ - public function count(){ - return count($this->map); - } - - /** - * spin off an iterator for our list - * @return \ArrayIterator - */ - public function getIterator(){ - return new \ArrayIterator($this->map); - } - - /** - * spin off a generator for our list - * @yield \Generator - */ - // function range($rev = false){ - // $map = $rev ? array_reverse($this->map) : $this->map; - // foreach($map as $value){ - // yield $value; - // } - // } - -} - - - - - diff --git a/src/SetInterface.php b/src/SetInterface.php deleted file mode 100644 index 0190930..0000000 --- a/src/SetInterface.php +++ /dev/null @@ -1,41 +0,0 @@ -map, $value); + } + + /** + * pop values off our array + * @return mixed + */ + public function pop(){ + if($this->count()){ + return array_pop($this->map); + } + } + + /** + * view the next item in the stack + */ + public function peek($n = 1) { + if($this->isEmpty() || $n > $this->count() ){ + return null; + } + return $this->map[ $this->count() - $n ]; + } + +} + + + + + diff --git a/src/StackInterface.php b/src/StackInterface.php new file mode 100644 index 0000000..a93823c --- /dev/null +++ b/src/StackInterface.php @@ -0,0 +1,23 @@ +get($name); + if($value != null && $callback){ + $value = call_user_func($callback, $value); + $this->set($name, $value); + } + return $value; + } +} diff --git a/tests/PHPUnit/ReferenceTest.php b/tests/PHPUnit/ReferenceTest.php deleted file mode 100644 index 7b3599b..0000000 --- a/tests/PHPUnit/ReferenceTest.php +++ /dev/null @@ -1,30 +0,0 @@ - 1, "two" => 2, "three" => 3); - - $ref = new Containers\Reference($base); - - $ref->set("one", 5); - - $this->assertEquals($base["one"], 5); - - } - - function test___construct_mod_via_array(){ - - $base = array("one" => 1, "two" => 2, "three" => 3); - - $ref = new Containers\Reference($base); - - $base["one"] = 5; - - $this->assertEquals($ref->get("one"), 5); - - } -} \ No newline at end of file diff --git a/tests/PHPUnit/RegistryTest.php b/tests/PHPUnit/RegistryTest.php index 8a5f3e5..1c81ba8 100644 --- a/tests/PHPUnit/RegistryTest.php +++ b/tests/PHPUnit/RegistryTest.php @@ -4,6 +4,30 @@ class RegistryTest extends PHPUnit_Framework_TestCase { + function test___construct_mod_via_reference(){ + + $base = array("one" => 1, "two" => 2, "three" => 3); + + $ref = new Containers\Registry($base); + + $ref->set("one", 5); + + $this->assertEquals($base["one"], 5); + + } + + function test___construct_mod_via_array(){ + + $base = array("one" => 1, "two" => 2, "three" => 3); + + $ref = new Containers\Registry($base); + + $base["one"] = 5; + + $this->assertEquals($ref->get("one"), 5); + + } + function test_count_and_set(){ $R = new Containers\Registry; @@ -33,6 +57,20 @@ function test_has(){ } + function test_del(){ + + $R = new Containers\Registry; + + $pre = count($R); + + $R->set("bloop", "bleep"); + + $this->assertEquals($R->get("bloop"), "bleep"); + $R->del("bloop"); + $this->assertEquals($R->get("bloop"), null); + + } + function test_setMany(){ $R = new Containers\Registry; @@ -67,30 +105,6 @@ function test_get(){ } - function test_getIterator(){ - - $R = new Containers\Registry; - - $R->setMany(array( - "bloop" => "bleep", - "blop" => "blep", - "blope" => "blepe", - )); - - $iter = $R->getIterator(); - - $isType = $iter InstanceOf \ArrayIterator; - - $this->assertEquals($isType, true); - - foreach($iter as $key => $value){ - if(!$R->has($key)){ - $this->fail("property not found in iteration"); - } - } - - } - function test_getGenerator(){ $R = new Containers\Registry; @@ -103,7 +117,7 @@ function test_getGenerator(){ $R->setMany($values); - foreach($R->iter() as $key => $value){ + foreach($R->range() as $key => $value){ $this->assertArrayHasKey($key, $values); $this->assertEquals($value, $values[$key]); } diff --git a/tests/PHPUnit/SetTest.php b/tests/PHPUnit/SetTest.php deleted file mode 100644 index cf11637..0000000 --- a/tests/PHPUnit/SetTest.php +++ /dev/null @@ -1,53 +0,0 @@ -lpush(4); - $R->lpush(2); - $R->rpush(6); - $R->rpush(8); - - $this->assertEquals($R->lpop(), 2); - $this->assertEquals($R->rpop(), 8); - $this->assertEquals(count($R), 2); - - } - - - // function test_range(){ - - // $R = new Containers\Set; - - // $R->lpush(4); - // $R->lpush(2); - // $R->rpush(6); - // $R->rpush(8); - - // $base = 2; - // foreach($R->range() as $v){ - // $this->assertEquals($v, $base); - // $base += 2; - // } - - // $base = 2; - // $iter = $R->getIterator(); - // foreach($iter as $v){ - // $this->assertEquals($v, $base); - // $base += 2; - // } - - // $base = 8; - // foreach($R->range(1) as $v){ - // $this->assertEquals($v, $base); - // $base -= 2; - // } - - // } - -} \ No newline at end of file diff --git a/tests/PHPUnit/StackTest.php b/tests/PHPUnit/StackTest.php new file mode 100644 index 0000000..d1ed9ed --- /dev/null +++ b/tests/PHPUnit/StackTest.php @@ -0,0 +1,28 @@ +push(2); + $R->push(4); + $R->push(6); + $R->push(8); + + $this->assertEquals($R->pop(), 8); + $this->assertEquals(count($R), 3); + + $this->assertEquals($R->peek(), 6); + $this->assertEquals(count($R), 3); + + $this->assertEquals($R->peek(2), 4); + $this->assertEquals(count($R), 3); + + $this->assertEquals($R->peek(4), null); + + } +}