PHP Basics

StrictList extends SplDoublyLinkedList
in package
implements ArrayAccess, Countable, Iterator, Serializable uses StrictSplDoublyLinkedListTrait

A type-sensitive, taversable list.

Extends \SplDoublyLinkedList with an option to restrict the allowed data types for list items by providing the constructor with an array of atomic types or fully qualified class names.

Tags
author

Sebastian Meyer sebastian.meyer@opencultureconsulting.com

template
phpstan-consistent-constructor
psalm-consistent-constructor

Table of Contents

Interfaces

ArrayAccess
Countable
Iterator
Serializable

Methods

__construct()  : mixed
Create a type-sensitive, traversable list of items.
add()  : void
Add/insert a new item at the specified offset.
append()  : void
Append items at the end of the list.
clear()  : void
Clear the list of any items.
fromArray()  : TAllowedType>
Create a type-sensitive list from an array of items.
get()  : AllowedType
Get the item at the specified index.
isList()  : true
Check if this can be considered a list.
offsetSet()  : void
Set the item at the specified offset.
prepend()  : void
Prepend items at the start of the list.
push()  : void
Push an item at the end of the list.
remove()  : void
Remove an item from the list.
set()  : void
Set an item at the specified index.
toArray()  : array<int, AllowedType>
Return array representation of list.
toStrictCollection()  : StrictCollection<int, AllowedType>
Turn list into a type-sensitive collection.
unshift()  : void
Prepend the list with an item.

Methods

__construct()

Create a type-sensitive, traversable list 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
throws
InvalidArgumentException

if any value of $allowedTypes is not a string or contains an empty type

add()

Add/insert a new item at the specified offset.

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

The offset where the new item is to be inserted

$value : AllowedType

The new item for the offset

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

OutOfRangeException

when $offset is out of bounds

fromArray()

Create a type-sensitive list from an array of items.

public static fromArray(array<string|int, TAllowedType> $items[, array<string|int, string|TAllowedType>> $allowedTypes = [] ]) : TAllowedType>
APIYes

The items are pushed in the given order (like append()), while their keys are discarded.

Parameters
$items : array<string|int, TAllowedType>

The items

$allowedTypes : array<string|int, string|TAllowedType>> = []

Allowed data types of items (see __construct())

Tags
template
throws
InvalidArgumentException

if any value of $allowedTypes is not a string or contains an empty type

InvalidDataTypeException

if any of $items is not of allowed type

Return values
TAllowedType> —

The new list

get()

Get the item at the specified index.

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

The item's index

Tags
throws
OutOfRangeException

when $offset is out of bounds

Return values
AllowedType —

The item

isList()

Check if this can be considered a list.

public isList() : true
APIYes
Return values
true —

Always TRUE (this exists only for compatibility reasons)

offsetSet()

Set the item at the specified offset.

public offsetSet(int|null $offset, AllowedType $value) : void
APIYes
Parameters
$offset : int|null

The offset being set or NULL to append

$value : AllowedType

The new item for the offset

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

OutOfRangeException

when $offset is out of bounds

prepend()

Prepend items at the start of the list.

public prepend(AllowedType ...$values) : void
APIYes

The items keep their order, i.e. prepend(1, 2) on a list [3] results in [1, 2, 3] (just like array_unshift()).

Parameters
$values : AllowedType

One or more items to prepend

Tags
throws
InvalidDataTypeException

if $value is not of allowed type

remove()

Remove an item from the list.

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

The item's index

Tags
throws
OutOfRangeException

when $offset is out of bounds

toArray()

Return array representation of list.

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

Array of list items


        
On this page

Search results