Adding replace and replaceRecursively methods
This commit is contained in:
@ -20,5 +20,8 @@
|
||||
"files": [
|
||||
"src/helper.php"
|
||||
]
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^6.3"
|
||||
}
|
||||
}
|
||||
|
25
readme.md
25
readme.md
@ -1,15 +1,22 @@
|
||||
Ary
|
||||
===
|
||||
|
||||
Are you tired from casting objects and arrays to each other or are bored using is `isset`? Don't do those anymore!
|
||||
Ary is a light class/function that makes accessing array items more convenient.
|
||||
Ary provides unified interface for ary/object. Really handy to store:
|
||||
|
||||
- arrays/objects those their items/properties varies conditionally.
|
||||
- storing registry, system configuration, env variables, etc.
|
||||
- when you perform a chain of action on an array/object
|
||||
|
||||
It's super cool and after a while you can not code in php without it.
|
||||
|
||||
Features
|
||||
---------
|
||||
1. You can access array items using `->` or `['']` syntax.
|
||||
2. You will get `null` if an index does not exists (instead of a nasty notification!)
|
||||
3. You can specify a default value for missing indexes.
|
||||
4. You can set/get a value within a deeply nested array using "dot" notation.
|
||||
5. A bunch of really handy methods: `merge`, `only`, `search`, `toObject`, ...
|
||||
6. (new) It extends `Illuminate\Support\Collection` class so all of its methods are available (https://laravel.com/docs/master/eloquent-collections)
|
||||
6. Now, `Ary` extends `Illuminate\Support\Collection` class so all of its methods are available (https://laravel.com/docs/master/eloquent-collections)
|
||||
|
||||
Examples
|
||||
--------
|
||||
@ -24,7 +31,7 @@ $ary = ary(2, 4, 6, 8); // or
|
||||
$ary = ary([2, 4, 6, 8]);
|
||||
$ary = ary(['x' => 'foo', 'y' => 'bar']);
|
||||
|
||||
//Assignment;
|
||||
//Assignment
|
||||
$ary->newItem=20; //or
|
||||
$ary['newItem']=20;
|
||||
|
||||
@ -34,7 +41,7 @@ $foo = $ary['x'];
|
||||
$missed = $ary->get('missed', 'Default value');
|
||||
$ary->all(); // returns the simple php array;
|
||||
|
||||
// behave similar to regular arrays
|
||||
// behaive as regular arrays
|
||||
count($ary); //returns 3
|
||||
unset($ary[0]);
|
||||
json_encode($ary);
|
||||
@ -48,14 +55,14 @@ $ary['production.table.weight']=200;
|
||||
|
||||
Installation
|
||||
============
|
||||
Simply, `include '__DIR__.'/src/helper.php';`
|
||||
Simply `include '__DIR__.'/src/helper.php';`
|
||||
|
||||
or using Composer:
|
||||
or use Composer:
|
||||
|
||||
composer require salarmehr/ary
|
||||
|
||||
* The class (`Ary()`) requires PHP 5.4 or newer.
|
||||
* The helper function (`ary()`) requires PHP 5.6 or newer.
|
||||
* The class (`Ary()`) requires PHP 5.4 or above.
|
||||
* The helper function (`ary()`) requires PHP 5.6 or above.
|
||||
|
||||
Licence
|
||||
=======
|
||||
|
43
src/Ary.php
43
src/Ary.php
@ -10,16 +10,12 @@
|
||||
|
||||
namespace Salarmehr;
|
||||
|
||||
use ArrayAccess;
|
||||
use Countable;
|
||||
use Illuminate\Support\Collection;
|
||||
use IteratorAggregate;
|
||||
use JsonSerializable;
|
||||
|
||||
class Ary extends Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
class Ary extends Collection
|
||||
{
|
||||
/**
|
||||
* Create a new collection.
|
||||
* Create a new ary.
|
||||
*
|
||||
* @param mixed $items
|
||||
*/
|
||||
@ -47,7 +43,7 @@ class Ary extends Collection implements ArrayAccess, Countable, IteratorAggregat
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item from the collection by key.
|
||||
* Get an item from the ary by key.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param mixed $default
|
||||
@ -84,8 +80,6 @@ class Ary extends Collection implements ArrayAccess, Countable, IteratorAggregat
|
||||
return;
|
||||
}
|
||||
|
||||
// $this->items[$key] = $value;
|
||||
|
||||
$keys = explode('.', $key);
|
||||
$array =& $this->items;
|
||||
while (count($keys) > 1) {
|
||||
@ -122,7 +116,7 @@ class Ary extends Collection implements ArrayAccess, Countable, IteratorAggregat
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the collection of items as a plain object.
|
||||
* Get the ary of items as a plain object.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
@ -138,11 +132,38 @@ class Ary extends Collection implements ArrayAccess, Countable, IteratorAggregat
|
||||
*/
|
||||
public function ary($item)
|
||||
{
|
||||
return new ary($this->get($item));
|
||||
return new self($this->get($item));
|
||||
}
|
||||
|
||||
public function __unset($key)
|
||||
{
|
||||
$this->offsetUnset($key);
|
||||
}
|
||||
|
||||
#region extra helpers
|
||||
|
||||
/**
|
||||
* Replaces elements from passed arrays into the collection
|
||||
*
|
||||
* @param mixed $items
|
||||
* @return static
|
||||
* @see http://php.net/manual/en/function.array-replace-recursive.php
|
||||
*/
|
||||
public function replace($items)
|
||||
{
|
||||
return new static(array_replace($this->items, $this->getArrayableItems($items)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces elements from passed arrays into the collection recursively
|
||||
*
|
||||
* @param mixed $items
|
||||
* @return static
|
||||
* @see http://php.net/manual/en/function.array-replace-recursive.php
|
||||
*/
|
||||
public function replaceRecursively($items)
|
||||
{
|
||||
return new static(array_replace_recursive($this->items, $this->getArrayableItems($items)));
|
||||
}
|
||||
#endrtiong
|
||||
}
|
@ -13,7 +13,7 @@ if (!function_exists('ary') && phpversion()) {
|
||||
function parse_version($version)
|
||||
{
|
||||
$version = explode('.', $version);
|
||||
return $version[0] * 10000 + $version[1] * 100 + $version[2];
|
||||
return $version[0] * 10000 + $version[1] * 100 + (int) $version[2];
|
||||
}
|
||||
|
||||
if (!defined('ary') && parse_version(PHP_VERSION) > parse_version('5.6.0')) {
|
||||
|
@ -12,7 +12,8 @@ use \Salarmehr\Ary;
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
require __DIR__ . '/../src/Ary.php';
|
||||
|
||||
class Test extends PHPUnit_Framework_TestCase
|
||||
class Test extends PHPUnit\Framework\TestCase
|
||||
|
||||
{
|
||||
protected $item;
|
||||
|
||||
@ -40,7 +41,7 @@ class Test extends PHPUnit_Framework_TestCase
|
||||
public function testAll($originalary, $expectedary)
|
||||
{
|
||||
$ary = new Ary();
|
||||
$ary->all($originalary);
|
||||
$ary->all();
|
||||
$this->assertEquals($expectedary, $originalary);
|
||||
}
|
||||
|
||||
@ -85,7 +86,7 @@ class Test extends PHPUnit_Framework_TestCase
|
||||
public function testGetArrayableItems($original, $expected)
|
||||
{
|
||||
$ary = new Ary($original);
|
||||
$result = $this->invokeMethod($ary, 'getArrayableItems', array($original));
|
||||
$this->invokeMethod($ary, 'getArrayableItems', array($original));
|
||||
$this->assertEquals($expected, $original);
|
||||
}
|
||||
|
||||
@ -125,8 +126,9 @@ class Test extends PHPUnit_Framework_TestCase
|
||||
|
||||
/**
|
||||
* @dataProvider various
|
||||
* @param $original
|
||||
*/
|
||||
public function testJsonSerialize($original, $expected)
|
||||
public function testJsonSerialize($original)
|
||||
{
|
||||
$this->assertEquals(json_encode(new Ary($original)), json_encode((array)$original));
|
||||
}
|
||||
@ -142,6 +144,21 @@ class Test extends PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(ary(ary($test)->x['xx']), $ary->ary('x')->ary('xx'));
|
||||
}
|
||||
|
||||
public function testReplace()
|
||||
{
|
||||
$c = new Ary(['foo' => 'x', 'bar' => 'y']);
|
||||
$this->assertEquals(['foo' => 'f', 'bar' => 'y', 'baz' => 'z'], $c->replace(new Ary(['foo' => 'f', 'baz' => 'z']))->all());
|
||||
}
|
||||
|
||||
public function testReplaceAryRecursively()
|
||||
{
|
||||
$base = ['citrus' => ["orange"], 'berries' => ["blackberry", "raspberry"],];
|
||||
$replacements = ['citrus' => ['pineapple'], 'berries' => ['blueberry']];
|
||||
$expect = ['citrus' => ['pineapple'], 'berries' => ['blueberry', 'raspberry']];
|
||||
$c = new Ary($base);
|
||||
$this->assertEquals($expect, $c->replaceRecursively(new Ary($replacements))->all());
|
||||
}
|
||||
|
||||
// public function testOffsetExists()
|
||||
// {
|
||||
// $parameters = array(7,8,9,4);
|
||||
|
Reference in New Issue
Block a user