PHP Basics

StrictArray extends StrictCollection
in package
implements ArrayAccess, Countable, IteratorAggregate, Serializable uses IteratorAggregateTrait

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
author

Sebastian Meyer sebastian.meyer@opencultureconsulting.com

template
template

Table of Contents

Interfaces

ArrayAccess
Countable
IteratorAggregate
Serializable

Methods

__construct()  : mixed
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
Count the data items.
get()  : AllowedType
Get the item at the specified index.
getAllowedTypes()  : AllowedTypes
Get allowed data types.
getIterator()  : ArrayIterator<TKey, TValue>
Retrieve an external iterator.
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.
pop()  : AllowedType
Pop the item from the end of the array.
push()  : void
Push an item at the end of the array.
remove()  : void
Remove an item from the collection.
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, mixed>
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.

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"
Tags
throws
InvalidArgumentException

if any value of $allowedTypes is not a string

add()

Add/insert a item at the specified index.

public add(Index $offset, AllowedType $value) : void
APIYes
Parameters
$offset : Index

The item's index

$value : AllowedType

The item

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

bottom()

Peek at the first item of the array.

public bottom() : AllowedType
APIYes
Tags
throws
RuntimeException

if the array is empty

Return values
AllowedType

The first item of the array

clear()

Clear the collection of any items.

public clear() : void
APIYes

count()

Count the data items.

public count() : int
APIYes
Return values
int

get()

Get the item at the specified index.

public get(Index $offset) : AllowedType
APIYes
Parameters
$offset : Index

The item's index

Tags
throws
OutOfRangeException

when $offset is out of bounds

Return values
AllowedType

The item

getAllowedTypes()

Get allowed data types.

public getAllowedTypes() : AllowedTypes
APIYes
Return values
AllowedTypes

The list of allowed data types

getIterator()

Retrieve an external iterator.

public getIterator() : ArrayIterator<TKey, TValue>
APIYes
Return values
ArrayIterator<TKey, TValue>

New iterator for the data array

hasAllowedType()

Check if a value's data type is allowed.

public hasAllowedType(mixed $value) : bool
APIYes
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
APIYes
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
APIYes
Return values
bool

Whether the collection contains no items

isList()

Check if this collection can be considered a list.

public isList() : bool
APIYes

It is considered a list if all keys are consecutive integers starting from 0.

Tags
see
StrictCollection::toStrictList()
phpstan-assert-if-true

static<int, AllowedType> $this

Return values
bool

Whether the collection is a list

offsetExists()

Check if the specified offset exists.

public offsetExists(TKey $offset) : bool
APIYes
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
APIYes
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
APIYes
Parameters
$offset : Index|null

The offset being set

$value : AllowedType

The new item for the offset

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

throws
InvalidArgumentException

if $offset is not a valid array key

Attributes
#[Override]

offsetUnset()

Unset the specified offset.

public offsetUnset(TKey $offset) : void
APIYes
Parameters
$offset : TKey

The offset to unset

pop()

Pop the item from the end of the array.

public pop() : AllowedType
APIYes
Tags
throws
RuntimeException

if the array is empty

Return values
AllowedType

The last item of the array

push()

Push an item at the end of the array.

public push(AllowedType $value) : void
APIYes
Parameters
$value : AllowedType

The item to push

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

remove()

Remove an item from the collection.

public remove(Index $offset) : void
APIYes
Parameters
$offset : Index

The item's key

Tags
throws
OutOfRangeException

when $offset is out of bounds

serialize()

Get string representation of $this.

public serialize() : string
Attributes
#[Override]
Return values
string

The string representation

set()

Set an item at the specified index.

public set(Index $offset, AllowedType $value) : void
APIYes
Parameters
$offset : Index

The item's index

$value : AllowedType

The item

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

setAllowedTypes()

Set allowed data types.

public setAllowedTypes([AllowedTypes $allowedTypes = [] ]) : void
APIYes
Parameters
$allowedTypes : AllowedTypes = []

Allowed data types

Tags
throws
InvalidArgumentException

if any value of $allowedTypes is not a string

shift()

Shift the item from the beginning of the array.

public shift() : AllowedType
APIYes
Tags
throws
RuntimeException

if the array is empty

Return values
AllowedType

The first item of the array

toArray()

Return array representation of collection.

public toArray() : array<string|int, mixed>
APIYes
Return values
array<string|int, mixed>

top()

Peek at the last item of the array.

public top() : AllowedType
APIYes
Tags
throws
RuntimeException

if the array is empty

Return values
AllowedType

The last item of the array

toStrictList()

Turn collection into a type-sensitive list.

public toStrictList() : StrictList<string|int, AllowedType>
APIYes
Tags
throws
DomainException

if the collection is not a list

see
StrictCollection::isList()
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

Attributes
#[Override]

unshift()

Prepend the array with an item.

public unshift(AllowedType $value) : void
APIYes
Parameters
$value : AllowedType

The item to unshift

Tags
throws
InvalidDataTypeException

if $value is not of allowed type


        
On this page

Search results