- extending ArrayObject class
- adding some phpunit tests.
This commit is contained in:
10
readme.md
10
readme.md
@ -5,6 +5,7 @@ Are you tired from casting objects and arrays to each other? Don't do that anymo
|
||||
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. It extends PHP ArrayObject class. So you can use its methods.
|
||||
|
||||
* The class (`Ary()`) requires PHP 5.4 or newer.
|
||||
* The helper function (`ary()`) requires PHP 5.6 or newer.
|
||||
@ -31,3 +32,12 @@ $ary['newItem']=20;
|
||||
|
||||
count($ary); //returns 3
|
||||
$ary->all(); // returns simple php array;
|
||||
~~~~~~
|
||||
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
composer require salarmehr/ary
|
||||
|
||||
|
||||
|
124
src/Ary.php
124
src/Ary.php
@ -10,14 +10,7 @@
|
||||
|
||||
namespace Salarmehr;
|
||||
|
||||
use ArrayAccess;
|
||||
use ArrayIterator;
|
||||
use CachingIterator;
|
||||
use Countable;
|
||||
use IteratorAggregate;
|
||||
use JsonSerializable;
|
||||
|
||||
class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable
|
||||
{
|
||||
|
||||
/**
|
||||
@ -25,7 +18,7 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $items = [];
|
||||
// protected $items = [];
|
||||
|
||||
/**
|
||||
* Create a new collection.
|
||||
@ -36,15 +29,13 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
public function __construct()
|
||||
{
|
||||
$items = func_get_args();
|
||||
if (count($items) == 0) {
|
||||
$this->items = [];
|
||||
if (count($items) === 0) {
|
||||
$items = [];
|
||||
}
|
||||
elseif (count($items) > 1) {
|
||||
$this->items = $items;
|
||||
}
|
||||
else {
|
||||
$this->items = is_array($items[0]) ? $items[0] : $this->getArrayableItems($items[0]);
|
||||
elseif (count($items) === 1) {
|
||||
$items = is_array($items[0]) ? $items[0] : $this->getArrayableItems($items[0]);
|
||||
}
|
||||
parent::__construct($items);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,7 +52,7 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
if (method_exists($items, 'toArray')) {
|
||||
return $items->toArray();
|
||||
}
|
||||
if ($items instanceof JsonSerializable) {
|
||||
if ($items instanceof \JsonSerializable) {
|
||||
return json_decode(json_encode($items), true);
|
||||
}
|
||||
return (array)$items;
|
||||
@ -74,7 +65,7 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->items;
|
||||
return $this->getArrayCopy();
|
||||
}
|
||||
|
||||
public function &__get($item)
|
||||
@ -97,38 +88,22 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
public function &get($key, $default = null)
|
||||
{
|
||||
if ($this->offsetExists($key)) {
|
||||
return $this->items[$key];
|
||||
|
||||
return $this[$key];
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if an item exists at an offset.
|
||||
* Get an item at a given offset.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return bool
|
||||
* @return mixed
|
||||
*/
|
||||
public function offsetExists($key)
|
||||
public function offsetGet($key)
|
||||
{
|
||||
return array_key_exists($key, $this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the item at a given offset.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function offsetSet($key, $value)
|
||||
{
|
||||
if (is_null($key)) {
|
||||
$this->items[] = $value;
|
||||
}
|
||||
else {
|
||||
$this->items[$key] = $value;
|
||||
}
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -138,7 +113,7 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->toArray();
|
||||
return $this->jsonSerialize();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -159,60 +134,7 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
*/
|
||||
public function merge($items)
|
||||
{
|
||||
return new static(array_merge($this->items, $this->getArrayableItems($items)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a CachingIterator instance.
|
||||
*
|
||||
* @param int $flags
|
||||
* @return \CachingIterator
|
||||
*/
|
||||
public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
|
||||
{
|
||||
return new CachingIterator($this->getIterator(), $flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an iterator for the items.
|
||||
*
|
||||
* @return \ArrayIterator
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return new ArrayIterator($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the number of items in the collection.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an item at a given offset.
|
||||
*
|
||||
* @param mixed $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function offsetGet($key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset the item at a given offset.
|
||||
*
|
||||
* @param string $key
|
||||
* @return void
|
||||
*/
|
||||
public function offsetUnset($key)
|
||||
{
|
||||
unset($this->items[$key]);
|
||||
return new static(array_merge($this->getArrayCopy(), $this->getArrayableItems($items)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -233,7 +155,7 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
*/
|
||||
public function toJson($options = 0)
|
||||
{
|
||||
return json_encode($this->toArray(), $options);
|
||||
return json_encode($this->all(), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -273,14 +195,4 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
return (object)$this->all();
|
||||
}
|
||||
|
||||
|
||||
// /**
|
||||
// * sets a nested ary.
|
||||
// * @param $key
|
||||
// * @param $items
|
||||
// */
|
||||
// public function setAry($key, $items)
|
||||
// {
|
||||
// $this->items[$key] = new self($items);
|
||||
// }
|
||||
}
|
109
test/Test.php
Normal file
109
test/Test.php
Normal file
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: samaneh
|
||||
* Date: 2015/10/15
|
||||
* Time: 11:12 AM
|
||||
*/
|
||||
use salarmehr\ary;
|
||||
|
||||
require '..\src\Ary.php';
|
||||
|
||||
class Test extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $item;
|
||||
|
||||
public function setup()
|
||||
{
|
||||
$this->item = [];
|
||||
}
|
||||
|
||||
public function provider()
|
||||
{
|
||||
return array(
|
||||
array(''),
|
||||
array(['ali', 'reza', 'mohammad']),
|
||||
array([1, 2, 3, 4]),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $originalary array to get
|
||||
* @param array $expectedary What we expect to get
|
||||
*
|
||||
* @dataProvider various
|
||||
*/
|
||||
|
||||
public function testAll($originalary, $expectedary)
|
||||
{
|
||||
$ary = new Ary();
|
||||
$ary->all($originalary);
|
||||
$this->assertEquals($expectedary, $originalary);
|
||||
}
|
||||
|
||||
public function various()
|
||||
{
|
||||
return array(
|
||||
array('', ''),
|
||||
array(['ali', 'reza', 'mohammad'], ['ali', 'reza', 'mohammad']),
|
||||
array(['name' => 'ali', 'lastname' => 'reza', 'age' => 30], ['name' => 'ali', 'lastname' => 'reza', 'age' => 30]),
|
||||
array((object)['name' => 'ali', 'lastname' => 'reza', 'age' => 30], (object)['name' => 'ali', 'lastname' => 'reza', 'age' => 30]),
|
||||
array(['ali', 'reza', 'mohammad'], ['ali', 'reza', 'mohammad']),
|
||||
array([1, 2, 3, 4], [1, 2, 3, 4]),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$ary = new Ary();
|
||||
$this->assertEquals($ary[0], null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @dataProvider Provider
|
||||
*/
|
||||
public function testCount($objects)
|
||||
{
|
||||
$ary = new Ary($objects);
|
||||
$result = count($objects);
|
||||
$num = count($ary);
|
||||
$this->assertEquals($result, $num);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider various
|
||||
*/
|
||||
public function testGetArrayableItems($original, $expected)
|
||||
{
|
||||
$ary = new Ary($original);
|
||||
$result = $this->invokeMethod($ary, 'getArrayableItems', array($original));
|
||||
$this->assertEquals($expected, $original);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object &$ary Instantiated object that we will run method on.
|
||||
* @param string $getArrayableItems Method name to call
|
||||
* @param array $parameters Array of parameters to pass into method.
|
||||
*
|
||||
* @return mixed Method return.
|
||||
*/
|
||||
public function invokeMethod(&$ary, $getArrayableItems, array $parameters = array())
|
||||
{
|
||||
$reflection = new \ReflectionClass(get_class($ary));
|
||||
$method = $reflection->getMethod($getArrayableItems);
|
||||
$method->setAccessible(true);
|
||||
return $method->invokeArgs($ary, $parameters);
|
||||
}
|
||||
|
||||
// public function testOffsetExists()
|
||||
// {
|
||||
// $parameters = array(7,8,9,4);
|
||||
// $ary= new Ary($parameters);
|
||||
// $key= $ary[1];
|
||||
// $result=$ary->offsetExists($key);
|
||||
// $this->assert($result);
|
||||
// }
|
||||
}
|
Reference in New Issue
Block a user