Skip to content

Commit

Permalink
OptionableArray supports nested arrays. Resolve #4
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-aranda committed Apr 13, 2015
1 parent 2e1efa9 commit 578f7f3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
34 changes: 8 additions & 26 deletions core/PHPRocks/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPRocks\Exception\Configuration\InvalidEnvironment;
use PHPRocks\Exception\Configuration\EnvironmentNotFound;
use PHPRocks\Util\JSON;
use PHPRocks\Util\OptionableArray;

/**
* PHP Rocks :: Fat Free Framework
Expand All @@ -15,7 +16,7 @@
final class Configuration{

/**
* @var array
* @var OptionableArray
*/
private $data;

Expand Down Expand Up @@ -86,7 +87,8 @@ private function invalidateData(){

$file_content = file_get_contents($this->path);

$this->data = JSON::decode($file_content, true);
$data = JSON::decode($file_content, true);
$this->data = new OptionableArray($data);
}

public function environment(){
Expand Down Expand Up @@ -117,36 +119,16 @@ private function findEnvironment($domain){

public function get($key){

$keys = explode('.', $key);
$child = $this->data;

foreach($keys as $current_key){
if( !isset($child[$current_key]) ){
throw new ValueNotSet($key);
}
$child = $child[$current_key];
if( !$this->data->has($key) ){
throw new ValueNotSet($key);
}

return $child;
return $this->data->get($key);

}

public function set($key, $value){
$keys = explode('.', $key);
$child = &$this->data;

foreach($keys as $index => $current_key){
if( $index === count($keys) - 1 ){
$child[$current_key] = $value;
}else{
if( !isset($child[$current_key]) ){
$child[$current_key] = [];
}
$child = &$child[$current_key];
}
}

return true;
return $this->data->set($key, $value);
}

public function getPerEnvironment($key, $environment = null){
Expand Down
47 changes: 38 additions & 9 deletions core/PHPRocks/Util/OptionableArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,52 @@ public function __construct(array $list){
$this->list = $list;
}

public function set($key, $value){
$this->list[$key] = $value;
public function get($key){

$keys = explode('.', $key);
$child = $this->list;

foreach($keys as $current_key){
if( !isset($child[$current_key]) ){
return null;
}
$child = $child[$current_key];
}

return $child;

return $value;
}

public function get($key){
if( !$this->has($key) ){
return null;
public function set($key, $value){
$keys = explode('.', $key);
$child = &$this->list;

foreach($keys as $index => $current_key){
if( $index === count($keys) - 1 ){
$child[$current_key] = $value;
}else{
if( !isset($child[$current_key]) ){
$child[$current_key] = [];
}
$child = &$child[$current_key];
}
}
$value = $this->list[$key];

return $value;
return true;
}

public function has($key){
return array_key_exists($key, $this->list);
$keys = explode('.', $key);
$child = $this->list;

foreach($keys as $current_key){
if( !array_key_exists($current_key, $child) ){
return false;
}
$child = $child[$current_key];
}

return true;
}

public function source(){
Expand Down

0 comments on commit 578f7f3

Please sign in to comment.