2011-05-03 16:06:42 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A list object that wraps around an array of objects or arrays.
|
|
|
|
*
|
2012-12-12 05:22:45 +01:00
|
|
|
* Note that (like DataLists), the implementations of the methods from SS_Filterable, SS_Sortable and
|
|
|
|
* SS_Limitable return a new instance of ArrayList, rather than modifying the existing instance.
|
|
|
|
*
|
|
|
|
* For easy reference, methods that operate in this way are:
|
|
|
|
*
|
|
|
|
* - limit
|
|
|
|
* - reverse
|
|
|
|
* - sort
|
|
|
|
* - filter
|
|
|
|
* - exclude
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-05-03 16:06:42 +02:00
|
|
|
* @subpackage model
|
|
|
|
*/
|
2012-03-09 02:28:14 +01:00
|
|
|
class ArrayList extends ViewableData implements SS_List, SS_Filterable, SS_Sortable, SS_Limitable {
|
2011-05-03 16:06:42 +02:00
|
|
|
|
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Holds the items in the list
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-05-03 16:06:42 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2012-11-03 04:16:51 +01:00
|
|
|
protected $items = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param array $items - an initial array to fill this object with
|
|
|
|
*/
|
2011-05-05 16:30:45 +02:00
|
|
|
public function __construct(array $items = array()) {
|
2012-11-12 04:25:55 +01:00
|
|
|
$this->items = array_values($items);
|
2011-05-03 16:06:42 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-10 04:58:49 +01:00
|
|
|
/**
|
|
|
|
* Return the class of items in this list, by looking at the first item inside it.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function dataClass() {
|
2012-01-10 04:58:49 +01:00
|
|
|
if(count($this->items) > 0) return get_class($this->items[0]);
|
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Return the number of items in this list
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function count() {
|
2011-05-05 16:30:45 +02:00
|
|
|
return count($this->items);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this list has items
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2011-05-05 09:48:12 +02:00
|
|
|
public function exists() {
|
2015-08-28 16:56:54 +02:00
|
|
|
return !empty($this->items);
|
2011-05-05 09:48:12 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Returns an Iterator for this ArrayList.
|
|
|
|
* This function allows you to use ArrayList in foreach loops
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return ArrayIterator
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function getIterator() {
|
2012-10-16 07:24:40 +02:00
|
|
|
foreach($this->items as $i => $item) {
|
|
|
|
if(is_array($item)) $this->items[$i] = new ArrayData($item);
|
|
|
|
}
|
2011-05-05 16:30:45 +02:00
|
|
|
return new ArrayIterator($this->items);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Return an array of the actual items that this ArrayList contains.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return array
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function toArray() {
|
2011-05-05 16:30:45 +02:00
|
|
|
return $this->items;
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-03 02:14:04 +01:00
|
|
|
/**
|
|
|
|
* Walks the list using the specified callback
|
|
|
|
*
|
|
|
|
* @param callable $callback
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public function each($callback) {
|
|
|
|
foreach($this as $item) {
|
|
|
|
$callback($item);
|
|
|
|
}
|
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
|
2011-12-17 00:45:45 +01:00
|
|
|
public function debug() {
|
|
|
|
$val = "<h2>" . $this->class . "</h2><ul>";
|
|
|
|
foreach($this->toNestedArray() as $item) {
|
|
|
|
$val .= "<li style=\"list-style-type: disc; margin-left: 20px\">" . Debug::text($item) . "</li>";
|
|
|
|
}
|
|
|
|
$val .= "</ul>";
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Return this list as an array and every object it as an sub array as well
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @return array
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function toNestedArray() {
|
|
|
|
$result = array();
|
|
|
|
|
2011-05-05 16:30:45 +02:00
|
|
|
foreach ($this->items as $item) {
|
2011-05-03 16:06:42 +02:00
|
|
|
if (is_object($item)) {
|
|
|
|
if (method_exists($item, 'toMap')) {
|
|
|
|
$result[] = $item->toMap();
|
|
|
|
} else {
|
|
|
|
$result[] = (array) $item;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2012-03-09 02:02:37 +01:00
|
|
|
/**
|
|
|
|
* Get a sub-range of this dataobjectset as an array
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-09 02:02:37 +01:00
|
|
|
* @param int $offset
|
|
|
|
* @param int $length
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return ArrayList
|
2012-03-09 02:02:37 +01:00
|
|
|
*/
|
|
|
|
public function limit($length, $offset = 0) {
|
2013-08-05 09:59:12 +02:00
|
|
|
if(!$length) {
|
|
|
|
$length = count($this->items);
|
|
|
|
}
|
|
|
|
|
2012-12-12 22:02:18 +01:00
|
|
|
$list = clone $this;
|
|
|
|
$list->items = array_slice($this->items, $offset, $length);
|
2013-08-05 09:59:12 +02:00
|
|
|
|
2012-12-12 22:02:18 +01:00
|
|
|
return $list;
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Add this $item into this list
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param mixed $item
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function add($item) {
|
2011-05-05 09:48:12 +02:00
|
|
|
$this->push($item);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Remove this item from this list
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @param mixed $item
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function remove($item) {
|
2012-11-12 04:25:55 +01:00
|
|
|
$renumberKeys = false;
|
2011-05-05 16:30:45 +02:00
|
|
|
foreach ($this->items as $key => $value) {
|
2012-11-12 04:25:55 +01:00
|
|
|
if ($item === $value) {
|
|
|
|
$renumberKeys = true;
|
|
|
|
unset($this->items[$key]);
|
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
2012-11-12 04:25:55 +01:00
|
|
|
if($renumberKeys) $this->items = array_values($this->items);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-05-05 09:48:12 +02:00
|
|
|
/**
|
|
|
|
* Replaces an item in this list with another item.
|
|
|
|
*
|
|
|
|
* @param array|object $item
|
|
|
|
* @param array|object $with
|
2011-09-27 00:12:35 +02:00
|
|
|
* @return void;
|
2011-05-05 09:48:12 +02:00
|
|
|
*/
|
|
|
|
public function replace($item, $with) {
|
2011-05-05 16:30:45 +02:00
|
|
|
foreach ($this->items as $key => $candidate) {
|
2011-05-05 09:48:12 +02:00
|
|
|
if ($candidate === $item) {
|
2011-05-05 16:30:45 +02:00
|
|
|
$this->items[$key] = $with;
|
2011-05-05 09:48:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges with another array or list by pushing all the items in it onto the
|
|
|
|
* end of this list.
|
|
|
|
*
|
|
|
|
* @param array|object $with
|
|
|
|
*/
|
|
|
|
public function merge($with) {
|
|
|
|
foreach ($with as $item) $this->push($item);
|
|
|
|
}
|
|
|
|
|
2011-05-05 12:53:07 +02:00
|
|
|
/**
|
|
|
|
* Removes items from this list which have a duplicate value for a certain
|
|
|
|
* field. This is especially useful when combining lists.
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
*/
|
|
|
|
public function removeDuplicates($field = 'ID') {
|
|
|
|
$seen = array();
|
2012-11-12 04:25:55 +01:00
|
|
|
$renumberKeys = false;
|
2011-05-05 12:53:07 +02:00
|
|
|
|
2011-05-05 16:30:45 +02:00
|
|
|
foreach ($this->items as $key => $item) {
|
2011-09-27 00:12:35 +02:00
|
|
|
$value = $this->extractValue($item, $field);
|
2011-05-05 12:53:07 +02:00
|
|
|
|
|
|
|
if (array_key_exists($value, $seen)) {
|
2012-11-12 04:25:55 +01:00
|
|
|
$renumberKeys = true;
|
2011-05-05 16:30:45 +02:00
|
|
|
unset($this->items[$key]);
|
2011-05-05 12:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$seen[$value] = true;
|
|
|
|
}
|
2012-11-12 04:25:55 +01:00
|
|
|
|
|
|
|
if($renumberKeys) $this->items = array_values($this->items);
|
2011-05-05 12:53:07 +02:00
|
|
|
}
|
|
|
|
|
2011-05-05 09:48:12 +02:00
|
|
|
/**
|
|
|
|
* Pushes an item onto the end of this list.
|
|
|
|
*
|
|
|
|
* @param array|object $item
|
|
|
|
*/
|
|
|
|
public function push($item) {
|
2011-05-05 16:30:45 +02:00
|
|
|
$this->items[] = $item;
|
2011-05-05 09:48:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pops the last element off the end of the list and returns it.
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
|
|
|
public function pop() {
|
2011-05-05 16:30:45 +02:00
|
|
|
return array_pop($this->items);
|
2011-05-05 09:48:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-07 02:35:30 +01:00
|
|
|
* Add an item onto the beginning of the list.
|
2011-05-05 09:48:12 +02:00
|
|
|
*
|
|
|
|
* @param array|object $item
|
|
|
|
*/
|
|
|
|
public function unshift($item) {
|
2011-05-05 16:30:45 +02:00
|
|
|
array_unshift($this->items, $item);
|
2011-05-05 09:48:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shifts the item off the beginning of the list and returns it.
|
|
|
|
*
|
|
|
|
* @return array|object
|
|
|
|
*/
|
|
|
|
public function shift() {
|
2011-05-05 16:30:45 +02:00
|
|
|
return array_shift($this->items);
|
2011-05-05 09:48:12 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Returns the first item in the list
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function first() {
|
2011-05-05 16:30:45 +02:00
|
|
|
return reset($this->items);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Returns the last item in the list
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function last() {
|
2011-05-05 16:30:45 +02:00
|
|
|
return end($this->items);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Returns a map of this list
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $keyfield - the 'key' field of the result array
|
|
|
|
* @param string $titlefield - the value field of the result array
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return array
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
2011-10-22 15:42:46 +02:00
|
|
|
public function map($keyfield = 'ID', $titlefield = 'Title') {
|
2011-05-03 16:06:42 +02:00
|
|
|
$map = array();
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2011-05-05 16:30:45 +02:00
|
|
|
foreach ($this->items as $item) {
|
2014-01-12 06:55:40 +01:00
|
|
|
$map[$this->extractValue($item, $keyfield)] = $this->extractValue(
|
2014-08-15 08:53:05 +02:00
|
|
|
$item,
|
2014-01-12 06:55:40 +01:00
|
|
|
$titlefield
|
|
|
|
);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2011-05-03 16:06:42 +02:00
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Find the first item of this list where the given key = value
|
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
* @return mixed
|
2011-12-07 02:35:30 +01:00
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function find($key, $value) {
|
2011-05-05 16:30:45 +02:00
|
|
|
foreach ($this->items as $item) {
|
2014-01-12 06:55:40 +01:00
|
|
|
if ($this->extractValue($item, $key) == $value) {
|
|
|
|
return $item;
|
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Returns an array of a single field value for all items in the list.
|
|
|
|
*
|
|
|
|
* @param string $colName
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function column($colName = 'ID') {
|
2011-05-03 16:06:42 +02:00
|
|
|
$result = array();
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2011-05-05 16:30:45 +02:00
|
|
|
foreach ($this->items as $item) {
|
2011-12-07 02:35:30 +01:00
|
|
|
$result[] = $this->extractValue($item, $colName);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2011-05-03 16:06:42 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* You can always sort a ArrayList
|
|
|
|
*
|
|
|
|
* @param string $by
|
|
|
|
* @return bool
|
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function canSortBy($by) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
/**
|
|
|
|
* Reverses an {@link ArrayList}
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function reverse() {
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = clone $this;
|
2012-07-20 04:55:44 +02:00
|
|
|
$list->items = array_reverse($this->items);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-07-20 04:55:44 +02:00
|
|
|
return $list;
|
2012-04-15 10:34:10 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
/**
|
|
|
|
* Parses a specified column into a sort field and direction
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-05-15 04:25:23 +02:00
|
|
|
* @param type $column String to parse containing the column name
|
|
|
|
* @param type $direction Optional Additional argument which may contain the direction
|
|
|
|
* @return array Sort specification in the form array("Column", SORT_ASC).
|
|
|
|
*/
|
|
|
|
protected function parseSortColumn($column, $direction = null) {
|
|
|
|
// Substitute the direction for the column if column is a numeric index
|
|
|
|
if($direction && (empty($column) || is_numeric($column))) {
|
|
|
|
$column = $direction;
|
|
|
|
$direction = null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
// Parse column specification, considering possible ansi sql quoting
|
|
|
|
if(preg_match('/^"?(?<column>[^"\s]+)"?(\s+(?<direction>((asc)|(desc))(ending)?))?$/i', $column, $match)) {
|
|
|
|
$column = $match['column'];
|
|
|
|
if(empty($direction) && !empty($match['direction'])) {
|
|
|
|
$direction = $match['direction'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException("Invalid sort() column");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
// Parse sort direction specification
|
|
|
|
if(empty($direction) || preg_match('/^asc(ending)?$/i', $direction)) {
|
|
|
|
$direction = SORT_ASC;
|
|
|
|
} elseif(preg_match('/^desc(ending)?$/i', $direction)) {
|
|
|
|
$direction = SORT_DESC;
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException("Invalid sort() direction");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
return array($column, $direction);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-05 16:24:33 +02:00
|
|
|
/**
|
|
|
|
* Sorts this list by one or more fields. You can either pass in a single
|
|
|
|
* field name and direction, or a map of field names to sort directions.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-05-15 04:25:23 +02:00
|
|
|
* Note that columns may be double quoted as per ANSI sql standard
|
2011-05-05 16:24:33 +02:00
|
|
|
*
|
2011-12-08 22:08:46 +01:00
|
|
|
* @return DataList
|
2011-09-27 00:12:35 +02:00
|
|
|
* @see SS_List::sort()
|
2011-12-08 22:08:46 +01:00
|
|
|
* @example $list->sort('Name'); // default ASC sorting
|
|
|
|
* @example $list->sort('Name DESC'); // DESC sorting
|
2011-09-27 00:12:35 +02:00
|
|
|
* @example $list->sort('Name', 'ASC');
|
2011-12-08 22:08:46 +01:00
|
|
|
* @example $list->sort(array('Name'=>'ASC,'Age'=>'DESC'));
|
2011-05-05 16:24:33 +02:00
|
|
|
*/
|
2011-12-08 22:08:46 +01:00
|
|
|
public function sort() {
|
|
|
|
$args = func_get_args();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
if(count($args)==0){
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
if(count($args)>2){
|
|
|
|
throw new InvalidArgumentException('This method takes zero, one or two arguments');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
// One argument and it's a string
|
|
|
|
if(count($args)==1 && is_string($args[0])){
|
2014-05-15 04:25:23 +02:00
|
|
|
list($column, $direction) = $this->parseSortColumn($args[0]);
|
|
|
|
$columnsToSort[$column] = $direction;
|
2011-12-08 22:08:46 +01:00
|
|
|
|
2014-05-15 04:25:23 +02:00
|
|
|
} else if(count($args)==2) {
|
|
|
|
list($column, $direction) = $this->parseSortColumn($args[0], $args[1]);
|
|
|
|
$columnsToSort[$column] = $direction;
|
2011-12-08 22:08:46 +01:00
|
|
|
|
|
|
|
} else if(is_array($args[0])) {
|
2014-05-15 04:25:23 +02:00
|
|
|
foreach($args[0] as $key => $value) {
|
|
|
|
list($column, $direction) = $this->parseSortColumn($key, $value);
|
|
|
|
$columnsToSort[$column] = $direction;
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException("Bad arguments passed to sort()");
|
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
|
2015-08-26 11:29:37 +02:00
|
|
|
// Store the original keys of the items as a sort fallback, so we can preserve the original order in the event
|
|
|
|
// that array_multisort is unable to work out a sort order for them. This also prevents array_multisort trying
|
|
|
|
// to inspect object properties which can result in errors with circular dependencies
|
|
|
|
$originalKeys = array_keys($this->items);
|
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
// This the main sorting algorithm that supports infinite sorting params
|
|
|
|
$multisortArgs = array();
|
|
|
|
$values = array();
|
2015-08-26 11:29:37 +02:00
|
|
|
foreach($columnsToSort as $column => $direction) {
|
2011-12-08 22:08:46 +01:00
|
|
|
// The reason these are added to columns is of the references, otherwise when the foreach
|
|
|
|
// is done, all $values and $direction look the same
|
|
|
|
$values[$column] = array();
|
|
|
|
$sortDirection[$column] = $direction;
|
|
|
|
// We need to subtract every value into a temporary array for sorting
|
|
|
|
foreach($this->items as $index => $item) {
|
|
|
|
$values[$column][] = $this->extractValue($item, $column);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
// PHP 5.3 requires below arguments to be reference when using array_multisort together
|
2011-12-08 22:08:46 +01:00
|
|
|
// with call_user_func_array
|
|
|
|
// First argument is the 'value' array to be sorted
|
|
|
|
$multisortArgs[] = &$values[$column];
|
2014-08-15 08:53:05 +02:00
|
|
|
// First argument is the direction to be sorted,
|
2011-12-08 22:08:46 +01:00
|
|
|
$multisortArgs[] = &$sortDirection[$column];
|
2011-05-05 16:24:33 +02:00
|
|
|
}
|
2012-07-20 04:55:44 +02:00
|
|
|
|
2015-08-26 11:29:37 +02:00
|
|
|
$multisortArgs[] = &$originalKeys;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = clone $this;
|
2012-07-20 04:55:44 +02:00
|
|
|
// As the last argument we pass in a reference to the items that all the sorting will be applied upon
|
|
|
|
$multisortArgs[] = &$list->items;
|
2011-12-08 22:08:46 +01:00
|
|
|
call_user_func_array('array_multisort', $multisortArgs);
|
2012-07-20 04:55:44 +02:00
|
|
|
return $list;
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 02:02:37 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the given column can be used to filter the records.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-09 02:02:37 +01:00
|
|
|
* It works by checking the fields available in the first record of the list.
|
|
|
|
*/
|
|
|
|
public function canFilterBy($by) {
|
2013-08-05 05:43:35 +02:00
|
|
|
$firstRecord = $this->first();
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2013-08-05 05:43:35 +02:00
|
|
|
if ($firstRecord === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2013-08-05 05:43:35 +02:00
|
|
|
return array_key_exists($by, $firstRecord);
|
2012-03-09 02:02:37 +01:00
|
|
|
}
|
2011-05-05 16:24:33 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* Filter the list to include items with these charactaristics
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-08 22:08:46 +01:00
|
|
|
* @return ArrayList
|
|
|
|
* @see SS_List::filter()
|
|
|
|
* @example $list->filter('Name', 'bob'); // only bob in the list
|
|
|
|
* @example $list->filter('Name', array('aziz', 'bob'); // aziz and bob in list
|
|
|
|
* @example $list->filter(array('Name'=>'bob, 'Age'=>21)); // bob with the Age 21 in list
|
|
|
|
* @example $list->filter(array('Name'=>'bob, 'Age'=>array(21, 43))); // bob with the Age 21 or 43
|
2014-08-15 08:53:05 +02:00
|
|
|
* @example $list->filter(array('Name'=>array('aziz','bob'), 'Age'=>array(21, 43)));
|
2012-09-26 23:34:00 +02:00
|
|
|
* // aziz with the age 21 or 43 and bob with the Age 21 or 43
|
2011-12-08 22:08:46 +01:00
|
|
|
*/
|
|
|
|
public function filter() {
|
2015-08-28 16:56:54 +02:00
|
|
|
|
|
|
|
$keepUs = call_user_func_array(array($this, 'normaliseFilterArgs'), func_get_args());
|
|
|
|
|
|
|
|
$itemsToKeep = array();
|
|
|
|
foreach($this->items as $item){
|
|
|
|
$keepItem = true;
|
|
|
|
foreach ($keepUs as $column => $value) {
|
|
|
|
if ((is_array($value) && !in_array($this->extractValue($item, $column), $value))
|
|
|
|
|| (!is_array($value) && $this->extractValue($item, $column) != $value)
|
|
|
|
) {
|
|
|
|
$keepItem = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($keepItem) {
|
|
|
|
$itemsToKeep[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = clone $this;
|
|
|
|
$list->items = $itemsToKeep;
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a copy of this list which contains items matching any of these charactaristics.
|
|
|
|
*
|
|
|
|
* @example // only bob in the list
|
|
|
|
* $list = $list->filterAny('Name', 'bob');
|
|
|
|
* @example // azis or bob in the list
|
|
|
|
* $list = $list->filterAny('Name', array('aziz', 'bob');
|
|
|
|
* @example // bob or anyone aged 21 in the list
|
|
|
|
* $list = $list->filterAny(array('Name'=>'bob, 'Age'=>21));
|
|
|
|
* @example // bob or anyone aged 21 or 43 in the list
|
|
|
|
* $list = $list->filterAny(array('Name'=>'bob, 'Age'=>array(21, 43)));
|
|
|
|
* @example // all bobs, phils or anyone aged 21 or 43 in the list
|
|
|
|
* $list = $list->filterAny(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
|
|
|
|
*
|
|
|
|
* @param string|array See {@link filter()}
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public function filterAny() {
|
|
|
|
$keepUs = call_user_func_array(array($this, 'normaliseFilterArgs'), func_get_args());
|
|
|
|
|
|
|
|
$itemsToKeep = array();
|
|
|
|
|
|
|
|
foreach ($this->items as $item) {
|
|
|
|
foreach ($keepUs as $column => $value) {
|
|
|
|
$extractedValue = $this->extractValue($item, $column);
|
|
|
|
$matches = is_array($value) ? in_array($extractedValue, $value) : $extractedValue == $value;
|
|
|
|
if ($matches) {
|
|
|
|
$itemsToKeep[] = $item;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = clone $this;
|
|
|
|
$list->items = array_unique($itemsToKeep, SORT_REGULAR);
|
|
|
|
return $list;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Take the "standard" arguments that the filter/exclude functions take and return a single array with
|
|
|
|
* 'colum' => 'value'
|
|
|
|
*
|
|
|
|
* @param $column array|string The column name to filter OR an assosicative array of column => value
|
|
|
|
* @param $value array|string|null The values to filter the $column against
|
|
|
|
*
|
|
|
|
* @return array The normalised keyed array
|
|
|
|
*/
|
|
|
|
protected function normaliseFilterArgs($column, $value = null) {
|
2011-12-08 22:08:46 +01:00
|
|
|
if(count(func_get_args())>2){
|
|
|
|
throw new InvalidArgumentException('filter takes one array or two arguments');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
if(count(func_get_args()) == 1 && !is_array(func_get_arg(0))){
|
|
|
|
throw new InvalidArgumentException('filter takes one array or two arguments');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
$keepUs = array();
|
|
|
|
if(count(func_get_args())==2){
|
|
|
|
$keepUs[func_get_arg(0)] = func_get_arg(1);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
if(count(func_get_args())==1 && is_array(func_get_arg(0))){
|
|
|
|
foreach(func_get_arg(0) as $column => $value) {
|
|
|
|
$keepUs[$column] = $value;
|
2011-05-05 16:24:33 +02:00
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-08-28 16:56:54 +02:00
|
|
|
return $keepUs;
|
|
|
|
}
|
2011-12-08 22:08:46 +01:00
|
|
|
|
2015-08-28 16:56:54 +02:00
|
|
|
/**
|
|
|
|
* Filter this list to only contain the given Primary IDs
|
|
|
|
*
|
|
|
|
* @param array $ids Array of integers, will be automatically cast/escaped.
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function byIDs($ids) {
|
|
|
|
$ids = array_map('intval', $ids); // sanitize
|
|
|
|
return $this->filter('ID', $ids);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-09 06:47:59 +01:00
|
|
|
public function byID($id) {
|
2013-08-05 05:43:35 +02:00
|
|
|
$firstElement = $this->filter("ID", $id)->first();
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2013-08-05 05:43:35 +02:00
|
|
|
if ($firstElement === false) {
|
|
|
|
return null;
|
|
|
|
}
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2013-08-05 05:43:35 +02:00
|
|
|
return $firstElement;
|
2012-01-09 06:47:59 +01:00
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
|
2013-10-30 14:08:55 +01:00
|
|
|
/**
|
|
|
|
* @see SS_Filterable::filterByCallback()
|
|
|
|
*
|
|
|
|
* @example $list = $list->filterByCallback(function($item, $list) { return $item->Age == 9; })
|
|
|
|
* @param callable $callback
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function filterByCallback($callback) {
|
|
|
|
if(!is_callable($callback)) {
|
|
|
|
throw new LogicException(sprintf(
|
|
|
|
"SS_Filterable::filterByCallback() passed callback must be callable, '%s' given",
|
|
|
|
gettype($callback)
|
|
|
|
));
|
|
|
|
}
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2016-02-10 20:19:29 +01:00
|
|
|
$output = static::create();
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2013-10-30 14:08:55 +01:00
|
|
|
foreach($this as $item) {
|
|
|
|
if(call_user_func($callback, $item, $this)) $output->push($item);
|
|
|
|
}
|
2014-01-12 06:55:40 +01:00
|
|
|
|
2013-10-30 14:08:55 +01:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
/**
|
|
|
|
* Exclude the list to not contain items with these charactaristics
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
* @see SS_List::exclude()
|
|
|
|
* @example $list->exclude('Name', 'bob'); // exclude bob from list
|
|
|
|
* @example $list->exclude('Name', array('aziz', 'bob'); // exclude aziz and bob from list
|
|
|
|
* @example $list->exclude(array('Name'=>'bob, 'Age'=>21)); // exclude bob that has Age 21
|
|
|
|
* @example $list->exclude(array('Name'=>'bob, 'Age'=>array(21, 43))); // exclude bob with Age 21 or 43
|
2012-09-26 23:34:00 +02:00
|
|
|
* @example $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
|
|
|
|
* // bob age 21 or 43, phil age 21 or 43 would be excluded
|
2011-12-08 22:08:46 +01:00
|
|
|
*/
|
|
|
|
public function exclude() {
|
|
|
|
|
2015-08-28 16:56:54 +02:00
|
|
|
$removeUs = call_user_func_array(array($this, 'normaliseFilterArgs'), func_get_args());
|
2011-12-08 22:08:46 +01:00
|
|
|
|
|
|
|
$hitsRequiredToRemove = count($removeUs);
|
|
|
|
$matches = array();
|
|
|
|
foreach($removeUs as $column => $excludeValue) {
|
|
|
|
foreach($this->items as $key => $item){
|
|
|
|
if(!is_array($excludeValue) && $this->extractValue($item, $column) == $excludeValue) {
|
|
|
|
$matches[$key]=isset($matches[$key])?$matches[$key]+1:1;
|
|
|
|
} elseif(is_array($excludeValue) && in_array($this->extractValue($item, $column), $excludeValue)) {
|
|
|
|
$matches[$key]=isset($matches[$key])?$matches[$key]+1:1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$keysToRemove = array_keys($matches,$hitsRequiredToRemove);
|
2012-07-20 04:55:44 +02:00
|
|
|
|
2012-11-12 04:25:55 +01:00
|
|
|
$itemsToKeep = array();
|
|
|
|
foreach($this->items as $key => $value) {
|
|
|
|
if(!in_array($key, $keysToRemove)) {
|
|
|
|
$itemsToKeep[] = $value;
|
|
|
|
}
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
2012-07-20 04:55:44 +02:00
|
|
|
|
2012-12-12 05:22:45 +01:00
|
|
|
$list = clone $this;
|
2012-11-12 04:25:55 +01:00
|
|
|
$list->items = $itemsToKeep;
|
2012-07-20 04:55:44 +02:00
|
|
|
return $list;
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function shouldExclude($item, $args) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-08 22:08:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Returns whether an item with $key exists
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param mixed $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function offsetExists($offset) {
|
2011-05-05 16:30:45 +02:00
|
|
|
return array_key_exists($offset, $this->items);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Returns item stored in list with index $key
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param mixed $key
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function offsetGet($offset) {
|
2011-05-05 16:30:45 +02:00
|
|
|
if ($this->offsetExists($offset)) return $this->items[$offset];
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Set an item with the key in $key
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param mixed $key
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function offsetSet($offset, $value) {
|
2014-01-12 06:55:40 +01:00
|
|
|
if($offset == null) {
|
|
|
|
$this->items[] = $value;
|
|
|
|
} else {
|
|
|
|
$this->items[$offset] = $value;
|
|
|
|
}
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
2011-12-07 02:35:30 +01:00
|
|
|
/**
|
|
|
|
* Unset an item with the key in $key
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-07 02:35:30 +01:00
|
|
|
* @param mixed $key
|
|
|
|
*/
|
2011-05-03 16:06:42 +02:00
|
|
|
public function offsetUnset($offset) {
|
2011-05-05 16:30:45 +02:00
|
|
|
unset($this->items[$offset]);
|
2011-05-03 16:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts a value from an item in the list, where the item is either an
|
|
|
|
* object or array.
|
|
|
|
*
|
|
|
|
* @param array|object $item
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2011-09-27 00:12:35 +02:00
|
|
|
protected function extractValue($item, $key) {
|
2011-05-03 16:06:42 +02:00
|
|
|
if (is_object($item)) {
|
|
|
|
return $item->$key;
|
|
|
|
} else {
|
|
|
|
if (array_key_exists($key, $item)) return $item[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|