Skip to content

4. API reference

Ulysse ARNAUD edited this page Sep 14, 2024 · 2 revisions

API Reference

This page provides a detailed reference for all the methods available in the flexible-collections package. Each method includes a description, usage examples, and the expected output.

Table of Contents

Creating a Collection

To create a new collection instance, simply instantiate the Collection class:

use Ulyssear\Collection;

$collection = new Collection();

Adding Elements

push()

Adds one or more elements to the collection.

Usage

$collection->push('PHP', 'Symfony', 'Laravel');

Example

$collection = new Collection();
$collection->push('PHP', 'Symfony', 'Laravel');
print_r($collection->entries());

Output

Array
(
    [0] => PHP
    [1] => Symfony
    [2] => Laravel
)

pushNamedItem()

Adds an element with a specific key.

Usage

$collection->pushNamedItem('language', 'PHP');

Example

$collection = new Collection();
$collection->pushNamedItem('language', 'PHP');
print_r($collection->entries());

Output

Array
(
    [language] => PHP
)

pushNamedItemIfNotExists()

Adds a named item only if it does not already exist.

Usage

$collection->pushNamedItemIfNotExists('framework', 'Laravel');

Example

$collection = new Collection();
$collection->pushNamedItem('framework', 'Symfony');
$collection->pushNamedItemIfNotExists('framework', 'Laravel');
print_r($collection->entries());

Output

Array
(
    [framework] => Symfony
)

pushNamedItems()

Adds multiple named elements to the collection.

Usage

$collection->pushNamedItems(['key1', 'value1'], ['key2', 'value2']);

pushNamedItemsIfNotExists()

Adds multiple named items only if they do not already exist.

Usage

$collection->pushNamedItemsIfNotExists(['key1', 'value1'], ['key2', 'value2']);

pushHeap()

Adds elements in heap order.

Usage

$collection->pushHeap(5, 1, 3);

pushStack()

Adds elements in stack order (LIFO).

Usage

$collection->pushStack(2, 1);

Retrieving Elements

get()

Retrieves an element at a specific index.

Usage

$collection->get(1);

entries()

Returns all entries in the collection.

Usage

$collection->entries();

keys()

Returns all keys of the elements in the collection.

Usage

$collection->keys();

values()

Returns all values of the elements in the collection.

Usage

$collection->values();

Checking Elements

has()

Checks if an element exists in the collection.

Usage

$collection->has('Symfony');

hasIndex()

Checks if a specific key exists in the collection.

Usage

$collection->hasIndex('framework');

Removing Elements

pop()

Removes and returns the last item added.

Usage

$collection->pop();

shift()

Removes and returns the first item added.

Usage

$collection->shift();

clear()

Clears the entire collection.

Usage

$collection->clear();

Conditional Operations

pushIfNotExists()

Adds one or more elements only if they do not already exist in the collection.

Usage

$collection->pushIfNotExists(3, 4);

This reference should help you understand the different methods and their usage in the flexible-collections package. For further examples, check out the Usage page.