ENHANCEMENT: Added exists(), replace(), merge(), push(), pop(), unshift() and shift() methods to ArrayList.

This commit is contained in:
ajshort 2011-05-05 17:48:12 +10:00
parent bdfff5bee3
commit 9f87294427
2 changed files with 139 additions and 1 deletions

View File

@ -21,6 +21,10 @@ class ArrayList extends ViewableData implements SS_List {
return count($this->array);
}
public function exists() {
return (bool) count($this);
}
public function getIterator() {
return new ArrayIterator($this->array);
}
@ -52,7 +56,7 @@ class ArrayList extends ViewableData implements SS_List {
}
public function add($item) {
$this->array[] = $item;
$this->push($item);
}
public function remove($item) {
@ -61,6 +65,67 @@ class ArrayList extends ViewableData implements SS_List {
}
}
/**
* Replaces an item in this list with another item.
*
* @param array|object $item
* @param array|object $with
*/
public function replace($item, $with) {
foreach ($this->array as $key => $candidate) {
if ($candidate === $item) {
$this->array[$key] = $with;
return;
}
}
}
/**
* Merges with another array or list by pushing all the items in it onto the
* end of this list.
*
* @param array|object $with
*/
public function merge($with) {
foreach ($with as $item) $this->push($item);
}
/**
* Pushes an item onto the end of this list.
*
* @param array|object $item
*/
public function push($item) {
$this->array[] = $item;
}
/**
* Pops the last element off the end of the list and returns it.
*
* @return array|object
*/
public function pop() {
return array_pop($this->array);
}
/**
* Unshifts an item onto the beginning of the list.
*
* @param array|object $item
*/
public function unshift($item) {
array_unshift($this->array, $item);
}
/**
* Shifts the item off the beginning of the list and returns it.
*
* @return array|object
*/
public function shift() {
return array_shift($this->array);
}
public function first() {
return reset($this->array);
}

View File

@ -12,6 +12,13 @@ class ArrayListTest extends SapphireTest {
$this->assertEquals(3, $list->count());
}
public function testExists() {
$list = new ArrayList();
$this->assertFalse($list->exists());
$list = new ArrayList(array(1, 2, 3));
$this->assertTrue($list->exists());
}
public function testToNestedArray() {
$list = new ArrayList(array(
array('First' => 'FirstFirst', 'Second' => 'FirstSecond'),
@ -51,6 +58,72 @@ class ArrayListTest extends SapphireTest {
));
}
public function testReplace() {
$list = new ArrayList(array(
array('Key' => 1),
$two = (object) array('Key' => 2),
(object) array('Key' => 3)
));
$this->assertEquals(array('Key' => 1), $list[0]);
$list->replace(array('Key' => 1), array('Replaced' => 1));
$this->assertEquals(3, count($list));
$this->assertEquals(array('Replaced' => 1), $list[0]);
$this->assertEquals($two, $list[1]);
$list->replace($two, array('Replaced' => 2));
$this->assertEquals(3, count($list));
$this->assertEquals(array('Replaced' => 2), $list[1]);
}
public function testMerge() {
$list = new ArrayList(array(
array('Num' => 1), array('Num' => 2)
));
$list->merge(array(
array('Num' => 3), array('Num' => 4)
));
$this->assertEquals(4, count($list));
$this->assertEquals($list->toArray(), array(
array('Num' => 1), array('Num' => 2), array('Num' => 3), array('Num' => 4)
));
}
public function testPushPop() {
$list = new ArrayList(array('Num' => 1));
$this->assertEquals(1, count($list));
$list->push(array('Num' => 2));
$this->assertEquals(2, count($list));
$this->assertEquals(array('Num' => 2), $list->last());
$list->push(array('Num' => 3));
$this->assertEquals(3, count($list));
$this->assertEquals(array('Num' => 3), $list->last());
$this->assertEquals(array('Num' => 3), $list->pop());
$this->assertEquals(2, count($list));
$this->assertEquals(array('Num' => 2), $list->last());
}
public function testShiftUnshift() {
$list = new ArrayList(array('Num' => 1));
$this->assertEquals(1, count($list));
$list->unshift(array('Num' => 2));
$this->assertEquals(2, count($list));
$this->assertEquals(array('Num' => 2), $list->first());
$list->unshift(array('Num' => 3));
$this->assertEquals(3, count($list));
$this->assertEquals(array('Num' => 3), $list->first());
$this->assertEquals(array('Num' => 3), $list->shift());
$this->assertEquals(2, count($list));
$this->assertEquals(array('Num' => 2), $list->first());
}
public function testFirstLast() {
$list = new ArrayList(array(
array('Key' => 1), array('Key' => 2), array('Key' => 3)