diff --git a/composer.json b/composer.json index 55c2116..ffe6cc3 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "psr-4": { "Salarmehr\\": "src/" }, - "files":[ + "files": [ "src/helper.php" ] } diff --git a/readme.md b/readme.md index c8810d5..58bd325 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,8 @@ 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. +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 + + diff --git a/src/Ary.php b/src/Ary.php index 0c6a3b8..7fba035 100644 --- a/src/Ary.php +++ b/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); -// } } \ No newline at end of file diff --git a/test/Test.php b/test/Test.php new file mode 100644 index 0000000..2d87e1f --- /dev/null +++ b/test/Test.php @@ -0,0 +1,109 @@ +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); +// } +}