jamsouf/repeat

重复工具。以简单的方式处理重复操作。

v1.0.0 2015-03-08 14:03 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:54:49 UTC


README

Latest Stable Version Build Status

Repeat 是一个用于 PHP / Composer 的重复工具。
它使得重复调用函数或类似操作变得简单。

安装

通过 composer

使用 composer require jamsouf/repeat 安装最新版本

require 'vendor/autoload.php';

示例

调用匿名函数 100 次
$results = Repeat::_function(100, function () {
    // your function code
});
调用命名函数,并在每次调用中访问当前结果
$calculate = function ($result) {
    // your function code
};

$results = Repeat::_function(20, $calculate);
调用命名函数,直到满足特定条件
$calculate = function ($result) {
    // your function code
};

$until = function ($result) {
    // return true or false
};

$results = Repeat::_function(50, $calculate, $until);
使用索引引用和分隔符重复字符串,直到满足特定条件
$result = Repeat::_string(10, 'v1.{j}.{i}', function ($result) {
    return strpos($result, '.4.') !== false ? true : false;
}, ', ');

// => v1.1.0, v1.2.1, v1.3.2, v1.4.3

API 文档

Repeat::_function($count, $func, $until = null)

重复调用函数

  • integer $count: 函数应调用的次数
  • callable $func: 要重复调用的函数
  • callable $until (可选): 重复调用直到此函数返回 true
  • => 返回 array: 每次函数调用的结果

Repeat::_string($count, $string, $until = null, $delimiter = null)

重复字符串

  • integer $count: 字符串应重复的次数
  • string $string: 要重复的字符串
  • callable $until (可选): 重复字符串直到此函数返回 true
  • string $delimiter (可选): 分隔字符串的符号
  • => 返回 string: 重复的字符串