Skip to content

Commit

Permalink
all registries are now by reference, removes set & adds stack
Browse files Browse the repository at this point in the history
  • Loading branch information
henderjon committed Aug 9, 2016
1 parent 33d612c commit 9e80bc0
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 282 deletions.
21 changes: 18 additions & 3 deletions src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}

53 changes: 11 additions & 42 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,79 +7,48 @@
*
* @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];
}
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);
}

}
35 changes: 31 additions & 4 deletions src/RegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

}
84 changes: 0 additions & 84 deletions src/Set.php

This file was deleted.

41 changes: 0 additions & 41 deletions src/SetInterface.php

This file was deleted.

45 changes: 45 additions & 0 deletions src/Stack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Chevron\Containers;
/**
* implementation of a list -- a hybrid of a queue and a stack using redis-like
* methods of left/right push/pop
*
* @package Chevron\Container
*/
class Stack extends Registry implements StackInterface {

/**
* push values to our list
* @param mixed $value
*/
public function push($value){
array_push($this->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 ];
}

}





23 changes: 23 additions & 0 deletions src/StackInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Chevron\Containers;
/**
* establishes the minimum signature of Chevron\Containers\Registry
*
* @package Chevron\Container
*/
interface StackInterface extends RegistryInterface {

/**
* push values to our list
* @param mixed $value
*/
public function push($value);

/**
* shift values off our list
* @return mixed
*/
public function pop();

}
19 changes: 19 additions & 0 deletions src/Traits/FilterTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Chevron\Containers\Traits;

trait FilterTrait {

abstract function get($key);

abstract function set($key, $val);

public function filter($name, callable $callback = null){
$value = $this->get($name);
if($value != null && $callback){
$value = call_user_func($callback, $value);
$this->set($name, $value);
}
return $value;
}
}
Loading

0 comments on commit 9e80bc0

Please sign in to comment.