griffins/sequence

一个简单的序列生成器

1.0.1 2018-02-04 20:17 UTC

This package is auto-updated.

Last update: 2024-09-12 05:33:44 UTC


README

一个简单的序列生成器

Build Status

安装

composer require griffins/sequence

基本用法

此文档假设您通过 composer 自动加载库。

    
$allowedChars = 'ABCDEF0123456789';
//the only argument is an optional character dictonary, if not specified the default one is used. (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ)
$sequence = \Sequence\Factory::create($allowedChars);

echo $sequence->next('<yyyy>/????');

输出 2018/0001

<yyyy>, <mm>, <dd> 分别代表当前年、月、日。

$sequence->next('<yyyy>/????',null,'2018/AAAA')

输出 2018/AAAB

第二个参数是一个回调函数,用于返回 true 或 false 来拒绝生成的序列。当返回 false 时,会重新生成序列,直到回调函数满足或发生溢出。例如,尝试增加 9999,而使用的格式限制为 4 个字符。

智能搜索通过使用二分搜索优化回调函数的使用,以提升性能。

良好的使用方式将是

$sequence = \Sequence\Factory::create();

$id =  $sequence->next('<yyyy>/????', function($id){
    //check if its exits in a dataset, 
    
    if($exists){
        return true;
    }else{
        // looks like we found a valid id
        return false;
    }
});
// now use the id generated

echo $id;