Skip to content

Commit

Permalink
Merge pull request #53 from dschoenbauer/bug/52-zero-notation-working
Browse files Browse the repository at this point in the history
Corrected issue with key that evaluated to false as triggering a false fail
  • Loading branch information
dschoenbauer committed May 3, 2017
2 parents 1ef7233 + d680e34 commit 337381f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
53 changes: 33 additions & 20 deletions src/DotNotation/ArrayDotNotation.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\DotNotation;

use DSchoenbauer\DotNotation\Exception\PathNotArrayException;
Expand All @@ -37,7 +35,8 @@
* @author David Schoenbauer
* @version 1.1.1
*/
class ArrayDotNotation {
class ArrayDotNotation
{

/**
* Property that houses the data that the dot notation should access
Expand All @@ -53,7 +52,8 @@ class ArrayDotNotation {
* @author David Schoenbauer
* @return \static
*/
public static function with(array $data = []) {
public static function with(array $data = [])
{
return new static($data);
}

Expand All @@ -64,7 +64,8 @@ public static function with(array $data = []) {
* @since 1.0.0
* @param array $data optional Array of data that will be accessed via dot notation.
*/
public function __construct(array $data = []) {
public function __construct(array $data = [])
{
$this->setData($data);
}

Expand All @@ -76,7 +77,8 @@ public function __construct(array $data = []) {
* @since 1.0.0
* @return array Array of data that will be accessed via dot notation.
*/
public function getData() {
public function getData()
{
return $this->_data;
}

Expand All @@ -87,7 +89,8 @@ public function getData() {
* @param array $data Array of data that will be accessed via dot notation.
* @return $this
*/
public function setData(array $data) {
public function setData(array $data)
{
$this->_data = $data;
return $this;
}
Expand All @@ -102,7 +105,8 @@ public function setData(array $data) {
* @param mixed $defaultValue value to return if the dot notation does not find a valid key
* @return mixed value found via dot notation in the array of data
*/
public function get($dotNotation, $defaultValue = null) {
public function get($dotNotation, $defaultValue = null)
{
return $this->recursiveGet($this->getData(), $this->getKeys($dotNotation), $defaultValue);
}

Expand All @@ -114,9 +118,10 @@ public function get($dotNotation, $defaultValue = null) {
* @param mixed $defaultValue value to return when a key is not found
* @return mixed value that the keys find in the data array
*/
protected function recursiveGet($data, $keys, $defaultValue) {
protected function recursiveGet($data, $keys, $defaultValue)
{
$key = array_shift($keys);
if (is_array($data) && $key && count($keys) == 0) { //Last Key
if (is_array($data) && $key !== null && count($keys) == 0) { //Last Key
return array_key_exists($key, $data) ? $data[$key] : $defaultValue;
} elseif (is_array($data) && array_key_exists($key, $data)) {
return $this->recursiveGet($data[$key], $keys, $defaultValue);
Expand All @@ -139,7 +144,8 @@ protected function recursiveGet($data, $keys, $defaultValue) {
* @return $this
* @throws PathNotArrayException if a value in the dot notation path is not an array
*/
public function set($dotNotation, $value) {
public function set($dotNotation, $value)
{
$this->recursiveSet($this->_data, $this->getKeys($dotNotation), $value);
return $this;
}
Expand All @@ -154,7 +160,8 @@ public function set($dotNotation, $value) {
* @param mixed $value the value to be placed at the final key
* @throws PathNotArrayException if a value in the dot notation path is not an array
*/
protected function recursiveSet(array &$data, array $keys, $value) {
protected function recursiveSet(array &$data, array $keys, $value)
{
$key = array_shift($keys);
if ($key && count($keys) == 0) { //Last Key
$data[$key] = $value;
Expand All @@ -178,7 +185,8 @@ protected function recursiveSet(array &$data, array $keys, $value) {
* @throws UnexpectedValueException if a value in the dot notation path is not an array
* @throws TargetNotArrayException if the value in the dot notation target is not an array
*/
public function merge($dotNotation, array $value) {
public function merge($dotNotation, array $value)
{
$target = $this->get($dotNotation, []);
if (!is_array($target)) {
throw new Exception\TargetNotArrayException($dotNotation);
Expand All @@ -194,7 +202,8 @@ public function merge($dotNotation, array $value) {
* @param string $dotNotation dot notation representation of keys of where to remove a value
* @return $this
*/
public function remove($dotNotation) {
public function remove($dotNotation)
{
$this->recursiveRemove($this->_data, $this->getKeys($dotNotation));
return $this;
}
Expand All @@ -209,7 +218,8 @@ public function remove($dotNotation) {
* @throws UnexpectedValueException if a value in the dot notation path is
* not an array
*/
protected function recursiveRemove(array &$data, array $keys) {
protected function recursiveRemove(array &$data, array $keys)
{
$key = array_shift($keys);
if (!array_key_exists($key, $data)) {
throw new PathNotFoundException($key);
Expand All @@ -227,7 +237,8 @@ protected function recursiveRemove(array &$data, array $keys) {
* @param type $notation key path to a value in an array
* @return array array of keys as delimited by the notation type
*/
protected function getKeys($notation) {
protected function getKeys($notation)
{
return explode($this->getNotationType(), $notation);
}

Expand All @@ -236,7 +247,8 @@ protected function getKeys($notation) {
* Default: "."
* @return string current notation character delimiting the notation path
*/
public function getNotationType() {
public function getNotationType()
{
return $this->_notationType;
}

Expand All @@ -245,7 +257,8 @@ public function getNotationType() {
* @param string $notationType
* @return $this
*/
public function setNotationType($notationType = ".") {
public function setNotationType($notationType = ".")
{
$this->_notationType = $notationType;
return $this;
}
Expand All @@ -255,7 +268,8 @@ public function setNotationType($notationType = ".") {
* @param string $dotNotation dot notation representation of keys of where to remove a value
* @return boolean returns true if the dot notation path exists in the data
*/
public function has($dotNotation) {
public function has($dotNotation)
{
$keys = $this->getKeys($dotNotation);
$dataRef = &$this->_data;
foreach ($keys as $key) {
Expand All @@ -267,5 +281,4 @@ public function has($dotNotation) {
}
return true;
}

}
10 changes: 9 additions & 1 deletion tests/DotNotation/ArrayDotNotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public function testWithNoData(){
public function testGet() {
$this->assertEquals('someValueB', $this->_object->get('levelA.levelB'));
}

public function testGetZero(){
$data = ['zero','one','two'];
$this->_object->setData($data);
foreach ($data as $key => $value) {
$this->assertEquals($value, $this->_object->get($key), $key);
}
}

public function testGetNoFindDefaultValue() {
$this->assertEquals('noValue', $this->_object->get('levelA.levelB.levelC', 'noValue'));
Expand Down Expand Up @@ -188,5 +196,5 @@ public function testHas(){
$this->assertFalse($this->_object->has('level1.level2.level3'));
$this->assertFalse($this->_object->has('level2'));
}

}

0 comments on commit 337381f

Please sign in to comment.