PHP Basics

StrictStack extends StrictList
in package
implements ArrayAccess, Countable, Iterator, Serializable

A type-sensitive, taversable stack (LIFO).

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. It also restricts the iterator direction to last-in, first-out (LIFO) exactly like \SplStack.

Tags
author

Sebastian Meyer sebastian.meyer@opencultureconsulting.com

template

AllowedType of mixed

Table of Contents

Interfaces

ArrayAccess
Countable
Iterator
Serializable

Properties

$allowedTypes  : array<string|int, string>

Methods

__construct()  : void
Create a type-sensitive, traversable stack 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 offset.
append()  : void
Append items at the end of the list.
bottom()  : AllowedType
Peek at the item at the beginning of the list.
clear()  : void
Clear the list of any items.
count()  : int
Get the number of items on the list.
current()  : AllowedType
Get the current list item.
get()  : AllowedType
Get the item at the specified index.
getAllowedTypes()  : array<string|int, string>
Get allowed data types for list items.
getIteratorMode()  : int
Get the mode of iteration.
isAllowedType()  : bool
Check if the item's data type is allowed on the list.
isEmpty()  : bool
Check if list is empty.
isList()  : true
Check if this can be considered a list.
key()  : int
Get the current list index.
next()  : void
Move the cursor to the next list index.
offsetExists()  : bool
Check if the specified index exists and is not empty.
offsetGet()  : AllowedType
Get the item from the specified index.
offsetSet()  : void
Set the item at the specified offset.
offsetUnset()  : void
Unset the item at the specified index.
pop()  : AllowedType
Pops an item from the end of the list.
prepend()  : void
Prepend items at the start of the list.
prev()  : void
Move the cursor to the previous list index.
push()  : void
Push an item at the end of the list.
remove()  : void
Remove an item from the list.
rewind()  : void
Rewind the iterator's cursor.
set()  : void
Set an item at the specified index.
setIteratorMode()  : int
Set the mode of iteration.
shift()  : AllowedType
Shift an item from the beginning of the list.
stack()  : void
Add an item to the stack.
toArray()  : array<string|int, AllowedType>
Return array representation of list.
top()  : AllowedType
Peek at the item at the end of the list.
toStrictCollection()  : StrictCollection<string|int, AllowedType>
Turn list into a type-sensitive collection.
unshift()  : void
Prepend the list with an item.
unstack()  : AllowedType
Unstack an item from the stack.
valid()  : bool
Check if current cursor position is valid.
setAllowedTypes()  : void
Set allowed data types of list items.

Properties

$allowedTypes read-only

public array<string|int, string> $allowedTypes

The allowed data types for values.

Methods

__construct()

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

if $value is not of allowed type

throws
OutOfRangeException

when $offset is out of bounds

append()

Append items at the end of the list.

public append(AllowedType ...$values) : void
APIYes
Parameters
$values : AllowedType

One or more items to append

Tags
throws
InvalidArgumentException

if any $values is not of allowed type

bottom()

Peek at the item at the beginning of the list.

public bottom() : AllowedType
APIYes
Tags
throws
RuntimeException

if the list is empty

Return values
AllowedType

The first item of the list

clear()

Clear the list of any items.

public clear() : void
APIYes

count()

Get the number of items on the list.

public count() : int
APIYes
Return values
int

The number of items on the list

current()

Get the current list item.

public current() : AllowedType
APIYes
Return values
AllowedType

The current item

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

getAllowedTypes()

Get allowed data types for list items.

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

The list of allowed data types

getIteratorMode()

Get the mode of iteration.

public getIteratorMode() : int
APIYes
Return values
int

The set of flags and modes of iteration

isAllowedType()

Check if the item's data type is allowed on the list.

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 list is empty.

public isEmpty() : bool
APIYes
Return values
bool

Whether the list contains no items

isList()

Check if this can be considered a list.

public isList() : true
APIYes
Return values
true

Always TRUE (this exists only for compatibility reasons)

key()

Get the current list index.

public key() : int
APIYes
Return values
int

The current list index

next()

Move the cursor to the next list index.

public next() : void
APIYes

offsetExists()

Check if the specified index exists and is not empty.

public offsetExists(int $offset) : bool
APIYes
Parameters
$offset : int

The index to check

Return values
bool

Whether the index exists and is not empty

offsetGet()

Get the item from the specified index.

public offsetGet(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

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
InvalidArgumentException

if $value is not of allowed type

throws
OutOfRangeException

when $offset is out of bounds

offsetUnset()

Unset the item at the specified index.

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

The item's index

Tags
throws
OutOfRangeException

when $offset is out of bounds

pop()

Pops an item from the end of the list.

public pop() : AllowedType
APIYes
Tags
throws
RuntimeException

if the list is empty

Return values
AllowedType

The item from the end of the list

prepend()

Prepend items at the start of the list.

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

One or more items to prepend

Tags
throws
InvalidArgumentException

if $value is not of allowed type

prev()

Move the cursor to the previous list index.

public prev() : void
APIYes

push()

Push an item at the end of the list.

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

The item to push

Tags
throws
InvalidArgumentException

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

rewind()

Rewind the iterator's cursor.

public rewind() : void
APIYes

set()

Set an item at the specified index.

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

The item's index

$value : AllowedType

The item

Tags
throws
InvalidArgumentException

if $value is not of allowed type

setIteratorMode()

Set the mode of iteration.

public final setIteratorMode(int $mode) : int
APIYes
Parameters
$mode : int

The new iterator mode (2 or 3)

There are two orthogonal sets of modes that can be set.

The direction of iteration (fixed for StrictStack):

  • StrictStack::IT_MODE_LIFO (stack style)

The behavior of the iterator (either one or the other):

  • StrictStack::IT_MODE_DELETE (delete items)
  • StrictStack::IT_MODE_KEEP (keep items)

The default mode is: IT_MODE_LIFO | IT_MODE_KEEP

Tags
throws
RangeException

if an invalid $mode is given

throws
RuntimeException

if trying to change iterator direction

Return values
int

The set of flags and modes of iteration

shift()

Shift an item from the beginning of the list.

public shift() : AllowedType
APIYes
Tags
throws
RuntimeException

if the list is empty

Return values
AllowedType

The first item of the list

stack()

Add an item to the stack.

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

The item to stack

Tags
throws
InvalidArgumentException

if $value is not of allowed type

toArray()

Return array representation of list.

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

Array of list items

top()

Peek at the item at the end of the list.

public top() : AllowedType
APIYes
Tags
throws
RuntimeException

if the list is empty

Return values
AllowedType

The last item of the list

toStrictCollection()

Turn list into a type-sensitive collection.

public toStrictCollection() : StrictCollection<string|int, AllowedType>
APIYes
Return values
StrictCollection<string|int, AllowedType>

A type-sensitive collection of the list's items

unshift()

Prepend the list with an item.

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

The item to unshift

Tags
throws
InvalidArgumentException

if $value is not of allowed type

unstack()

Unstack an item from the stack.

public unstack() : AllowedType
APIYes
Return values
AllowedType

The unstacked item

valid()

Check if current cursor position is valid.

public valid() : bool
APIYes
Return values
bool

Whether the current cursor position is valid

setAllowedTypes()

Set allowed data types of list 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