From daaf4574de4b7c22a168d437ce64685019b27b20 Mon Sep 17 00:00:00 2001 From: Reza Date: Sun, 18 Oct 2015 16:30:32 +0330 Subject: [PATCH] - keep a property synced by ArrayObject --- src/Ary.php | 11 +++++------ test/Test.php | 13 +++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Ary.php b/src/Ary.php index f4512bb..3864b0e 100644 --- a/src/Ary.php +++ b/src/Ary.php @@ -18,7 +18,7 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg * * @var array */ -// protected $items = []; + protected $items = []; /** * Create a new collection. @@ -35,6 +35,7 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg elseif (count($items) === 1) { $items = is_array($items[0]) ? $items[0] : $this->getArrayableItems($items[0]); } + $this->items = $items; parent::__construct($items); } @@ -75,6 +76,7 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg public function __set($name, $value) { + $this->items['name'] = $value; $this->offsetSet($name, $value); } @@ -88,7 +90,7 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg public function &get($key, $default = null) { if ($this->offsetExists($key)) { - return $this->getArrayCopy()[$key]; + return $this->items[$key]; } return $default; } @@ -99,7 +101,7 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg * @param mixed $key * @return mixed */ - public function offsetGet($key) + public function &offsetGet($key) { return $this->get($key); } @@ -203,8 +205,5 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg public function search($value, $strict = false) { return array_search($value, $this->getArrayCopy(), $strict); - } - - } \ No newline at end of file diff --git a/test/Test.php b/test/Test.php index 27c2f1d..17988b8 100644 --- a/test/Test.php +++ b/test/Test.php @@ -99,6 +99,19 @@ class Test extends PHPUnit_Framework_TestCase return $method->invokeArgs($ary, $parameters); } + public function testAssignment() + { + $ary = new Ary(); +// var_dump($ary);die(); + $ary[] = 3; + $this->assertEquals($ary[0], 3); + $ary['x'] = ['z' => 'y']; + $this->assertEquals($ary['x']['z'], 'y'); + $ary['x']['z'] = 'm'; + $this->assertEquals($ary['x']['z'], 'm'); + + } + // public function testOffsetExists() // { // $parameters = array(7,8,9,4);