mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
NEW: Add support for push operator on ArrayList (Fixes #1539)
This commit is contained in:
parent
c61f6540fb
commit
a50996a010
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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')),
|
||||
|
Loading…
x
Reference in New Issue
Block a user