- keep a property synced by ArrayObject

This commit is contained in:
Reza
2015-10-18 16:30:32 +03:30
parent 28369b2aef
commit daaf4574de
2 changed files with 18 additions and 6 deletions

View File

@ -18,7 +18,7 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg
* *
* @var array * @var array
*/ */
// protected $items = []; protected $items = [];
/** /**
* Create a new collection. * Create a new collection.
@ -35,6 +35,7 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg
elseif (count($items) === 1) { elseif (count($items) === 1) {
$items = is_array($items[0]) ? $items[0] : $this->getArrayableItems($items[0]); $items = is_array($items[0]) ? $items[0] : $this->getArrayableItems($items[0]);
} }
$this->items = $items;
parent::__construct($items); parent::__construct($items);
} }
@ -75,6 +76,7 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg
public function __set($name, $value) public function __set($name, $value)
{ {
$this->items['name'] = $value;
$this->offsetSet($name, $value); $this->offsetSet($name, $value);
} }
@ -88,7 +90,7 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg
public function &get($key, $default = null) public function &get($key, $default = null)
{ {
if ($this->offsetExists($key)) { if ($this->offsetExists($key)) {
return $this->getArrayCopy()[$key]; return $this->items[$key];
} }
return $default; return $default;
} }
@ -99,7 +101,7 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg
* @param mixed $key * @param mixed $key
* @return mixed * @return mixed
*/ */
public function offsetGet($key) public function &offsetGet($key)
{ {
return $this->get($key); return $this->get($key);
} }
@ -203,8 +205,5 @@ class Ary extends \ArrayObject implements \ArrayAccess, \Countable, \IteratorAgg
public function search($value, $strict = false) public function search($value, $strict = false)
{ {
return array_search($value, $this->getArrayCopy(), $strict); return array_search($value, $this->getArrayCopy(), $strict);
} }
} }

View File

@ -99,6 +99,19 @@ class Test extends PHPUnit_Framework_TestCase
return $method->invokeArgs($ary, $parameters); 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() // public function testOffsetExists()
// { // {
// $parameters = array(7,8,9,4); // $parameters = array(7,8,9,4);