StrictArray
extends StrictCollection
in package
implements
Iterator
Uses
IteratorTrait
A type-sensitive, traversable array.
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
- Iterator
Methods
- __construct() : void
- Create a type-sensitive collection of items.
- add() : void
- Add/insert a item at the specified index.
- bottom() : AllowedType
- Peek at the first item of the array.
- clear() : void
- Clear the collection of any items.
- count() : int<0, max>
- Count the data items.
- current() : TValue|null
- Return the current item.
- get() : AllowedType
- Get the item at the specified index.
- getAllowedTypes() : array<string|int, string>
- 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.
- key() : string|int|null
- Return the current key.
- next() : void
- Move forward to next item.
- offsetExists() : bool
- Check if the specified offset exists.
- offsetGet() : TValue|null
- Retrieve data at the specified offset.
- offsetSet() : void
- Assign a value to the specified offset.
- offsetUnset() : void
- Unset the specified offset.
- pop() : AllowedType
- Pop the item from the end of the array.
- prev() : void
- Move back to previous item.
- push() : void
- Push an item at the end of the array.
- remove() : void
- Remove an item from the collection.
- rewind() : void
- Rewind the iterator to the first item.
- serialize() : string
- Get string representation of $this.
- set() : void
- Set an item at the specified index.
- setAllowedTypes() : void
- Set allowed data types.
- shift() : AllowedType
- Shift the item from the beginning of the array.
- toArray() : array<string|int, AllowedType>
- Return array representation of collection.
- top() : AllowedType
- Peek at the last item of the array.
- toStrictList() : StrictList<string|int, AllowedType>
- Turn collection into a type-sensitive list.
- unserialize() : void
- Restore $this from string representation.
- unshift() : void
- Prepend the array with an item.
- valid() : bool
- Check if current position is valid.
Methods
__construct()
Create a type-sensitive collection of items.
public
__construct([array<string|int, string> $allowedTypes = [] ]) : void
Parameters
- $allowedTypes : array<string|int, string> = []
-
Allowed data types of items (optional)
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"
Tags
add()
Add/insert a item at the specified index.
public
add(string|int $offset, AllowedType $value) : void
Parameters
- $offset : string|int
-
The item's index
- $value : AllowedType
-
The item
Tags
bottom()
Peek at the first item of the array.
public
bottom() : AllowedType
Tags
Return values
AllowedType —The first item of the array
clear()
Clear the collection of any items.
public
clear() : void
count()
Count the data items.
public
count() : int<0, max>
Return values
int<0, max> —The number of data items
current()
Return the current item.
public
current() : TValue|null
Return values
TValue|null —The current item or NULL if invalid
get()
Get the item at the specified index.
public
get(string|int $offset) : AllowedType
Parameters
- $offset : string|int
-
The item's index
Tags
Return values
AllowedType —The item
getAllowedTypes()
Get allowed data types.
public
getAllowedTypes() : array<string|int, string>
Return values
array<string|int, string> —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
Parameters
- $type : string
-
The type to check
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
key()
Return the current key.
public
key() : string|int|null
Return values
string|int|null —The current key or NULL if invalid
next()
Move forward to next item.
public
next() : void
offsetExists()
Check if the specified offset exists.
public
offsetExists(string|int $offset) : bool
Parameters
- $offset : string|int
-
The offset to check for
Return values
bool —Whether the offset exists
offsetGet()
Retrieve data at the specified offset.
public
offsetGet(string|int $offset) : TValue|null
Parameters
- $offset : string|int
-
The offset to retrieve
Return values
TValue|null —The value at the offset or NULL if invalid
offsetSet()
Assign a value to the specified offset.
public
offsetSet(string|int|null $offset, TValue $value) : void
Parameters
- $offset : string|int|null
-
The offset to assign to or NULL to append
- $value : TValue
-
The value to set
offsetUnset()
Unset the specified offset.
public
offsetUnset(string|int $offset) : void
Parameters
- $offset : string|int
-
The offset to unset
pop()
Pop the item from the end of the array.
public
pop() : AllowedType
Tags
Return values
AllowedType —The last item of the array
prev()
Move back to previous item.
public
prev() : void
push()
Push an item at the end of the array.
public
push(AllowedType $value) : void
Parameters
- $value : AllowedType
-
The item to push
Tags
remove()
Remove an item from the collection.
public
remove(string|int $offset) : void
Parameters
- $offset : string|int
-
The item's key
Tags
rewind()
Rewind the iterator to the first item.
public
rewind() : void
serialize()
Get string representation of $this.
public
serialize() : string
Return values
string —The string representation
set()
Set an item at the specified index.
public
set(string|int $offset, AllowedType $value) : void
Parameters
- $offset : string|int
-
The item's index
- $value : AllowedType
-
The item
Tags
setAllowedTypes()
Set allowed data types.
public
setAllowedTypes([array<string|int, string> $allowedTypes = [] ]) : void
Parameters
- $allowedTypes : array<string|int, string> = []
-
Allowed data types
Tags
shift()
Shift the item from the beginning of the array.
public
shift() : AllowedType
Tags
Return values
AllowedType —The first item of the array
toArray()
Return array representation of collection.
public
toArray() : array<string|int, AllowedType>
Return values
array<string|int, AllowedType> —Array of collection items
top()
Peek at the last item of the array.
public
top() : AllowedType
Tags
Return values
AllowedType —The last item of the array
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
unserialize()
Restore $this from string representation.
public
unserialize(string $data) : void
Parameters
- $data : string
-
The string representation
unshift()
Prepend the array with an item.
public
unshift(AllowedType $value) : void
Parameters
- $value : AllowedType
-
The item to unshift
Tags
valid()
Check if current position is valid.
public
valid() : bool
Return values
bool —Whether the current position is valid