Adding __unset magic method
This commit is contained in:
32
src/Ary.php
32
src/Ary.php
@ -201,17 +201,6 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
|||||||
return count($this->items);
|
return count($this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Unset the item at a given offset.
|
|
||||||
*
|
|
||||||
* @param string $key
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function offsetUnset($key)
|
|
||||||
{
|
|
||||||
unset($this->items[$key]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert the collection to its string representation.
|
* Convert the collection to its string representation.
|
||||||
*
|
*
|
||||||
@ -282,4 +271,25 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
|||||||
{
|
{
|
||||||
return array_search($value, $this->items, $strict);
|
return array_search($value, $this->items, $strict);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function ary($item)
|
||||||
|
{
|
||||||
|
return new ary($this->get($item));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __unset($key)
|
||||||
|
{
|
||||||
|
$this->offsetUnset($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unset the item at a given offset.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function offsetUnset($key)
|
||||||
|
{
|
||||||
|
unset($this->items[$key]);
|
||||||
|
}
|
||||||
}
|
}
|
@ -127,6 +127,17 @@ class Test extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals(json_encode(new Ary($original)), json_encode((array)$original));
|
$this->assertEquals(json_encode(new Ary($original)), json_encode((array)$original));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider various
|
||||||
|
*/
|
||||||
|
public function ary()
|
||||||
|
{
|
||||||
|
$test = ['x' => ['xx' => 'xxx']];
|
||||||
|
$ary = new Ary($test);
|
||||||
|
$this->assertEquals(ary($test)->x['xx'], $ary->ary('x')->xx);
|
||||||
|
$this->assertEquals(ary(ary($test)->x['xx']), $ary->ary('x')->ary('xx'));
|
||||||
|
}
|
||||||
|
|
||||||
// public function testOffsetExists()
|
// public function testOffsetExists()
|
||||||
// {
|
// {
|
||||||
// $parameters = array(7,8,9,4);
|
// $parameters = array(7,8,9,4);
|
||||||
|
Reference in New Issue
Block a user