nabeghe/yielder

A similar alternative to yield in PHP for reading values from arrays, with a touch of zest.

1.0.0 2024-10-11 14:15 UTC

This package is auto-updated.

Last update: 2024-10-11 14:18:11 UTC


README

A similar alternative to yield in PHP for reading values from arrays, with a touch of zest.

Retrieve the value of the next index in an array whenever you want, or define the values as callables so that their contents can be returned when needed. This is likely just the beginning or the end of this package library!

🫡 Usage

🚀 Installation

You can install the package via composer:

composer require nabeghe/yielder

Example 1: Value

Each time, the array returns the value of the next index.

use Nabeghe\Yielder\Yielder;

$data = [
    'value 1',
    'value 2',
    'value 3',
];

$value = Yielder::value($data, $index);
echo "Index $index: $value\n"; // Index 0: value 1

$value = Yielder::value($data, $index);
echo "Index $index: $value\n"; // Index 1: value 2

$value = Yielder::value($data, $index);
echo "Index $index: $value\n"; // Index 2: value 3

$value = Yielder::value($data, $index);
echo "Index $index: $value\n"; // Index -1: null

Yielder::reset($data);

$value = Yielder::value($data, $index);
echo "Index $index: $value\n"; // Index 0: value 1

Example 2: Each

Iterating through the array using the value method.

$data = [
    'value 1',
    'value 2',
    'value 3',
];

Yielder::each($data, function ($value, $index) {
    echo "Index $index: $value\n";
    // return true if you want to break.
});

Example 3: Value Call

Imagine you have an array that contains data, but instead of placing the data directly in each index, you can define them as callables. This way, they can be executed when needed, returning their values.

This is something like lazy loading.

use Nabeghe\Yielder\Yielder;

$data = [
    function () {
        return 'Value 1';
    },
    function () {
        return 'Value 2';
    },
    function () {
        return 'Value 3';
    },
];

$value = Yielder::valueCall($data, $index);
echo "Index $index: $value\n"; // Index 0: value 1

$value = Yielder::valueCall($data, $index);
echo "Index $index: $value\n"; // Index 1: value 2

$value = Yielder::valueCall($data, $index);
echo "Index $index: $value\n"; // Index 2: value 3

$value = Yielder::valueCall($data, $index);
echo "Index $index: $value\n"; // Index -1: null

Yielder::reset($data);

$value = Yielder::valueCall($data, $index);
echo "Index $index: $value\n"; // Index 0: value 1

Example 4: Each Call

Iterating through the array, but exactly like valueCall.

use Nabeghe\Yielder\Yielder;

$data = [
    function () {
        return 'Value 1';
    },
    function () {
        return 'Value 2';
    },
    function () {
        return 'Value 3';
    },
];

Yielder::eachCall($data, function ($value, $index) {
    echo "Index $index: $value\n";
    // return true if you want to break.
});

📖 License

Copyright (c) 2024 Hadi Akbarzadeh

Licensed under the MIT license, see LICENSE.md for details.