Skip to content

Commit

Permalink
Merge pull request #1 from chevronphp/feature/generatorAccess
Browse files Browse the repository at this point in the history
adds a generator (it's just time); deprecates IteratorAggregate
  • Loading branch information
henderjon committed Feb 18, 2016
2 parents e57a7bf + f765c60 commit 5f906df
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

matrix:
Expand Down
23 changes: 17 additions & 6 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Registry implements RegistryInterface, \Countable, \IteratorAggregate {
* @param mixed $value The value to store
* @return
*/
function set($key, $value){
public function set($key, $value){
$this->map[$key] = $value;
}

Expand All @@ -28,7 +28,7 @@ function set($key, $value){
* @param array $map The map of key => values
* @return
*/
function setMany(array $map){
public function setMany(array $map){
foreach($map as $key => $value){
$this->set($key, $value);
}
Expand All @@ -39,7 +39,7 @@ function setMany(array $map){
* @param string $key The key of the value to retrieve
* @return mixed
*/
function get($key){
public function get($key){
if(array_key_exists($key, $this->map)){
return $this->map[$key];
}
Expand All @@ -51,24 +51,35 @@ function get($key){
* @param string $key The key to check
* @return bool
*/
function has($key){
public function has($key){
return array_key_exists($key, $this->map);
}

/**
* Method to get an Iterator for the registry, allows looping
* @deprecated
* @return \ArrayIterator
*/
function getIterator(){
public function getIterator(){
return new \ArrayIterator($this->map);
}

/**
* allow access via generator
* @return mixed, mixed
*/
public function iter(){
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
*/
function count() {
public function count() {
return count($this->map);
}
}
19 changes: 19 additions & 0 deletions tests/PHPUnit/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,24 @@ function test_getIterator(){
}

}

function test_getGenerator(){

$R = new Containers\Registry;

$values = [
"bloop" => "bleep",
"blop" => "blep",
"blope" => "blepe",
];

$R->setMany($values);

foreach($R->iter() as $key => $value){
$this->assertArrayHasKey($key, $values);
$this->assertEquals($value, $values[$key]);
}

}
}

0 comments on commit 5f906df

Please sign in to comment.