StrictCollection
in package
implements
ArrayAccess, Countable
uses
ArrayAccessTrait, CountableTrait, TypeChecker
A type-sensitive, unsorted collection.
Holds items as key/value pairs where keys have to be valid array keys while values can be of any type. To restrict allowed data types for items, provide the constructor with an array of atomic types or fully qualified class names.
Internally it holds the items in the protected $_data array.
Tags
Table of Contents
Interfaces
- ArrayAccess
- Countable
Methods
- __construct() : mixed
- Create a type-sensitive collection of items.
- add() : void
- Add/insert a item at the specified index.
- clear() : void
- Clear the collection of any items.
- count() : int
- Count the data items.
- fromArray() : TAllowedType>
- Create a type-sensitive collection from an array of items.
- get() : AllowedType
- Get the item at the specified index.
- getAllowedTypes() : AllowedTypes
- Get allowed data types.
- hasAllowedType() : bool
- Check if a value's data type is allowed.
- isAllowedType() : bool
- Check if a data type is allowed.
- isEmpty() : bool
- Check if collection is empty.
- isList() : bool
- Check if this collection can be considered a list.
- offsetExists() : bool
- Check if the specified offset exists.
- offsetGet() : TValue|null
- Retrieve data at the specified offset.
- offsetSet() : void
- Set the item at the specified offset.
- offsetUnset() : void
- Unset the specified offset.
- remove() : void
- Remove an item from the collection.
- set() : void
- Set an item at the specified index.
- setAllowedTypes() : void
- Set allowed data types.
- toArray() : array<string|int, mixed>
- Return array representation of collection.
- toStrictList() : StrictList<string|int, AllowedType>
- Turn collection into a type-sensitive list.
Methods
__construct()
Create a type-sensitive collection of items.
public
__construct([array<string|int, string|AllowedType>> $allowedTypes = [] ]) : mixed
Parameters
- $allowedTypes : array<string|int, string|AllowedType>> = []
-
Allowed data types of items
If empty, all types are allowed. Possible values are:
- "array"
- "bool"
- "callable"
- "countable"
- "float" or "double"
- "int" or "integer" or "long"
- "iterable"
- "null"
- "numeric"
- "object" or FQCN
- "resource"
- "scalar"
- "string"
- "mixed" Union and nullable types like "int|string" or "?int" are supported, too.
Tags
add()
Add/insert a item at the specified index.
public
add(Index $offset, AllowedType $value) : void
Parameters
- $offset : Index
-
The item's index
- $value : AllowedType
-
The item
Tags
clear()
Clear the collection of any items.
public
clear() : void
count()
Count the data items.
public
count() : int
Return values
intfromArray()
Create a type-sensitive collection from an array of items.
public
static fromArray(array<string|int, mixed> $items[, array<string|int, string|TAllowedType>> $allowedTypes = [] ]) : TAllowedType>
Keys of $items are preserved.
Parameters
- $items : array<string|int, mixed>
- $allowedTypes : array<string|int, string|TAllowedType>> = []
-
Allowed data types of items (see
__construct())
Tags
Return values
TAllowedType> —The new collection
get()
Get the item at the specified index.
public
get(Index $offset) : AllowedType
Parameters
- $offset : Index
-
The item's index
Tags
Return values
AllowedType —The item
getAllowedTypes()
Get allowed data types.
public
getAllowedTypes() : AllowedTypes
Return values
AllowedTypes —The list of allowed data types
hasAllowedType()
Check if a value's data type is allowed.
public
hasAllowedType(mixed $value) : bool
Parameters
- $value : mixed
-
The value to check
Return values
bool —Whether the value's data type is allowed
isAllowedType()
Check if a data type is allowed.
public
isAllowedType(string $type) : bool
For union and nullable types, all of their types have to be allowed.
Parameters
- $type : string
-
The type to check
Tags
Return values
bool —Whether the data type is allowed
isEmpty()
Check if collection is empty.
public
isEmpty() : bool
Return values
bool —Whether the collection contains no items
isList()
Check if this collection can be considered a list.
public
isList() : bool
It is considered a list if all keys are consecutive integers starting
from 0.
Tags
Return values
bool —Whether the collection is a list
offsetExists()
Check if the specified offset exists.
public
offsetExists(TKey $offset) : bool
Parameters
- $offset : TKey
-
The offset to check for
Return values
bool —Whether the offset exists
offsetGet()
Retrieve data at the specified offset.
public
offsetGet(TKey $offset) : TValue|null
Parameters
- $offset : TKey
-
The offset to retrieve
Return values
TValue|null —The value at the offset or NULL if invalid
offsetSet()
Set the item at the specified offset.
public
offsetSet(Index|null $offset, AllowedType $value) : void
Parameters
- $offset : Index|null
-
The offset being set
- $value : AllowedType
-
The new item for the offset
Tags
Attributes
- #[Override]
offsetUnset()
Unset the specified offset.
public
offsetUnset(TKey $offset) : void
Parameters
- $offset : TKey
-
The offset to unset
remove()
Remove an item from the collection.
public
remove(Index $offset) : void
Parameters
- $offset : Index
-
The item's key
Tags
set()
Set an item at the specified index.
public
set(Index $offset, AllowedType $value) : void
Parameters
- $offset : Index
-
The item's index
- $value : AllowedType
-
The item
Tags
setAllowedTypes()
Set allowed data types.
public
setAllowedTypes([AllowedTypes $allowedTypes = [] ]) : void
Parameters
- $allowedTypes : AllowedTypes = []
-
Allowed data types
Tags
toArray()
Return array representation of collection.
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>toStrictList()
Turn collection into a type-sensitive list.
public
toStrictList() : StrictList<string|int, AllowedType>
Tags
Return values
StrictList<string|int, AllowedType> —A type-sensitive list of the collection's items