Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 51 lines (42 sloc) 0.969 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
<?php
/**
* A list decorator that allows a list to be grouped into sub-lists by common
* values of a field.
*
* @package sapphire
* @subpackage model
*/
class GroupedList extends SS_ListDecorator {

/**
* @param string $index
* @return ArrayList
*/
public function groupBy($index) {
$result = array();

foreach ($this->list as $item) {
$key = is_object($item) ? $item->$index : $item[$index];

if (array_key_exists($key, $result)) {
$result[$key]->push($item);
} else {
$result[$key] = new ArrayList(array($item));
}
}

return $result;
}

/**
* @param string $index
* @param string $children
* @return ArrayList
*/
public function GroupedBy($index, $children = 'Children') {
$grouped = $this->groupBy($index);
$result = new ArrayList();

foreach ($grouped as $indVal => $list) {
$result->push(new ArrayData(array(
$index => $indVal,
$children => $list
)));
}

return $result;
}

}
Something went wrong with that request. Please try again.