Skip to content

Latest commit

 

History

History
161 lines (105 loc) · 5.12 KB

Array.md

File metadata and controls

161 lines (105 loc) · 5.12 KB

Docs / Array

Array classes

The following ready-made classes are available.

Generic array class. Uses ArrayAccessTrait, CoercionTrait, ComparableTrait, CountableTrait, IteratorTrait, JsonSerializableTrait, StringableTrait and TypeTrait. Implements ArrayAccess, Comparable, Countable, Stringable and Iterator interfaces.

Queue implementation class. Uses CoercionTrait, ComparableTrait, CountableTrait, QueueIteratorTrait, QueueTrait, StringableTrait and TypeTrait. Implements Comparable, Countable, Stringable and IteratorAggregate interfaces.

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.

Implements ArrayAccess interface. Allows accessing class as an array, using [] with offset to get, set and check array content.

Trait that support coercing non-array content as input.

Implements Comparable and Equalable interfaces. Allows comparing class instances based on internal content.

Implements Countable interface. Enables count() function on class.

Implements JsonSerializable interface. Provides array content for JSON encode.

Implements queue methods. As a Queue (FIFO) implementation, enqueue will put items at the end and dequeue will retrieve the first item.

Adds stack methods. As a Stack (LIFO) implementation, push will put items at the top and pop will retrieve the last added item.

Implements Stringable interface. Allows string conversion of class to classname(count) (namespace is excluded).

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.

Implements IteratorAggregate and Traversable interfaces. Enables traversing methods such as foreach() by aggregating a Generator.

Implements Iterator and Traversable interfaces. Enables traversing methods such as foreach();

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.

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]);