承接 nabeghe/yielder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nabeghe/yielder

最新稳定版本:1.0.0

Composer 安装命令:

composer require nabeghe/yielder

包简介

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

README 文档

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

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

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-11