ITraversable
The ITraversable
interface represents a range of values that are able to be queried upon.
This is base interface for all query objects, it supports the following methods:
Queries
where
- Filters the values according to the supplied predicateorderBy[Ascending|Descending]->thenBy[Ascending|Descending]...
- Orders the values according the supplied order functions and direction(s)skip
- Skip the supplied amount of valuestake
- Limits the amount of values by the supplied amountslice
- Retrieve a slice of values according to the specified offset and amountindexBy
- Index the values according to the supplied mapping functionkeys
- Gets the keys as the values.reindex
- Indexes the values according to their 0-based positiongroupBy
- Groups the values according the supplied grouping functionjoin->on/onEquality[->withDefault]->to
- Matches the values with the supplied values according to the supplied filter then maps the results into as according to the supplied function.groupJoin->on/onEquality[->withDefault]->to
- Matches the values with the supplied values according to the supplied filter, groups the results and then maps into as according to the supplied function.unique
- Only return unique valuesselect
- Map the values according to the supplied functionselectMany
- Map the values according to the supplied function and merge the resultsfirst
- Returns the first value or null if emptylast
- Returns the last value or null if emptycontains
- Returns if the supplied value is present in the valuesoffsetGet
- Returns a value at the supplied indexoffsetExists
- Whether a value exists for the supplied index
Set/List Operations
append
- All values present in either the original or supplied valueswhereIn
- All values present in both the original and supplied valuesexcept
- All values present in the original but not in the supplied valuesunion
- Unique values present in either the original or supplied valuesintersect
- Unique values present in both the original and supplied valuesdifference
- Unique values present in the original but not in the supplied values
Aggregates
count
- The amount of elementsisEmpty
- Whether there are no elementsaggregate
- Aggregates the values according to the suppliedmaximum
- The maximum valueminimum
- The minimum valuesum
- The sum of all the valuesaverage
- The average of all the valuesall
- Whether all the values evaluate to trueany
- Whether any of the values evaluate to trueimplode
- Concatenates the values separated by the supplied delimiter
Other
getIterator
- Gets the elements as an iterator with non scalar keys mapped to integersgetTrueIterator
- Gets the elements as an iteratorasArray
- Gets the elements as an array. Non scalar keys will be mapped to integersasTraversable
- Gets the elements as aITraversable
asCollection
- Gets the elements as aICollection
ICollection
The ICollection
interface represents a queryable range of values that are also mutable,
they can be manipulated and altered using the additional methods:
apply
- Walks the values with the supplied functiongroupJoin->on/onEquality[->withDefault]->apply
- Matches the values with the supplied values according to the supplied filter then walks the results using to the supplied function.addRange
- Adds a list of values to the collectionremove
- Removes all occurrences of a value from the collectionremoveRange
- Removes all occurrences of a set of values from the collectionremoveWhere
- Removes the values according to the supplied predicate functionclear
- Removes all the values from the collection.offsetSet
- Sets a value to supplied indexoffsetUnset
- Removes any value at the supplied index
IQueryable
The IQueryable
interface represents is another version of the ITraversable
interface.
This provides the same API of the ITraversable
but through the use of a IQueryProvider
,
it supports querying external data sources.
IRepository
The IRepository
interface represents is another version of the ICollection
interface.
This provides the same API of the ICollection
but through the use of a IRepositoryProvider
,
it supports querying and mutating external data sources.
Limitations
Within a query, one should not use control structures such as
if, switch, goto, while, foreach,...
, these are not classified as valid query expressions and cannot be used with external data sources.One should not define multiple functions on the same line. An exception will be thrown for the following as it is ambiguous to determine the correct function:
$queryable->where(function ($i) { return $i > 50; })->where(function ($i) { return $i !== 70; });
Standard Classes
Along side the API, PINQ comes the set of standard implementations for each of the interfaces. If you need to add and custom functionality to the PINQ API, you should extend these classes as they contain the correct and tested implementation for the standard API.
The following classes are provided:
Traversable implements ITraversable
Collection implements ICollection
Queryable implements IQueryable
Repository implements IRepository