Array classes
The following ready-made classes are available.
Arr
Generic array class. Uses ArrayAccessTrait, CoercionTrait, ComparableTrait, CountableTrait, IteratorTrait, JsonSerializableTrait, StringableTrait and TypeTrait. Implements ArrayAccess, Comparable, Countable, Stringable and Iterator interfaces.
Queue
Queue implementation class. Uses CoercionTrait, ComparableTrait, CountableTrait, QueueIteratorTrait, QueueTrait, StringableTrait and TypeTrait. Implements Comparable, Countable, Stringable and IteratorAggregate interfaces.
Stack
Stack implementation class. Uses CoercionTrait, ComparableTrait, CountableTrait, StackIteratorTrait, StackTrait, StringableTrait and TypeTrait. Implements Comparable, Countable, Stringable and IteratorAggregate interfaces.
Funcitonal traits
The following traits provide functionality. Adding more than one of these traits do not cause conflicts.
ArrayAccessTrait
Implements ArrayAccess interface.
Allows accessing class as an array, using []
with offset to get, set and check array content.
CoercionTrait
Trait that support coercing non-array content as input.
ComparableTrait
Implements Comparable and Equalable interfaces. Allows comparing class instances based on internal content.
CountableTrait
Implements Countable interface.
Enables count()
function on class.
JsonSerializableTrait
Implements JsonSerializable interface. Provides array content for JSON encode.
QueueTrait
Implements queue methods.
As a Queue (FIFO) implementation, enqueue
will put items at the end and dequeue
will retrieve the first item.
StackTrait
Adds stack methods.
As a Stack (LIFO) implementation, push
will put items at the top and pop
will retrieve the last added item.
StringableTrait
Implements Stringable interface.
Allows string conversion of class to classname(count)
(namespace is excluded).
TypeTrait
Base trait for all traits using array as source.
Defines source property, options and the initialize
method.
Iterator traits
The following traits provide iterators. You should not use more than one iterator in a class.
IteratorAggregateTrait
Implements IteratorAggregate and Traversable interfaces.
Enables traversing methods such as foreach()
by aggregating a Generator.
IteratorTrait
Implements Iterator and Traversable interfaces.
Enables traversing methods such as foreach()
;
QueueIteratorTrait
Same as IteratorAggregateTrait, except it will consume array content. As a Queue (FIFO) iterator, it will consume from the start of the array and forward.
StackIteratorTrait
Same as IteratorAggregateTrait, except it will consume array content. As a Stack (LIFO) iterator, it will consume from the end of the array and backwards.
Defining data source
By default, source data is stored in protected property $o_array_source
.
If your class is using another property to keep array data, it may define the source property by setting
$o_source_ref
to the name of that property. The array traits use this definition;
protected array $o_array_source;
protected string $o_source_ref = 'o_array_source';
Example using standard array source.
class MyClass
{
// Use one or more traits
use ArrayAccessTrait;
use ComparableTrait;
use CountableTrait;
use IteratorTrait;
use StringableTrait;
public function __construct(array $data)
{
// Set data provided in constructor.
$this->o_array_source = $data;
}
}
$my = new MyClass([1, 2, 3]);
Example using non-standard array source.
class MyClass
{
// Use one or more traits
use ArrayAccessTrait;
use ComparableTrait;
use CountableTrait;
use IteratorTrait;
use StringableTrait;
// Define the variable shat should hold array source data
protected array $my_data_source;
public function __construct(array $data)
{
// Tell the traits where to find the source data.
$this->o_source_ref = 'my_data_source';
// Set data provided in constructor.
$this->my_data_source = $data;
}
}
$my = new MyClass([1, 2, 3]);
Requirements
- PHP
^8.0