PHP Basics

StrictCollection
in package
implements ArrayAccess, Countable, Serializable Uses ArrayAccessTrait, CountableTrait, Getter

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
author

Sebastian Meyer sebastian.meyer@opencultureconsulting.com

template

AllowedType of mixed

Table of Contents

Interfaces

ArrayAccess
Countable
Serializable

Properties

$allowedTypes  : array<string|int, string>

Methods

__construct()  : void
Create a type-sensitive collection of items.
__get()  : mixed
Read data from an inaccessible property.
__isset()  : bool
Check if an inaccessible property is set and not empty.
add()  : void
Add/insert a new item at the specified index.
clear()  : void
Clear the collection of any items.
count()  : int<0, max>
Count the data items.
get()  : AllowedType|null
Get the item at the specified index.
getAllowedTypes()  : array<string|int, string>
Get allowed data types for collection items.
isAllowedType()  : bool
Check if the item's data type is allowed in the collection.
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.
serialize()  : string
Get string representation of $this.
set()  : void
Set an item at the specified index.
toArray()  : array<string|int, AllowedType>
Return array representation of collection.
toStrictList()  : StrictList<string|int, AllowedType>
Turn collection into a type-sensitive list.
unserialize()  : void
Restore $this from string representation.
setAllowedTypes()  : void
Set allowed data types of collection items.

Properties

$allowedTypes read-only

public array<string|int, string> $allowedTypes

The allowed data types for items.

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

if any value of $allowedTypes is not a string

__get()

Read data from an inaccessible property.

public __get(string $property) : mixed
Parameters
$property : string

The class property to get

Tags
throws
InvalidArgumentException

if the property or getter method do not exist

Return values
mixed

The class property's current value

__isset()

Check if an inaccessible property is set and not empty.

public __isset(string $property) : bool
Parameters
$property : string

The class property to check

Return values
bool

Whether the class property is set and not empty

add()

Add/insert a new item at the specified index.

public add(string|int $offset, AllowedType $value) : void
APIYes
Parameters
$offset : string|int

The new item's index

$value : AllowedType

The new item

Tags
throws
InvalidArgumentException

if $offset is not of allowed type

clear()

Clear the collection of any items.

public clear() : void
APIYes

count()

Count the data items.

public count() : int<0, max>
APIYes
Return values
int<0, max>

The number of data items

get()

Get the item at the specified index.

public get(string|int $offset) : AllowedType|null
APIYes
Parameters
$offset : string|int

The item's index

Return values
AllowedType|null

The item or NULL if key is invalid

getAllowedTypes()

Get allowed data types for collection items.

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

The list of allowed data types

isAllowedType()

Check if the item's data type is allowed in the collection.

public isAllowedType(AllowedType $value) : bool
APIYes
Parameters
$value : AllowedType

The item to check

Return values
bool

Whether the item's 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()
Return values
bool

Whether the collection is a list

offsetExists()

Check if the specified offset exists.

public offsetExists(string|int $offset) : bool
APIYes
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
APIYes
Parameters
$offset : string|int

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(string|int|null $offset, AllowedType $value) : void
APIYes
Parameters
$offset : string|int|null

The offset being set

$value : AllowedType

The new item for the offset

Tags
throws
InvalidArgumentException

if $offset or $value is not of allowed type

offsetUnset()

Unset the specified offset.

public offsetUnset(string|int $offset) : void
APIYes
Parameters
$offset : string|int

The offset to unset

remove()

Remove an item from the collection.

public remove(string|int $offset) : void
APIYes
Parameters
$offset : string|int

The item's key

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
APIYes
Parameters
$offset : string|int

The item's index

$value : AllowedType

The item

Tags
throws
InvalidArgumentException

if $value is not of allowed type

toArray()

Return array representation of collection.

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

Array of collection items

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

setAllowedTypes()

Set allowed data types of collection items.

protected setAllowedTypes([array<string|int, string> $allowedTypes = [] ]) : void
Parameters
$allowedTypes : array<string|int, string> = []

Allowed data types of items

Tags
throws
InvalidArgumentException

if any value of $allowedTypes is not a string


        
On this page

Search results