Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.2.1 #57

Merged
merged 6 commits into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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,9 +160,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 @@ -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,11 +218,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 @@ -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;
}

}
Loading