2024-03-26 20:10:30 +01:00
|
|
|
.. title:: Datastructures
|
|
|
|
|
|
|
|
Typed Datastructures
|
|
|
|
####################
|
|
|
|
|
|
|
|
.. sidebar:: Table of Contents
|
|
|
|
.. contents::
|
|
|
|
|
2024-03-27 12:19:43 +01:00
|
|
|
The datastructures in this package are derived from their respective `Standard PHP Library (SPL) counterparts
|
|
|
|
<https://www.php.net/spl.datastructures>`_ which they extend by the option to control the data types of items those
|
|
|
|
datastructures can hold.
|
2024-03-26 20:10:30 +01:00
|
|
|
|
|
|
|
To restrict allowed data types for items, provide the constructor with an array of atomic types or fully qualified
|
|
|
|
class names you want to allow as item types. Available atomic types are `array`, `bool`, `callable`, `countable`,
|
|
|
|
`float` / `double`, `int` / `integer` / `long`, `iterable`, `null`, `numeric`, `object`, `resource`, `scalar` and
|
|
|
|
`string`.
|
|
|
|
|
|
|
|
Trying to add an item with a data type not on the list of allowed types to a strict datastructure will result in an
|
|
|
|
:php:class:`OCC\Basics\DataStructures\Exceptions\InvalidDataTypeException`.
|
|
|
|
|
2024-03-27 12:19:43 +01:00
|
|
|
All strict datastructures inherit the implementation of the `\ArrayAccess <https://www.php.net/arrayaccess>`_,
|
|
|
|
`\Countable <https://www.php.net/countable>`_ and `\Serializable <https://www.php.net/serializable>`_ interfaces. All
|
2024-03-31 17:41:04 +02:00
|
|
|
but `StrictCollection` also implement a `\Traversable <https://www.php.net/traversable>`_ interface.
|
2024-03-27 12:19:43 +01:00
|
|
|
|
2024-03-26 20:10:30 +01:00
|
|
|
Examples:
|
|
|
|
|
|
|
|
.. code-block:: php
|
|
|
|
// create a collection of strings
|
|
|
|
$stringCollection = new StrictCollection(['string']);
|
|
|
|
|
2024-03-31 17:41:04 +02:00
|
|
|
.. code-block:: php
|
2024-03-26 20:10:30 +01:00
|
|
|
// create a queue of PSR-15 middlewares
|
|
|
|
$middlewareQueue = new StrictQueue(['Psr\Http\Server\MiddlewareInterface']);
|
|
|
|
|
|
|
|
.. sidebar:: API Documentation
|
2024-03-27 12:19:43 +01:00
|
|
|
* :php:class:`OCC\Basics\DataStructures\StrictCollection`
|
2024-03-26 20:10:30 +01:00
|
|
|
|
2024-03-31 17:41:04 +02:00
|
|
|
StrictCollection
|
|
|
|
================
|
|
|
|
|
2024-03-26 20:10:30 +01:00
|
|
|
*A type-sensitive, unsorted collection of items.*
|
|
|
|
|
|
|
|
Holds items as key/value pairs where keys identify the items and have to be valid array keys while values can be of any
|
2024-03-27 12:19:43 +01:00
|
|
|
controlled type. The collection can be accessed like an array, but not traversed because it has no particular order.
|
|
|
|
|
|
|
|
.. note::
|
2024-03-31 17:41:04 +02:00
|
|
|
Internally it holds the items in the `$_data` array, the same as most :doc:`interfaces` and :doc:`traits` of this
|
|
|
|
package.
|
2024-03-26 20:10:30 +01:00
|
|
|
|
2024-03-27 12:19:43 +01:00
|
|
|
.. sidebar:: API Documentation
|
|
|
|
* :php:class:`OCC\Basics\DataStructures\StrictArray`
|
|
|
|
|
2024-03-31 17:41:04 +02:00
|
|
|
StrictArray
|
|
|
|
================
|
|
|
|
|
2024-03-27 12:19:43 +01:00
|
|
|
*A type-sensitive, traversable array of items.*
|
|
|
|
|
|
|
|
Holds items as key/value pairs where keys identify the items and have to be valid array keys while values can be of any
|
|
|
|
controlled type. The array can be accessed and traversed just like any other array.
|
2024-03-26 20:10:30 +01:00
|
|
|
|
|
|
|
.. note::
|
2024-03-31 17:41:04 +02:00
|
|
|
Internally it holds the items in the `$_data` array, the same as most :doc:`interfaces` and :doc:`traits` of this
|
|
|
|
package.
|
2024-03-26 20:10:30 +01:00
|
|
|
|
|
|
|
.. sidebar:: API Documentation
|
2024-03-27 12:19:43 +01:00
|
|
|
* :php:class:`OCC\Basics\DataStructures\StrictList`
|
2024-03-26 20:10:30 +01:00
|
|
|
|
2024-03-31 17:41:04 +02:00
|
|
|
StrictList
|
|
|
|
==========
|
|
|
|
|
2024-03-26 20:10:30 +01:00
|
|
|
*A type-sensitive, taversable list of items.*
|
|
|
|
|
|
|
|
Extends `\SplDoublyLinkedList <https://www.php.net/spldoublylinkedlist>`_ with an option to restrict the allowed data
|
2024-03-27 12:19:43 +01:00
|
|
|
types for list items. The list can be accessed and traversed like an array, but has only consecutive numerical keys.
|
2024-03-26 20:10:30 +01:00
|
|
|
|
|
|
|
.. sidebar:: API Documentation
|
2024-03-27 12:19:43 +01:00
|
|
|
* :php:class:`OCC\Basics\DataStructures\StrictQueue`
|
2024-03-26 20:10:30 +01:00
|
|
|
|
2024-03-31 17:41:04 +02:00
|
|
|
StrictQueue
|
|
|
|
===========
|
|
|
|
|
2024-03-26 20:10:30 +01:00
|
|
|
*A type-sensitive, taversable queue (FIFO) of items.*
|
|
|
|
|
2024-03-27 12:19:43 +01:00
|
|
|
Extends `\SplQueue <https://www.php.net/splqueue>`_ with an option to restrict the allowed data types for queue items.
|
|
|
|
The queue can be accessed and traversed like an array, but has only consecutive numerical keys. Traversal follows the
|
|
|
|
first-in, first-out (FIFO) principle meaning that items are returned in the same order they were added to the queue.
|
|
|
|
|
|
|
|
It is recommended to use the `StrictQueue::enqueue()` and `StrictQueue::dequeue()` alias methods when working with a
|
|
|
|
queue, because those will ensure proper FIFO behavior and remove items while traversing.
|
2024-03-26 20:10:30 +01:00
|
|
|
|
|
|
|
.. sidebar:: API Documentation
|
2024-03-27 12:19:43 +01:00
|
|
|
* :php:class:`OCC\Basics\DataStructures\StrictStack`
|
2024-03-26 20:10:30 +01:00
|
|
|
|
2024-03-31 17:41:04 +02:00
|
|
|
StrictStack
|
|
|
|
===========
|
|
|
|
|
2024-03-26 20:10:30 +01:00
|
|
|
*A type-sensitive, taversable stack (LIFO) of items.*
|
|
|
|
|
2024-03-27 12:19:43 +01:00
|
|
|
Extends `\SplStack <https://www.php.net/splstack>`_ with an option to restrict the allowed data types for stack items.
|
|
|
|
The stack can be accessed and traversed like an array, but has only consecutive numerical keys. Traversal follows the
|
|
|
|
last-in, first-out (LIFO) principle meaning that items are returned in the reversed order they were added to the stack.
|
|
|
|
|
|
|
|
It is recommended to use the `StrictStack::stack()` and `StrictStack::unstack()` alias methods when working with a
|
|
|
|
stack, because those will ensure proper LIFO behavior and remove items while traversing.
|