Collections
FEAST contains two collection classes for simulating generics. Each of these classes manage an underlying array. While they both share some methods, they each also have distinct methods.
Collection Trait
The \Feast\Traits\Collection
trait contains methods that are usable on both collection classes.
Methods:
-
getValues
- This gets the underlying array values from the collection. -
implode
- This method will return a string calling PHP's native implode on the underlying array with the specified separator. If the underlying array is an object, pass in the key you wish to implode on as the second parameter. Note that implode does not operate on array collections, -
sort
- This method sorts the collection based on a\Feast\Enum\CollectionSort
sort flag. It returns an array and takes the following parameters.-
sortType
- aCollectionSort
enum value (int backed, use the constant to ensure forwards compatibility with the next version of FEAST). -
modifyOriginal
- a boolean flag for whether to modify the stored collection values or only return the new sorted array. -
sortOptions
- a bitwise int ofSORT_
flags
-
-
objectSort
- This method allows sorting a collection of a named class by one or more keys. It returns an array and takes the following parameters.-
key
- a string or array of strings to sort on. If it is an array, it is sorted on them in order. -
sortType
- aCollectionSort
enum value (int backed, use the constant to ensure forwards compatibility with the next version of FEAST). -
modifyOriginal
- a boolean flag for whether to modify the stored collection values or only return the new sorted array.
-
-
shuffle
- This method shuffles the values of the correction. It takes a boolean flag for whether to modify the original as its only parameter and returns an array. -
toArray
- This method gets the underlying array from the collection. -
isEmpty
- Returns whether the collection is empty. -
clear
- Empties the collection -
contains
- This method takes a value to check against, and a boolean flag for using strict comparison. Returns true if the value is found. -
containsAll
- This method takes an array of values to check against, and a boolean flag for using strict comparison. Returns true if all the values are found. -
indexOf
- This method takes a value to check against, and a boolean flag for using strict comparison. Returns the first occurrence if found. -
lastIndexOf
- This method takes a value to check against, and a boolean flag for using strict comparison. Returns the last occurrence if found. -
size
- Returns the size of the collection (count of the underlying array). -
remove
- This method takes a value to check against, and a boolean flag for using strict comparison. Removes all matching items. -
removeAll
- This method takes an array of values to check against, and a boolean flag for using strict comparison. Removes all matching items. -
pop
- This method pops the last element off the underlying collection and returns it. -
shift
- This method shifts the first element off the underlying collection and returns it. -
first
- This method returns the first item from the collection. -
getType
- This method returns what the valid value type is for the collection. Can be any scalar type, array, object, any class, or mixed. -
map
- This method callsarray_map
with the underlying collection of items as the first parameter. It takes the same parameters as array_map other than the initial array. -
reduce
- This method callsarray_reduce
with the underlying collection of items as the first parameter. It takes the same parameters as array_reduce other than the initial array. -
walk
- This method callsarray_walk
with the underlying collection of items as the first parameter. It takes the same parameters as array_walk other than the initial array. -
filter
- This method callsarray_filter
with the underlying collection of items as the first parameter. It takes a callback and an additional int mode flag ( see array_filter). Additionally, it takes a boolean flag to specify whether to update the underlying collection.
In addition, all methods in the PHP built in ArrayAccess interface are available.
Collection List
The \Feast\Collection\CollectionList
class is used to manage a collection by key => value mapping. This collection
type allows duplicates.
The constructor takes the type, an array of values, and a boolean flag for if the values are pre-validated
Methods on CollectionList
-
add
- This method takes a key and value to add to the collection. -
addAll
- This method takes an array of key=>value pairs to add to the collection. -
removeByKey
- This method removes an item from the collection by key. -
get
- This method retrieves an item from the collection by key.
Set
The \Feast\Collection\Set
class is used to manage a collection of unique values. Duplicates are ignored.
The constructor takes the type, an array of values, a boolean flag for whether to perform a strict match when adding items to the collection, and a boolean flag for if the values are pre-validated.
Methods on Set
-
add
- This method takes a value to add to the collection. -
addAll
- This method takes an array and adds all the values to the collection. -
merge
- This method takes anotherSet
as the parameter and merges all values in.
Mathematical functions on Set
Set contains mathematical functions that behave in 2 different ways depending on if the collection is a named class type, or an int/float set. All math functions on a named class type take a key as the only parameter and searches the collection objects for that key and performs the operation on the corresponding values.
-
min
- This method returns the minimum value from the collection. -
max
- This method returns the maximum value from the collection. -
average
- This method returns the average value from the collection. -
sum
- This method returns the sum of the collection. -
product
- This method returns the product. Note: as witharray_product
this method will return1
on an empty collection.
Powered by FEAST Framework
See this project at https://github.com/feastframework/documentation