Skip to content

Commit

Permalink
Merge c164e08 into 70adfe1
Browse files Browse the repository at this point in the history
  • Loading branch information
dschoenbauer committed May 3, 2017
2 parents 70adfe1 + c164e08 commit 12e25e7
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 59 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

[![Build Status](https://travis-ci.org/dschoenbauer/dot-notation.svg?branch=develop)](https://travis-ci.org/dschoenbauer/dot-notation)
[![Coverage Status](https://coveralls.io/repos/github/dschoenbauer/dot-notation/badge.svg?branch=develop)](https://coveralls.io/github/dschoenbauer/dot-notation?branch=develop)
[![License](https://img.shields.io/packagist/l/dschoenbauer/dot-notation.svg)](https://github.com/dschoenbauer/dot-notation)
[![Downloads](https://img.shields.io/packagist/dt/dschoenbauer/dot-notation.svg)](https://packagist.org/packages/dschoenbauer/dot-notation)
[![Version](https://img.shields.io/packagist/v/dschoenbauer/dot-notation.svg)](https://github.com/dschoenbauer/dot-notation/releases)


[![License](https://img.shields.io/packagist/l/dschoenbauer/dot-notation.svg)](https://github.com/dschoenbauer/dot-notation)
[![Downloads](https://img.shields.io/packagist/dt/dschoenbauer/dot-notation.svg)](https://packagist.org/packages/dschoenbauer/dot-notation)
[![Version](https://img.shields.io/packagist/v/dschoenbauer/dot-notation.svg)](https://github.com/dschoenbauer/dot-notation/releases)


## Purpose
Simplifies access to large array structures
Expand Down Expand Up @@ -100,7 +109,7 @@ $user = $config->setNotationType(',')->get('mongo,default,user');
### WITH
````
//Functional fluent fun
$user = ArrayDotNotation::with($mongoConnection)->get('mongo,default,user');
$user = ArrayDotNotation::with($mongoConnection)->get('mongo.default.user');
/*
$user = 'username';
*/
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dschoenbauer/dot-notation",
"type": "library",
"description": "Given a complicated data structure, allows easy and safe access to data via dot notation. This library is a compilation of other Dot Notation libraries, taking the best feature from each and incorpating them here.",
"description": "Given a complicated data structure, this library allows easy and safe access to data via dot notation. This library is a compilation of other Dot Notation libraries, taking the best feature from each.",
"keywords": ["dot", "notation", "access", "array", "data"],
"license": "MIT",
"authors": [
Expand All @@ -19,7 +19,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "5.6.2",
"phpunit/phpunit": "^5.6",
"satooshi/php-coveralls": "dev-master"
},
"scripts": {
Expand Down
57 changes: 35 additions & 22 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.2.0
*/
class ArrayDotNotation {
class ArrayDotNotation
{

/**
* Property that houses the data that the dot notation should access
Expand All @@ -54,7 +53,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 @@ -65,7 +65,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 @@ -77,7 +78,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 @@ -88,7 +90,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 @@ -103,7 +106,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 @@ -115,9 +119,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 @@ -140,7 +145,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 @@ -155,9 +161,10 @@ 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
if ($key !== null && count($keys) == 0) { //Last Key
$data[$key] = $value;
} else {
if (!array_key_exists($key, $data)) {
Expand All @@ -179,7 +186,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 @@ -195,7 +203,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 @@ -210,11 +219,12 @@ 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);
} elseif ($key && count($keys) == 0) { //Last Key
} elseif ($key !== 0 && count($keys) == 0) { //Last Key
unset($data[$key]);
} elseif (!is_array($data[$key])) {
throw new PathNotArrayException($key);
Expand All @@ -229,7 +239,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 @@ -239,7 +250,8 @@ protected function getKeys($notation) {
* @since 1.2.0
* @return string current notation character delimiting the notation path
*/
public function getNotationType() {
public function getNotationType()
{
return $this->_notationType;
}

Expand All @@ -249,7 +261,8 @@ public function getNotationType() {
* @param string $notationType
* @return $this
*/
public function setNotationType($notationType = ".") {
public function setNotationType($notationType = ".")
{
$this->_notationType = $notationType;
return $this;
}
Expand All @@ -260,7 +273,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 @@ -272,5 +286,4 @@ public function has($dotNotation) {
}
return true;
}

}
Loading

0 comments on commit 12e25e7

Please sign in to comment.