Skip to content
This repository has been archived by the owner on Dec 29, 2018. It is now read-only.

Commit

Permalink
Making the API simpler for usage.
Browse files Browse the repository at this point in the history
Compatibility break due ArrayNodeNotFoundException been removed. When trying to get a non-existent path Stingray return null. When trying to set a value to a path which doesn't exists, creates it.
  • Loading branch information
rwillians committed Nov 11, 2015
1 parent 2afa3a3 commit a25ce2c
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 292 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea
/vendor
composer.lock
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
Stingray
======
=======

Dot notation reader/writer for multidimensional arrays in PHP.


Installing via Composer
-----------------------

Add Stingray to your project:

```bash
$> composer.phar require rwillians/stingray ^1.0
$> composer.phar require rwillians/stingray ^2.0
```

or directly to composer.json:
Expand All @@ -26,8 +29,10 @@ Then update your dependencies:
$> composer.phar update
```


Example Usage
-----------------------

To get any node from an array:

```php
Expand All @@ -47,8 +52,6 @@ Stingray::set($someArray, 'client.name', 'Jane Doe');

/*
* Create a new key-value to an existent array using dot notation:
* (Notice to add the "true" flag otherwise it will throw an exception
* claiming that the required path doesn't exists.)
*/
Stingray::set($someArray, 'client.address', 'Some Street, 123', true);
```
Stingray::set($someArray, 'client.address', 'Some Street, 123');
```
3 changes: 0 additions & 3 deletions behat.yml

This file was deleted.

3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
"php": ">=5.3.3"
},
"require-dev": {
"symfony/validator": "^2.7",
"behat/behat": "^2.5"
"phpunit/phpunit": "^4.0"
},
"minimum-stability": "stable"
}
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Stingray's test case">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
30 changes: 0 additions & 30 deletions src/Exception/ArrayNodeNotFoundException.php

This file was deleted.

94 changes: 31 additions & 63 deletions src/Stingray.php
Original file line number Diff line number Diff line change
@@ -1,96 +1,64 @@
<?php
/*
* This file is part of the Stingray package.
*
* (c) Matthew Ratzke <matthew.003@me.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Rwillians\Stingray;

use Rwillians\Stingray\Exception\ArrayNodeNotFoundException;


/**
* Stingray.
* Class Stingray.
* Get or Set array node using dot notation.
*
* @author Matthew Ratzke <matthew.003@me.com>
* @author Rafael Willians <me@rwillians.com>
*/
class Stingray
{
/**
* Get's an array node using dot notation.
* @param array &$data Multidimensional array being searched
* Get's the value from an array using dot notation.
*
* @param array &$data Multidimensional array being searched
* @param string $path Dot notation string path to be searched within the multidimensional array.
* @return array Node
*
* @return mixed Returns null in the the requested path couldn't be found.
*/
public static function get(&$data, $path)
{
return static::iterateNodeGet($data, $path);
}

/**
* Set's an array node value using dot notation.
* @param array &$data Array being searched
* @param string $string String used to search array
* @param mixed $value Value to set array node
* @param bool $silent If array node is missing it will be create
*/
public static function set(&$data, $string, $value, $silent = false)
{
static::iterateNodeSet($data, $string, $value, $silent);
}

/**
* Iterate through array using dot notation.
* @param array &$data Array being searched.
* @param string $string String used to search array.
* @return array $node Array Node.
* @throws \Rwillians\Stingray\Exception\ArrayNodeNotFoundException When tried to access an invalid path.
*/
protected static function iterateNodeGet(&$data, $string)
{
$paths = explode('.', $string);
$node =& $data;
$paths = explode('.', $path);
$node = &$data;

foreach ($paths as $path) {
if (array_key_exists($path, $node)) {
$node =& $node[$path];
} else {
throw new ArrayNodeNotFoundException($path, $string);
foreach ($paths as $nextPath) {
if (!array_key_exists($nextPath, $node)) {
return null;
}

$node = &$node[$nextPath];
}

return $node;
}

/**
* Iterate through array using dot notation.
* @param array &$data Array being searched.
* @param string $string String used to search array.
* @param bool $silent If array node is missing it will be create.
* @param mixed $value The value the node should be set to.
* @return void
* @throws \Rwillians\Stingray\Exception\ArrayNodeNotFoundException When $silence if false and tried to access an invalid path.
* Set's a value to an array node using dot notation.
*
* @param array &$data Array being searched.
* @param string $path Path used to search array.
* @param mixed $value Value to set array node.
*
* @return bool
*/
protected static function iterateNodeSet(&$data, $string, $value, $silent = false)
public static function set(&$data, $path, $value)
{
$paths = explode('.', $string);
$pathCount = count($paths);
$paths = explode('.', $path);
$pathCount = count($paths);
$currentIteration = 0;
$node =& $data;
$node = &$data;

foreach ($paths as $path) {
if (array_key_exists($path, $node)) {
$node =& $node[$path];
foreach ($paths as $nextPath) {
if (array_key_exists($nextPath, $node)) {
$node = &$node[$nextPath];
$currentIteration++;
} elseif ($silent == false) {
throw new ArrayNodeNotFoundException($path, $string);
} elseif ($currentIteration < $pathCount) {
$node[$path] = array();
$node =& $node[$path];
$node[$nextPath] = [];
$node = &$node[$nextPath];
$currentIteration++;
}
}
Expand Down
6 changes: 0 additions & 6 deletions test/features/bootstrap/ComposerAutoload.php

This file was deleted.

105 changes: 0 additions & 105 deletions test/features/bootstrap/FeatureContext.php

This file was deleted.

Loading

0 comments on commit a25ce2c

Please sign in to comment.