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') {
|
public function map($keyfield = 'ID', $titlefield = 'Title') {
|
||||||
$map = array();
|
$map = array();
|
||||||
|
|
||||||
foreach ($this->items as $item) {
|
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;
|
return $map;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,7 +301,9 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta
|
|||||||
*/
|
*/
|
||||||
public function find($key, $value) {
|
public function find($key, $value) {
|
||||||
foreach ($this->items as $item) {
|
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') {
|
public function column($colName = 'ID') {
|
||||||
$result = array();
|
$result = array();
|
||||||
|
|
||||||
foreach ($this->items as $item) {
|
foreach ($this->items as $item) {
|
||||||
$result[] = $this->extractValue($item, $colName);
|
$result[] = $this->extractValue($item, $colName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -410,9 +419,11 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta
|
|||||||
*/
|
*/
|
||||||
public function canFilterBy($by) {
|
public function canFilterBy($by) {
|
||||||
$firstRecord = $this->first();
|
$firstRecord = $this->first();
|
||||||
|
|
||||||
if ($firstRecord === false) {
|
if ($firstRecord === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_key_exists($by, $firstRecord);
|
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) {
|
public function byID($id) {
|
||||||
$firstElement = $this->filter("ID", $id)->first();
|
$firstElement = $this->filter("ID", $id)->first();
|
||||||
|
|
||||||
if ($firstElement === false) {
|
if ($firstElement === false) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $firstElement;
|
return $firstElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -490,10 +503,13 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta
|
|||||||
gettype($callback)
|
gettype($callback)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
$output = ArrayList::create();
|
$output = ArrayList::create();
|
||||||
|
|
||||||
foreach($this as $item) {
|
foreach($this as $item) {
|
||||||
if(call_user_func($callback, $item, $this)) $output->push($item);
|
if(call_user_func($callback, $item, $this)) $output->push($item);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -588,8 +604,12 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta
|
|||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
*/
|
*/
|
||||||
public function offsetSet($offset, $value) {
|
public function offsetSet($offset, $value) {
|
||||||
|
if($offset == null) {
|
||||||
|
$this->items[] = $value;
|
||||||
|
} else {
|
||||||
$this->items[$offset] = $value;
|
$this->items[$offset] = $value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unset an item with the key in $key
|
* Unset an item with the key in $key
|
||||||
|
@ -5,6 +5,20 @@
|
|||||||
*/
|
*/
|
||||||
class ArrayListTest extends SapphireTest {
|
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() {
|
public function testArrayAccessExists() {
|
||||||
$list = new ArrayList(array(
|
$list = new ArrayList(array(
|
||||||
$one = new DataObject(array('Title' => 'one')),
|
$one = new DataObject(array('Title' => 'one')),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user