From a50996a010e9a8578c2437f9f239310412ae2ea7 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Sun, 12 Jan 2014 18:55:40 +1300 Subject: [PATCH] NEW: Add support for push operator on ArrayList (Fixes #1539) --- model/ArrayList.php | 26 +++++++++++++++++++++++--- tests/model/ArrayListTest.php | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/model/ArrayList.php b/model/ArrayList.php index e0d4d1616..9d06e8219 100644 --- a/model/ArrayList.php +++ b/model/ArrayList.php @@ -281,9 +281,14 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta */ public function map($keyfield = 'ID', $titlefield = 'Title') { $map = array(); + foreach ($this->items as $item) { - $map[$this->extractValue($item, $keyfield)] = $this->extractValue($item, $titlefield); + $map[$this->extractValue($item, $keyfield)] = $this->extractValue( + $item, + $titlefield + ); } + return $map; } @@ -296,7 +301,9 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta */ public function find($key, $value) { foreach ($this->items as $item) { - if ($this->extractValue($item, $key) == $value) return $item; + if ($this->extractValue($item, $key) == $value) { + return $item; + } } } @@ -308,9 +315,11 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta */ public function column($colName = 'ID') { $result = array(); + foreach ($this->items as $item) { $result[] = $this->extractValue($item, $colName); } + return $result; } @@ -410,9 +419,11 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta */ public function canFilterBy($by) { $firstRecord = $this->first(); + if ($firstRecord === false) { return false; } + return array_key_exists($by, $firstRecord); } @@ -470,9 +481,11 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta public function byID($id) { $firstElement = $this->filter("ID", $id)->first(); + if ($firstElement === false) { return null; } + return $firstElement; } @@ -490,10 +503,13 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta gettype($callback) )); } + $output = ArrayList::create(); + foreach($this as $item) { if(call_user_func($callback, $item, $this)) $output->push($item); } + return $output; } @@ -588,7 +604,11 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta * @param mixed $value */ public function offsetSet($offset, $value) { - $this->items[$offset] = $value; + if($offset == null) { + $this->items[] = $value; + } else { + $this->items[$offset] = $value; + } } /** diff --git a/tests/model/ArrayListTest.php b/tests/model/ArrayListTest.php index 7f0e2477e..1c9ce1a6c 100644 --- a/tests/model/ArrayListTest.php +++ b/tests/model/ArrayListTest.php @@ -5,6 +5,20 @@ */ class ArrayListTest extends SapphireTest { + public function testPushOperator() { + $list = new ArrayList(array( + array('Num' => 1) + )); + + $list[] = array('Num' => 2); + $this->assertEquals(2, count($list)); + $this->assertEquals(array('Num' => 2), $list->last()); + + $list[] = array('Num' => 3); + $this->assertEquals(3, count($list)); + $this->assertEquals(array('Num' => 3), $list->last()); + } + public function testArrayAccessExists() { $list = new ArrayList(array( $one = new DataObject(array('Title' => 'one')),