MINOR Updated inline documentation and added visibility keywords to methods for SS_List, DataList and ArrayList

This commit is contained in:
Stig Lindqvist 2011-12-07 14:35:30 +13:00 committed by Sam Minnee
parent 60ced3b167
commit 2306ec94c3
3 changed files with 359 additions and 72 deletions

View File

@ -8,27 +8,66 @@
class ArrayList extends ViewableData implements SS_List {
/**
* Holds the items in the list
*
* @var array
*/
protected $items;
/**
* Synonym of the constructor. Can be chained with literate methods.
* ArrayList::create("SiteTree")->sort("Title") is legal, but
* new ArrayList("SiteTree")->sort("Title") is not.
*
* @param array $items - an initial array to fill this object with
*/
public static function create(array $items = array()) {
return new ArrayList($items);
}
/**
*
* @param array $items - an initial array to fill this object with
*/
public function __construct(array $items = array()) {
$this->items = $items;
parent::__construct();
}
/**
* Return the number of items in this list
*
* @return int
*/
public function count() {
return count($this->items);
}
/**
* Returns true if this list has items
*
* @return bool
*/
public function exists() {
return (bool) count($this);
}
/**
* Returns an Iterator for this ArrayList.
* This function allows you to use ArrayList in foreach loops
*
* @return ArrayIterator
*/
public function getIterator() {
return new ArrayIterator($this->items);
}
/**
* Return an array of the actual items that this ArrayList contains.
*
* @return array
*/
public function toArray() {
return $this->items;
}
@ -42,6 +81,11 @@ class ArrayList extends ViewableData implements SS_List {
return $val;
}
/**
* Return this list as an array and every object it as an sub array as well
*
* @return array
*/
public function toNestedArray() {
$result = array();
@ -60,14 +104,31 @@ class ArrayList extends ViewableData implements SS_List {
return $result;
}
/**
* Get a sub-range of this dataobjectset as an array
*
* @param int $offset
* @param int $length
* @return ArrayList
*/
public function getRange($offset, $length) {
return new ArrayList(array_slice($this->items, $offset, $length));
}
/**
* Add this $item into this list
*
* @param mixed $item
*/
public function add($item) {
$this->push($item);
}
/**
* Remove this item from this list
*
* @param mixed $item
*/
public function remove($item) {
foreach ($this->items as $key => $value) {
if ($item === $value) unset($this->items[$key]);
@ -139,7 +200,7 @@ class ArrayList extends ViewableData implements SS_List {
}
/**
* Unshifts an item onto the beginning of the list.
* Add an item onto the beginning of the list.
*
* @param array|object $item
*/
@ -156,14 +217,31 @@ class ArrayList extends ViewableData implements SS_List {
return array_shift($this->items);
}
/**
* Returns the first item in the list
*
* @return mixed
*/
public function first() {
return reset($this->items);
}
/**
* Returns the last item in the list
*
* @return mixed
*/
public function last() {
return end($this->items);
}
/**
* Returns a map of this list
*
* @param type $keyfield - the 'key' field of the result array
* @param type $titlefield - the value field of the result array
* @return array
*/
public function map($keyfield = 'ID', $titlefield = 'Title') {
$map = array();
foreach ($this->items as $item) {
@ -172,20 +250,39 @@ class ArrayList extends ViewableData implements SS_List {
return $map;
}
/**
* Find the first item of this list where the given key = value
*
* @param type $key
* @param type $value
* @return type
*/
public function find($key, $value) {
foreach ($this->items as $item) {
if ($this->extractValue($item, $key) == $value) return $item;
}
}
public function column($field = 'ID') {
/**
* Returns an array of a single field value for all items in the list.
*
* @param string $colName
* @return array
*/
public function column($colName = 'ID') {
$result = array();
foreach ($this->items as $item) {
$result[] = $this->extractValue($item, $field);
$result[] = $this->extractValue($item, $colName);
}
return $result;
}
/**
* You can always sort a ArrayList
*
* @param string $by
* @return bool
*/
public function canSortBy($by) {
return true;
}
@ -221,18 +318,41 @@ class ArrayList extends ViewableData implements SS_List {
call_user_func_array('array_multisort', $sorts);
}
/**
* Returns whether an item with $key exists
*
* @param mixed $key
* @return bool
*/
public function offsetExists($offset) {
return array_key_exists($offset, $this->items);
}
/**
* Returns item stored in list with index $key
*
* @param mixed $key
* @return DataObject
*/
public function offsetGet($offset) {
if ($this->offsetExists($offset)) return $this->items[$offset];
}
/**
* Set an item with the key in $key
*
* @param mixed $key
* @param mixed $value
*/
public function offsetSet($offset, $value) {
$this->items[$offset] = $value;
}
/**
* Unset an item with the key in $key
*
* @param mixed $key
*/
public function offsetUnset($offset) {
unset($this->items[$offset]);
}

View File

@ -2,20 +2,29 @@
/**
* Implements a "lazy loading" DataObjectSet.
* Uses {@link DataQuery} to do the actual query generation.
*
* @package sapphire
* @subpackage model
*/
class DataList extends ViewableData implements SS_List {
/**
* The DataObject class name that this data list is querying
*
* @var string
*/
protected $dataClass;
/**
* The {@link DataQuery} object responsible for getting this DataList's records
*
* @var DataQuery
*/
protected $dataQuery;
/**
* The DataModel from which this DataList comes.
*
* @var DataModel
*/
protected $model;
@ -23,45 +32,63 @@ class DataList extends ViewableData implements SS_List {
* Synonym of the constructor. Can be chained with literate methods.
* DataList::create("SiteTree")->sort("Title") is legal, but
* new DataList("SiteTree")->sort("Title") is not.
*
* @param string $dataClass - The DataObject class to query.
*/
static function create($dataClass) {
public static function create($dataClass) {
return new DataList($dataClass);
}
/**
* Create a new DataList.
* No querying is done on construction, but the initial query schema is set up.
* @param $dataClass The DataObject class to query.
*
* @param string $dataClass - The DataObject class to query.
*/
public function __construct($dataClass) {
$this->dataClass = $dataClass;
$this->dataQuery = new DataQuery($this->dataClass);
parent::__construct();
}
/**
* Set the DataModel
*
* @param DataModel $model
*/
public function setModel(DataModel $model) {
$this->model = $model;
}
/**
* Get the dataClass name for this DataList, ie the DataObject ClassName
*
* @return string
*/
public function dataClass() {
return $this->dataClass;
}
/**
* Clone this object
* When cloning this object, clone the dataQuery object as well
*/
function __clone() {
public function __clone() {
$this->dataQuery = clone $this->dataQuery;
}
/**
* Return the internal {@link DataQuery} object for direct manipulation
*
* @return DataQuery
*/
public function dataQuery() {
return $this->dataQuery;
}
/**
* Returns the SQL query that will be used to get this DataList's records. Good for debugging. :-)
*
* @return SQLQuery
*/
public function sql() {
return $this->dataQuery->query()->sql();
@ -71,6 +98,7 @@ class DataList extends ViewableData implements SS_List {
* Add a WHERE clause to the query.
*
* @param string $filter
* @return DataList
*/
public function where($filter) {
$this->dataQuery->where($filter);
@ -79,6 +107,10 @@ class DataList extends ViewableData implements SS_List {
/**
* Set the sort order of this data list
*
* @param string $sort
* @param string $direction
* @return DataList
*/
public function sort($sort, $direction = "ASC") {
if($direction && strtoupper($direction) != 'ASC') $sort = "$sort $direction";
@ -88,13 +120,20 @@ class DataList extends ViewableData implements SS_List {
/**
* Returns true if this DataList can be sorted by the given field.
*
* @param string $fieldName
* @return boolean
*/
public function canSortBy($field) {
return $this->dataQuery()->query()->canSortBy($field);
public function canSortBy($fieldName) {
return $this->dataQuery()->query()->canSortBy($fieldName);
}
/**
* Add an join clause to this data list's query.
*
* @param type $join
* @return DataList
* @deprecated 3.0
*/
public function join($join) {
Deprecation::notice('3.0', 'Use innerJoin() or leftJoin() instead.');
@ -104,6 +143,8 @@ class DataList extends ViewableData implements SS_List {
/**
* Restrict the records returned in this query by a limit clause
*
* @param string $limit
*/
public function limit($limit) {
$this->dataQuery->limit($limit);
@ -112,6 +153,11 @@ class DataList extends ViewableData implements SS_List {
/**
* Add an inner join clause to this data list's query.
*
* @param string $table
* @param string $onClause
* @param string $alias - if you want this table to be aliased under another name
* @return DataList
*/
public function innerJoin($table, $onClause, $alias = null) {
$this->dataQuery->innerJoin($table, $onClause, $alias);
@ -120,6 +166,11 @@ class DataList extends ViewableData implements SS_List {
/**
* Add an left join clause to this data list's query.
*
* @param string $table
* @param string $onClause
* @param string $alias - if you want this table to be aliased under another name
* @return DataList
*/
public function leftJoin($table, $onClause, $alias = null) {
$this->dataQuery->leftJoin($table, $onClause, $alias);
@ -142,10 +193,14 @@ class DataList extends ViewableData implements SS_List {
return $results;
}
/**
* Return this list as an array and every object it as an sub array as well
*
* @return type
*/
public function toNestedArray() {
$result = array();
foreach ($this as $item) {
foreach($this as $item) {
$result[] = $item->toMap();
}
@ -161,22 +216,37 @@ class DataList extends ViewableData implements SS_List {
return $val;
}
/**
* Returns a map of this list
*
* @param string $keyField - the 'key' field of the result array
* @param string $titleField - the value field of the result array
* @return SS_Map
*/
public function map($keyField = 'ID', $titleField = 'Title') {
return new SS_Map($this, $keyField, $titleField);
}
/**
* Create a data object from the given SQL row
* Create a DataObject from the given SQL row
*
* @param array $row
* @return DataObject
*/
protected function createDataObject($row) {
$defaultClass = $this->dataClass;
// Failover from RecordClassName to ClassName
if(empty($row['RecordClassName'])) $row['RecordClassName'] = $row['ClassName'];
if(empty($row['RecordClassName'])) {
$row['RecordClassName'] = $row['ClassName'];
}
// Instantiate the class mentioned in RecordClassName only if it exists, otherwise default to $this->dataClass
if(class_exists($row['RecordClassName'])) $item = new $row['RecordClassName']($row, false, $this->model);
else $item = new $defaultClass($row, false, $this->model);
if(class_exists($row['RecordClassName'])) {
$item = new $row['RecordClassName']($row, false, $this->model);
} else {
$item = new $defaultClass($row, false, $this->model);
}
return $item;
}
@ -184,6 +254,7 @@ class DataList extends ViewableData implements SS_List {
/**
* Returns an Iterator for this DataList.
* This function allows you to use DataLists in foreach loops
*
* @return ArrayIterator
*/
public function getIterator() {
@ -192,44 +263,60 @@ class DataList extends ViewableData implements SS_List {
/**
* Return the number of items in this DataList
*
* @return int
*/
function count() {
public function count() {
return $this->dataQuery->count();
}
/**
* Return the maximum value of the given field in this DataList
*
* @param string $fieldName
* @return mixed
*/
function Max($field) {
return $this->dataQuery->max($field);
public function max($fieldName) {
return $this->dataQuery->max($fieldName);
}
/**
* Return the minimum value of the given field in this DataList
*
* @param string $fieldName
* @return mixed
*/
function Min($field) {
return $this->dataQuery->min($field);
public function min($fieldName) {
return $this->dataQuery->min($fieldName);
}
/**
* Return the average value of the given field in this DataList
*
* @param string $fieldName
* @return mixed
*/
function Avg($field) {
return $this->dataQuery->avg($field);
public function avg($fieldName) {
return $this->dataQuery->avg($fieldName);
}
/**
* Return the sum of the values of the given field in this DataList
*
* @param string $fieldName
* @return mixed
*/
function Sum($field) {
return $this->dataQuery->sum($field);
public function sum($fieldName) {
return $this->dataQuery->sum($fieldName);
}
/**
* Returns the first item in this DataList
*
* @return DataObject
*/
function First() {
public function first() {
foreach($this->dataQuery->firstRow()->execute() as $row) {
return $this->createDataObject($row);
}
@ -237,8 +324,10 @@ class DataList extends ViewableData implements SS_List {
/**
* Returns the last item in this DataList
*
* @return DataObject
*/
function Last() {
public function last() {
foreach($this->dataQuery->lastRow()->execute() as $row) {
return $this->createDataObject($row);
}
@ -246,20 +335,30 @@ class DataList extends ViewableData implements SS_List {
/**
* Returns true if this DataList has items
*
* @return bool
*/
function exists() {
public function exists() {
return $this->count() > 0;
}
/**
* Get a sub-range of this dataobjectset as an array
*
* @param int $offset
* @param int $length
* @return DataList
*/
public function getRange($offset, $length) {
return $this->limit(array('start' => $offset, 'limit' => $length));
}
/**
* Find an element of this DataList where the given key = value
* Find the first DataObject of this DataList where the given key = value
*
* @param string $key
* @param string $value
* @return DataObject|null
*/
public function find($key, $value) {
$clone = clone $this;
@ -274,19 +373,24 @@ class DataList extends ViewableData implements SS_List {
return $clone->where("$SQL_col = '" . Convert::raw2sql($value) . "'")->First();
}
/**
* Filter this list to only contain the given IDs
* Filter this list to only contain the given Primary IDs
*
* @param array $ids
* @return DataList
*/
public function byIDs(array $ids) {
$baseClass = ClassInfo::baseDataClass($this->dataClass);
$this->where("\"$baseClass\".\"ID\" IN (" . implode(',', $ids) .")");
return $this;
}
/**
* Return the item of the given ID
* Return the first DataObject with the given ID
*
* @param int $id
* @return DataObject
*/
public function byID($id) {
$baseClass = ClassInfo::baseDataClass($this->dataClass);
@ -294,21 +398,24 @@ class DataList extends ViewableData implements SS_List {
}
/**
* Return a single column from this DataList.
* @param $colNum The DataObject field to return.
* Returns an array of a single field value for all items in the list.
*
* @param string $colName
* @return array
*/
function column($colName = "ID") {
public function column($colName = "ID") {
return $this->dataQuery->column($colName);
}
// Member altering methods
/**
* Sets the ComponentSet to be the given ID list.
* Records will be added and deleted as appropriate.
*
* @param array $idList List of IDs.
*/
function setByIDList($idList) {
public function setByIDList($idList) {
$has = array();
// Index current data
@ -334,8 +441,9 @@ class DataList extends ViewableData implements SS_List {
/**
* Returns an array with both the keys and values set to the IDs of the records in this list.
*
*/
function getIDList() {
public function getIDList() {
$ids = $this->column("ID");
return $ids ? array_combine($ids, $ids) : array();
}
@ -348,71 +456,95 @@ class DataList extends ViewableData implements SS_List {
* Example: Get members from all Groups:
*
* DataObject::get("Group")->relation("Members")
*
* @param string $relationName
* @return HasManyList|ManyManyList
*/
function relation($relationName) {
public function relation($relationName) {
$ids = $this->column('ID');
return singleton($this->dataClass)->$relationName()->forForeignID($ids);
}
/**
* Add a number of items to the component set.
*
* @param array $items Items to add, as either DataObjects or IDs.
* @return DataList
*/
function addMany($items) {
public function addMany($items) {
foreach($items as $item) {
$this->add($item);
}
return $this;
}
/**
* Remove the items from this list with the given IDs
*
* @param array $idList
* @return DataList
*/
function removeMany($idList) {
public function removeMany($idList) {
foreach($idList as $id) {
$this->removeByID($id);
}
return $this;
}
/**
* Remove every element in this DataList matching the given $filter.
*
* @param string $filter - a sql type where filter
* @return DataList
*/
function removeByFilter($filter) {
public function removeByFilter($filter) {
foreach($this->where($filter) as $item) {
$this->remove($item);
}
return $this;
}
/**
* Remove every element in this DataList.
*
* @return DataList
*/
function removeAll() {
public function removeAll() {
foreach($this as $item) {
$this->remove($item);
}
return $this;
}
// These methods are overloaded by HasManyList and ManyMany list to perform
// more sophisticated list manipulation
function add($item) {
/**
* This method are overloaded by HasManyList and ManyMany list to perform more sophisticated
* list manipulation
*
* @param type $item
*/
public function add($item) {
// Nothing needs to happen by default
// TO DO: If a filter is given to this data list then
}
/**
* Return a new item to add to this DataList.
*
* @todo This doesn't factor in filters.
*/
function newObject($initialFields = null) {
public function newObject($initialFields = null) {
$class = $this->dataClass;
return new $class($initialFields, false, $this->model);
}
function remove($item) {
// TO DO: Allow for amendment of this behaviour - for exmaple, we can remove an item from
// an "ActiveItems" DataList by chaning the status to inactive.
/**
* Remove this item by deleting it
*
* @param DataClass $item
* @todo Allow for amendment of this behaviour - for example, we can remove an item from
* an "ActiveItems" DataList by chaning the status to inactive.
*/
public function remove($item) {
// By default, we remove an item from a DataList by deleting it.
if($item instanceof $this->dataClass) $item->delete();
@ -420,35 +552,67 @@ class DataList extends ViewableData implements SS_List {
/**
* Remove an item from this DataList by ID
*
* @param int $itemID - The primary ID
*/
function removeByID($itemID) {
public function removeByID($itemID) {
$item = $this->byID($itemID);
if($item) return $item->delete();
}
// Methods that won't function on DataLists
function push($item) {
/**
* This method won't function on DataLists due to the specific query that it represent
*
* @param mixed $item
*/
public function push($item) {
user_error("Can't call DataList::push() because its data comes from a specific query.", E_USER_ERROR);
}
function insertFirst($item) {
/**
* This method won't function on DataLists due to the specific query that it represent
*
* @param mixed $item
*/
public function insertFirst($item) {
user_error("Can't call DataList::insertFirst() because its data comes from a specific query.", E_USER_ERROR);
}
function shift() {
/**
* This method won't function on DataLists due to the specific query that it represent
*
*/
public function shift() {
user_error("Can't call DataList::shift() because its data comes from a specific query.", E_USER_ERROR);
}
function replace() {
/**
* This method won't function on DataLists due to the specific query that it represent
*
*/
public function replace() {
user_error("Can't call DataList::replace() because its data comes from a specific query.", E_USER_ERROR);
}
function merge() {
/**
* This method won't function on DataLists due to the specific query that it represent
*
*/
public function merge() {
user_error("Can't call DataList::merge() because its data comes from a specific query.", E_USER_ERROR);
}
function removeDuplicates() {
/**
* This method won't function on DataLists due to the specific query that it represent
*
*/
public function removeDuplicates() {
user_error("Can't call DataList::removeDuplicates() because its data comes from a specific query.", E_USER_ERROR);
}
/**
* Necessary for interface ArrayAccess. Returns whether an item with $key exists
* Returns whether an item with $key exists
*
* @param mixed $key
* @return bool
*/
@ -457,7 +621,8 @@ class DataList extends ViewableData implements SS_List {
}
/**
* Necessary for interface ArrayAccess. Returns item stored in array with index $key
* Returns item stored in list with index $key
*
* @param mixed $key
* @return DataObject
*/
@ -466,20 +631,22 @@ class DataList extends ViewableData implements SS_List {
}
/**
* Necessary for interface ArrayAccess. Set an item with the key in $key
* Set an item with the key in $key
*
* @param mixed $key
* @param mixed $value
*/
public function offsetSet($key, $value) {
throw new Exception("Can't alter items in a DataList using array-access");
user_error("Can't alter items in a DataList using array-access", E_USER_ERROR);
}
/**
* Necessary for interface ArrayAccess. Unset an item with the key in $key
* Unset an item with the key in $key
*
* @param mixed $key
*/
public function offsetUnset($key) {
throw new Exception("Can't alter items in a DataList using array-access");
user_error("Can't alter items in a DataList using array-access", E_USER_ERROR);
}
}

View File

@ -82,7 +82,7 @@ interface SS_List extends ArrayAccess, Countable, IteratorAggregate {
/**
* Returns an array of a single field value for all items in the list.
*
* @param string $field
* @param string $colName
* @return array
*/
public function column($colName = "ID");