diff --git a/model/ArrayList.php b/model/ArrayList.php
index 4335b405c..dc869cd23 100644
--- a/model/ArrayList.php
+++ b/model/ArrayList.php
@@ -66,6 +66,18 @@ class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sorta
public function toArray() {
return $this->items;
}
+
+ /**
+ * Walks the list using the specified callback
+ *
+ * @param callable $callback
+ * @return DataList
+ */
+ public function each($callback) {
+ foreach($this as $item) {
+ $callback($item);
+ }
+ }
public function debug() {
$val = "
" . $this->class . "
";
diff --git a/model/DataList.php b/model/DataList.php
index dd0a4387f..25e907e83 100644
--- a/model/DataList.php
+++ b/model/DataList.php
@@ -588,6 +588,20 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
return $result;
}
+
+ /**
+ * Walks the list using the specified callback
+ *
+ * @param callable $callback
+ * @return DataList
+ */
+ public function each($callback) {
+ foreach($this as $row) {
+ $callback($row);
+ }
+
+ return $this;
+ }
public function debug() {
$val = "" . $this->class . "
";
diff --git a/model/List.php b/model/List.php
index 912cccb3a..32aff0a9d 100644
--- a/model/List.php
+++ b/model/List.php
@@ -77,4 +77,12 @@ interface SS_List extends ArrayAccess, Countable, IteratorAggregate {
* @return array
*/
public function column($colName = "ID");
+
+ /**
+ * Walks the list using the specified callback
+ *
+ * @param callable $callback
+ * @return mixed
+ */
+ public function each($callback);
}
diff --git a/model/ListDecorator.php b/model/ListDecorator.php
index 725a5ada1..a1af521eb 100644
--- a/model/ListDecorator.php
+++ b/model/ListDecorator.php
@@ -103,6 +103,10 @@ abstract class SS_ListDecorator extends ViewableData implements SS_List, SS_Sort
public function column($value = 'ID') {
return $this->list->column($value);
}
+
+ public function each($callback) {
+ return $this->list->each($callback);
+ }
public function canSortBy($by) {
return $this->list->canSortBy($by);
diff --git a/tests/model/ArrayListTest.php b/tests/model/ArrayListTest.php
index 4a73a376e..3087d2db0 100644
--- a/tests/model/ArrayListTest.php
+++ b/tests/model/ArrayListTest.php
@@ -60,6 +60,21 @@ class ArrayListTest extends SapphireTest {
array('First' => 'ThirdFirst', 'Second' => 'ThirdSecond')
));
}
+
+ public function testEach() {
+ $list = new ArrayList(array(1, 2, 3));
+
+ $count = 0;
+ $test = $this;
+
+ $list->each(function($item) use (&$count, $test) {
+ $count++;
+
+ $test->assertTrue(is_int($item));
+ });
+
+ $this->assertEquals($list->Count(), $count);
+ }
public function testLimit() {
$list = new ArrayList(array(
diff --git a/tests/model/DataListTest.php b/tests/model/DataListTest.php
index 59b36850d..7febd9115 100644
--- a/tests/model/DataListTest.php
+++ b/tests/model/DataListTest.php
@@ -179,6 +179,21 @@ class DataListTest extends SapphireTest {
$this->assertEquals($otherExpected, $otherMap);
}
+ public function testEach() {
+ $list = DataObjectTest_TeamComment::get();
+
+ $count = 0;
+ $test = $this;
+
+ $list->each(function($item) use (&$count, $test) {
+ $count++;
+
+ $test->assertTrue(is_a($item, "DataObjectTest_TeamComment"));
+ });
+
+ $this->assertEquals($list->Count(), $count);
+ }
+
public function testFilter() {
// coming soon!
}