MNT Use generics with DataList

This commit is contained in:
Guy Sartorelli 2023-11-15 11:45:41 +13:00
parent f22282acb2
commit 7d911d0a80
No known key found for this signature in database
GPG Key ID: F313E3B9504D496A
7 changed files with 129 additions and 92 deletions

View File

@ -35,6 +35,12 @@ use SilverStripe\ORM\Filters\SearchFilterable;
* - removeAll
*
* Subclasses of DataList may add other methods that have the same effect.
*
* @template T of DataObject
* @implements SS_List<T>
* @implements Filterable<T>
* @implements Sortable<T>
* @implements Limitable<T>
*/
class DataList extends ViewableData implements SS_List, Filterable, Sortable, Limitable
{
@ -49,7 +55,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
/**
* The DataObject class name that this data list is querying
*
* @var string
* @var class-string<T>
*/
protected $dataClass;
@ -63,7 +69,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
/**
* A cached Query to save repeated database calls. {@see DataList::getTemplateIteratorCount()}
*
* @var SilverStripe\ORM\Connect\Query
* @var Query
*/
protected $finalisedQuery;
@ -89,7 +95,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Create a new DataList.
* No querying is done on construction, but the initial query schema is set up.
*
* @param string $dataClass - The DataObject class to query.
* @param class-string<T> $dataClass - The DataObject class to query.
*/
public function __construct($dataClass)
{
@ -102,7 +108,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
/**
* Get the dataClass name for this DataList, ie the DataObject ClassName
*
* @return string
* @return class-string<T>
*/
public function dataClass()
{
@ -150,7 +156,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* alterDataQuery
*
* @param callable $callback
* @return static
* @return static<T>
* @throws Exception
*/
public function alterDataQuery($callback)
@ -187,7 +193,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Return a new DataList instance with the underlying {@link DataQuery} object changed
*
* @param DataQuery $dataQuery
* @return static
* @return static<T>
*/
public function setDataQuery(DataQuery $dataQuery)
{
@ -201,7 +207,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* @param string|array $keyOrArray Either the single key to set, or an array of key value pairs to set
* @param mixed $val If $keyOrArray is not an array, this is the value to set
* @return static
* @return static<T>
*/
public function setDataQueryParam($keyOrArray, $val = null)
{
@ -242,7 +248,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or
* paramaterised queries
* @return static
* @return static<T>
*/
public function where($filter)
{
@ -264,7 +270,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or
* paramaterised queries
* @return static
* @return static<T>
*/
public function whereAny($filter)
{
@ -336,7 +342,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Return a new DataList instance with distinct records or not
*
* @param bool $value
* @return static
* @return static<T>
*/
public function distinct($value)
{
@ -446,6 +452,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* This method accepts raw SQL so could be vulnerable to SQL injection attacks if used incorrectly,
* it's preferable to use sort() instead which does not allow raw SQL
*
* @return static<T>
*/
public function orderBy(string $orderBy): static
{
@ -473,7 +481,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* ->filter('Field:not', null) will generate '"Field" IS NOT NULL'
*
* @param string|array Escaped SQL statement. If passed as array, all keys and values will be escaped internally
* @return $this
* @return static<T>
*/
public function filter()
{
@ -499,7 +507,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Return a new instance of the list with an added filter
*
* @param array $filterArray
* @return $this
* @return static<T>
*/
public function addFilter($filterArray)
{
@ -535,7 +543,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* // SQL: WHERE (("Name" IN ('bob', 'phil')) OR ("Age" IN ('21', '43'))
*
* @param string|array See {@link filter()}
* @return static
* @return static<T>
*/
public function filterAny()
{
@ -580,7 +588,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* @example $list = $list->filterByCallback(function($item, $list) { return $item->Age == 9; })
* @param callable $callback
* @return ArrayList (this may change in future implementations)
* @return ArrayList<T> (this may change in future implementations)
*/
public function filterByCallback($callback)
{
@ -620,7 +628,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* @param string $columnName Quoted column name (by reference)
* @param bool $linearOnly Set to true to restrict to linear relations only. Set this
* if this relation will be used for sorting, and should not include duplicate rows.
* @return $this DataList with this relation applied
* @return static<T> DataList with this relation applied
*/
public function applyRelation($field, &$columnName = null, $linearOnly = false)
{
@ -678,7 +686,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* @param string|array
* @param string [optional]
*
* @return $this
* @return static<T>
*/
public function exclude()
{
@ -718,7 +726,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* @param string|array
* @param string [optional]
*
* @return $this
* @return static<T>
*/
public function excludeAny()
{
@ -747,8 +755,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* The $list passed needs to contain the same dataclass as $this
*
* @param DataList $list
* @return static
* @param DataList<T> $list
* @return static<T>
* @throws InvalidArgumentException
*/
public function subtract(DataList $list)
@ -772,7 +780,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* will cause the query to appear first. The default is 20, and joins created automatically by the
* ORM have a value of 10.
* @param array $parameters Any additional parameters if the join is a parameterised subquery
* @return static
* @return static<T>
*/
public function innerJoin($table, $onClause, $alias = null, $order = 20, $parameters = [])
{
@ -791,7 +799,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* will cause the query to appear first. The default is 20, and joins created automatically by the
* ORM have a value of 10.
* @param array $parameters Any additional parameters if the join is a parameterised subquery
* @return static
* @return static<T>
*/
public function leftJoin($table, $onClause, $alias = null, $order = 20, $parameters = [])
{
@ -810,7 +818,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* will cause the query to appear first. The default is 20, and joins created automatically by the
* ORM have a value of 10.
* @param array $parameters Any additional parameters if the join is a parameterised subquery
* @return static
* @return static<T>
*/
public function rightJoin($table, $onClause, $alias = null, $order = 20, $parameters = [])
{
@ -823,7 +831,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Return an array of the actual items that this DataList contains at this stage.
* This is when the query is actually executed.
*
* @return array
* @return array<T>
*/
public function toArray()
{
@ -856,7 +864,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Walks the list using the specified callback
*
* @param callable $callback
* @return $this
* @return static<T> $this
*/
public function each($callback)
{
@ -894,7 +902,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* If called without $row['ID'] set, then a new object will be created rather than rehydrated.
*
* @param array $row
* @return DataObject
* @return T
*/
public function createDataObject($row)
{
@ -946,6 +954,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
/**
* Returns an Iterator for this DataList.
* This function allows you to use DataLists in foreach loops
* @return iterable<T>
*/
public function getIterator(): Traversable
{
@ -1457,6 +1466,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* IMPORTANT: Calling eagerLoad() will cause any relations on DataObjects to be returned as an EagerLoadedList
* instead of a subclass of DataList such as HasManyList i.e. MyDataObject->MyHasManyRelation() returns an EagerLoadedList
*
* @return static<T>
*/
public function eagerLoad(...$relationChains): static
{
@ -1552,7 +1563,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* The object returned is not cached, unlike {@link DataObject::get_one()}
*
* @return DataObject|null
* @return T|null
*/
public function first()
{
@ -1572,7 +1583,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* The object returned is not cached, unlike {@link DataObject::get_one()}
*
* @return DataObject|null
* @return T|null
*/
public function last()
{
@ -1605,7 +1616,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* @param string $key
* @param string $value
* @return DataObject|null
* @return T|null
*/
public function find($key, $value)
{
@ -1616,7 +1627,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Restrict the columns to fetch into this DataList
*
* @param array $queriedColumns
* @return static
* @return static<T>
*/
public function setQueriedColumns($queriedColumns)
{
@ -1629,7 +1640,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Filter this list to only contain the given Primary IDs
*
* @param array $ids Array of integers
* @return $this
* @return static<T>
*/
public function byIDs($ids)
{
@ -1642,7 +1653,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* The object returned is not cached, unlike {@link DataObject::get_by_id()}
*
* @param int $id
* @return DataObject|null
* @return T|null
*/
public function byID($id)
{
@ -1734,13 +1745,13 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* DataList::Create(\SilverStripe\Security\Group::class)->relation("Members")
*
* @param string $relationName
* @return HasManyList|ManyManyList
* @return RelationList<T>
*/
public function relation($relationName)
{
$ids = $this->column('ID');
$singleton = DataObject::singleton($this->dataClass);
/** @var HasManyList|ManyManyList $relation */
/** @var RelationList $relation */
$relation = $singleton->$relationName($ids);
return $relation;
}
@ -1753,8 +1764,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
/**
* Add a number of items to the component set.
*
* @param array $items Items to add, as either DataObjects or IDs.
* @return $this
* @param array<T> $items Items to add, as either DataObjects or IDs.
* @return static<T> $this
*/
public function addMany($items)
{
@ -1768,7 +1779,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Remove the items from this list with the given IDs
*
* @param array $idList
* @return $this
* @return static<T> $this
*/
public function removeMany($idList)
{
@ -1782,7 +1793,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Remove every element in this DataList matching the given $filter.
*
* @param string|array $filter - a sql type where filter
* @return $this
* @return static<T> $this
*/
public function removeByFilter($filter)
{
@ -1795,7 +1806,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
/**
* Shuffle the datalist using a random function provided by the SQL engine
*
* @return $this
* @return static<T>
*/
public function shuffle()
{
@ -1805,7 +1816,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
/**
* Remove every element in this DataList.
*
* @return $this
* @return static<T> $this
*/
public function removeAll()
{
@ -1819,7 +1830,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* This method are overloaded by HasManyList and ManyMany list to perform more sophisticated
* list manipulation
*
* @param mixed $item
* @param T $item
*/
public function add($item)
{
@ -1831,7 +1842,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Return a new item to add to this DataList.
*
* @param array $initialFields
* @return DataObject
* @return T
*/
public function newObject($initialFields = null)
{
@ -1842,7 +1853,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
/**
* Remove this item by deleting it
*
* @param DataObject $item
* @param T $item
*/
public function remove($item)
{
@ -1867,7 +1878,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
/**
* Reverses a list of items.
*
* @return static
* @return static<T>
*/
public function reverse()
{
@ -1888,6 +1899,8 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
* Returns item stored in list with index $key
*
* The object returned is not cached, unlike {@link DataObject::get_one()}
*
* @return T|null
*/
public function offsetGet(mixed $key): ?DataObject
{
@ -1896,6 +1909,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
/**
* Set an item with the key in $key
*
* @throws BadMethodCallException
*/
public function offsetSet(mixed $key, mixed $value): void
@ -1922,7 +1936,7 @@ class DataList extends ViewableData implements SS_List, Filterable, Sortable, Li
*
* @param int $chunkSize
* @throws InvalidArgumentException If `$chunkSize` has an invalid size.
* @return Generator|DataObject[]
* @return iterable<T>
*/
public function chunkedFetch(int $chunkSize = 1000): iterable
{

View File

@ -716,7 +716,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* or destroy and reinstanciate the record.
*
* @param string $className The new ClassName attribute (a subclass of {@link DataObject})
* @return $this
* @return static $this
*/
public function setClassName($className)
{
@ -742,9 +742,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* there is no record, or the record has no ID. In this case, we do have an ID but
* we still need to repopulate the defaults.
*
* @param string $newClassName The name of the new class
* @template T of DataObject
* @param class-string<T> $newClassName The name of the new class
*
* @return DataObject The new instance of the new class, The exact type will be of the class name provided.
* @return T The new instance of the new class, The exact type will be of the class name provided.
*/
public function newClassInstance($newClassName)
{
@ -963,7 +964,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* Returns the associated database record - in this case, the object itself.
* This is included so that you can call $dataOrController->data() and get a DataObject all the time.
*
* @return DataObject Associated database record
* @return static Associated database record
*/
public function data()
{
@ -1012,7 +1013,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* whitelist the allowed keys.
*
* @param array $data A map of field name to data values to update.
* @return DataObject $this
* @return static $this
*/
public function update($data)
{
@ -1073,7 +1074,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* use the write() method.
*
* @param array $data A map of field name to data values to update.
* @return DataObject $this
* @return static $this
*/
public function castedUpdate($data)
{
@ -1097,7 +1098,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* Caution: Does not delete the merged object.
* Caution: Does now overwrite Created date on the original object.
*
* @param DataObject $rightObj
* @param static $rightObj
* @param string $priority left|right Determines who wins in case of a conflict (optional)
* @param bool $includeRelations Merge any existing relations (optional)
* @param bool $overwriteWithEmpty Overwrite existing left values with empty right values.
@ -1182,7 +1183,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* next write. Existing CHANGE_VALUE or CHANGE_STRICT values
* are preserved.
*
* @return $this
* @return static $this
*/
public function forceChange()
{
@ -1303,7 +1304,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* Called by the constructor when creating new records.
*
* @uses DataExtension::populateDefaults()
* @return DataObject $this
* @return static $this
*/
public function populateDefaults()
{
@ -1654,7 +1655,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
*
* @param bool $recursive Recursively write components
* @param array $skip List of DataObject references to skip
* @return DataObject $this
* @return static $this
*/
public function writeComponents($recursive = false, $skip = [])
{
@ -1888,7 +1889,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
*
* @param string $componentName
* @param DataObject|null $item
* @return $this
* @return static $this
*/
public function setComponent($componentName, $item)
{
@ -2327,7 +2328,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* Generates a SearchContext to be used for building and processing
* a generic search form for properties on this object.
*
* @return SearchContext
* @return SearchContext<static>
*/
public function getDefaultSearchContext()
{
@ -2817,7 +2818,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
*
* @param string $fieldName Name of the field
* @param mixed $val New field value
* @return $this
* @return static $this
*/
public function setField($fieldName, $val)
{
@ -2913,7 +2914,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
*
* @param string $fieldName Name of the field
* @param mixed $value New field value
* @return $this
* @return static $this
*/
public function setCastedField($fieldName, $value)
{
@ -2992,7 +2993,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* See {@link extendedCan()} for a more versatile tri-state permission control.
*
* @param string $perm The permission to be checked, such as 'View'.
* @param Member $member The member whose permissions need checking. Defaults to the currently logged
* @param Member|null $member The member whose permissions need checking. Defaults to the currently logged
* in user.
* @param array $context Additional $context to pass to extendedCan()
*
@ -3038,7 +3039,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* </code>
*
* @param string $methodName Method on the same object, e.g. {@link canEdit()}
* @param Member|int $member
* @param Member|null $member
* @param array $context Optional context
* @return boolean|null
*/
@ -3060,7 +3061,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
}
/**
* @param Member $member
* @param Member|null $member
* @return boolean
*/
public function canView($member = null)
@ -3073,7 +3074,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
}
/**
* @param Member $member
* @param Member|null $member
* @return boolean
*/
public function canEdit($member = null)
@ -3086,7 +3087,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
}
/**
* @param Member $member
* @param Member|null $member
* @return boolean
*/
public function canDelete($member = null)
@ -3099,7 +3100,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
}
/**
* @param Member $member
* @param Member|null $member
* @param array $context Additional context-specific data which might
* affect whether (or where) this object could be created.
* @return boolean
@ -3285,7 +3286,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* Return all objects matching the filter
* sub-classes are automatically selected and included
*
* @param string $callerClass The class of objects to be returned
* @param string|null $callerClass The class of objects to be returned
* @param string|array $filter A filter to be inserted into the WHERE clause.
* Supports parameterised queries. See SQLSelect::addWhere() for syntax examples.
* @param string|array|null $sort Passed to DataList::sort()
@ -3294,7 +3295,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* @param string|array $limit A limit expression to be inserted into the LIMIT clause.
* @param string $containerClass The container class to return the results in.
*
* @return DataList The objects matching the filter, in the class specified by $containerClass
* @return DataList<static> The objects matching the filter, in the class specified by $containerClass
*/
public static function get(
$callerClass = null,
@ -3360,7 +3361,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* @param boolean $cache Use caching
* @param string|array|null $sort Passed to DataList::sort() so that DataList::first() returns the desired item
*
* @return DataObject|null The first item matching the query
* @return static|null The first item matching the query
*/
public static function get_one($callerClass = null, $filter = "", $cache = true, $sort = "")
{
@ -3411,7 +3412,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
*
* @param boolean $persistent When true will also clear persistent data stored in the Cache system.
* When false will just clear session-local cached data
* @return DataObject $this
* @return static $this
*/
public function flushCache($persistent = true)
{
@ -3512,7 +3513,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
/**
* Get the base class for this object
*
* @return string
* @return class-string<DataObject>
*/
public function baseClass()
{
@ -3569,7 +3570,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
/**
* @see $sourceQueryParams
* @param string $key
* @return string
* @return string|null
*/
public function getSourceQueryParam($key)
{
@ -4314,7 +4315,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
*
* @param DataObject $object
* @param string $alias Alias
* @return $this
* @return static $this
*/
public function setJoin(DataObject $object, $alias = null)
{

View File

@ -11,6 +11,9 @@ namespace SilverStripe\ORM;
* @see SS_List
* @see Sortable
* @see Limitable
*
* @template T
* @implements SS_List<T>
*/
interface Filterable extends SS_List
{
@ -33,6 +36,8 @@ interface Filterable extends SS_List
* @example $list = $list->filter(array('Name'=>'bob, 'Age'=>array(21, 43))); // bob with the Age 21 or 43
* @example $list = $list->filter(array('Name'=>array('aziz','bob'), 'Age'=>array(21, 43)));
* // aziz with the age 21 or 43 and bob with the Age 21 or 43
*
* @return static<T>
*/
public function filter();
@ -56,7 +61,7 @@ interface Filterable extends SS_List
* // SQL: WHERE (("Name" IN ('bob', 'phil')) OR ("Age" IN ('21', '43'))
*
* @param string|array See {@link filter()}
* @return static
* @return static<T>
*/
public function filterAny();
@ -70,6 +75,8 @@ interface Filterable extends SS_List
* @example $list = $list->exclude(array('Name'=>'bob, 'Age'=>array(21, 43))); // exclude bob with Age 21 or 43
* @example $list = $list->exclude(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
* // bob age 21 or 43, phil age 21 or 43 would be excluded
*
* @return static<T>
*/
public function exclude();
@ -80,7 +87,7 @@ interface Filterable extends SS_List
*
* @example $list = $list->filterByCallback(function($item, $list) { return $item->Age == 9; })
* @param callable $callback
* @return Filterable
* @return Filterable<T>
*/
public function filterByCallback($callback);
@ -88,7 +95,7 @@ interface Filterable extends SS_List
* Return the first item with the given ID
*
* @param int $id
* @return mixed
* @return T|null
*/
public function byID($id);
@ -96,7 +103,7 @@ interface Filterable extends SS_List
* Filter this list to only contain the given Primary IDs
*
* @param array $ids Array of integers
* @return SS_List
* @return static<T>
*/
public function byIDs($ids);
}

View File

@ -11,6 +11,9 @@ namespace SilverStripe\ORM;
* @see SS_List
* @see Sortable
* @see Filterable
*
* @template T
* @implements SS_List<T>
*/
interface Limitable extends SS_List
{
@ -22,6 +25,7 @@ interface Limitable extends SS_List
*
* If `$length` is null, then no limit is applied. If `$length` is 0, then an empty list is returned.
*
* @return static<T>
* @throws InvalidArgumentException if $length or offset are negative
*/
public function limit(?int $length, int $offset = 0): Limitable;

View File

@ -8,6 +8,10 @@ use IteratorAggregate;
/**
* An interface that a class can implement to be treated as a list container.
*
* @template T
* @extends ArrayAccess<array-key, T>
* @extends IteratorAggregate<array-key, T>
*/
interface SS_List extends ArrayAccess, Countable, IteratorAggregate
{
@ -15,14 +19,14 @@ interface SS_List extends ArrayAccess, Countable, IteratorAggregate
/**
* Returns all the items in the list in an array.
*
* @return array
* @return array<T>
*/
public function toArray();
/**
* Returns the contents of the list as an array of maps.
*
* @return array
* @return array<T>
*/
public function toNestedArray();
@ -30,28 +34,28 @@ interface SS_List extends ArrayAccess, Countable, IteratorAggregate
* Adds an item to the list, making no guarantees about where it will
* appear.
*
* @param mixed $item
* @param T $item
*/
public function add($item);
/**
* Removes an item from the list.
*
* @param mixed $item
* @param T $item
*/
public function remove($item);
/**
* Returns the first item in the list.
*
* @return mixed
* @return T|null
*/
public function first();
/**
* Returns the last item in the list.
*
* @return mixed
* @return T|null
*/
public function last();
@ -71,7 +75,7 @@ interface SS_List extends ArrayAccess, Countable, IteratorAggregate
*
* @param string $key
* @param mixed $value
* @return mixed
* @return T|null
*/
public function find($key, $value);
@ -87,7 +91,7 @@ interface SS_List extends ArrayAccess, Countable, IteratorAggregate
* Walks the list using the specified callback
*
* @param callable $callback
* @return $this
* @return static<T> $this
*/
public function each($callback);
}

View File

@ -41,6 +41,8 @@ use SilverStripe\ORM\DataQuery;
* to include.
*
* @see http://doc.silverstripe.com/doku.php?id=searchcontext
*
* @template T
*/
class SearchContext
{
@ -50,7 +52,7 @@ class SearchContext
* DataObject subclass to which search parameters relate to.
* Also determines as which object each result is provided.
*
* @var string
* @var class-string<T>
*/
protected $modelClass;
@ -83,7 +85,7 @@ class SearchContext
* in the form of a $_REQUEST object.
* CAUTION: All values should be treated as insecure client input.
*
* @param string $modelClass The base {@link DataObject} class that search properties related to.
* @param class-string<T> $modelClass The base {@link DataObject} class that search properties related to.
* Also used to generate a set of result objects based on this class.
* @param FieldList $fields Optional. FormFields mapping to {@link DataObject::$db} properties
* which are to be searched. Derived from modelclass using
@ -132,7 +134,7 @@ class SearchContext
* Falls back to {@link DataObject::$default_sort} if not provided.
* @param array|null|string $limit
* @param DataList $existingQuery
* @return DataList
* @return DataList<T>
* @throws Exception
*/
public function getQuery($searchParams, $sort = false, $limit = false, $existingQuery = null)
@ -326,7 +328,7 @@ class SearchContext
* @param array $searchParams
* @param array|bool|string $sort
* @param array|null|string $limit
* @return DataList
* @return DataList<T>
* @throws Exception
*/
public function getResults($searchParams, $sort = false, $limit = null)
@ -353,7 +355,7 @@ class SearchContext
* Accessor for the filter attached to a named field.
*
* @param string $name
* @return SearchFilter
* @return SearchFilter|null
*/
public function getFilter($name)
{
@ -377,7 +379,7 @@ class SearchContext
/**
* Overwrite the current search context filter map.
*
* @param array $filters
* @param SearchFilter[] $filters
*/
public function setFilters($filters)
{
@ -448,7 +450,7 @@ class SearchContext
* Set search param values
*
* @param array|HTTPRequest $searchParams
* @return $this
* @return static<T> $this
*/
public function setSearchParams($searchParams)
{

View File

@ -11,6 +11,9 @@ namespace SilverStripe\ORM;
* @see SS_List
* @see Filterable
* @see Limitable
*
* @template T
* @extends SS_List<T>
*/
interface Sortable extends SS_List
{
@ -27,11 +30,12 @@ interface Sortable extends SS_List
* Return a new instance of this list that is sorted 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.
*
* @return static
* @example $list = $list->sort('Name'); // default ASC sorting
* @example $list = $list->sort('Name DESC'); // DESC sorting
* @example $list = $list->sort('Name', 'ASC');
* @example $list = $list->sort(array('Name'=>'ASC,'Age'=>'DESC'));
*
* @return static<T>
*/
public function sort();
@ -39,8 +43,9 @@ interface Sortable extends SS_List
/**
* Return a new instance of this list based on reversing the current sort.
*
* @return Sortable
* @example $list = $list->reverse();
*
* @return static<T>
*/
public function reverse();
}