gajus/paggern

用于生成随机字符串的模式解释器。

0.1.1 2014-04-17 13:14 UTC

This package is not auto-updated.

Last update: 2024-09-19 16:56:20 UTC


README

Build Status Coverage Status Latest Stable Version License

用于生成随机字符串的模式解释器。

生成器

$generator = new \Gajus\Paggern\Generator();

/**
 * Generate a set of random codes based on Paggern pattern.
 * Codes are guaranteed to be unique within the set.
 *
 * @param string $pattern Paggern pattern.
 * @param int $amount Number of codes to generate.
 * @param int $safeguard Number of additional codes generated in case there are duplicates that need to be replaced.
 * @return array
 */
$codes = $generator->generateFromPattern('FOO[A-Z]{10}[0-9]{2}', 100);

上述示例将生成一个包含100个代码的数组,每个代码前缀为"FOO",后跟从"ABCDEFGHKMNOPRSTUVWXYZ23456789"的10个字符和从"0123456789"的2个数字。

Paggern使用RandomLib来生成匹配随机字符池的模式。

词法分析器

词法分析器提供了一个方法来标记字符串。`Lexer`与`Generator`独立。您可以选择使用自己的`Generator`实现来解释`Lexer`标记。

$lexer = new \Gajus\Paggern\Lexer();

/**
 * Tokeniser explodes input into components describing the properties expressed in the pattern.
 *
 * @param string $pattern
 * @param boolean $expand Augment token definition with the haystack of possible values.
 * @return array
 */
$lexer->tokenise('[a-c]{3}[1-3]{3}', true);
[
    [
        'type' => 'range',
        'token' => 'a-c',
        'repetition' => 3,
        'haystack' => 'abc'
    ],
    [
        'type' => 'range',
        'token' => '1-3',
        'repetition' => 3,
        'haystack' => 123
    ]
]

支持的标记

字面量

模式可以由字面字符组成,例如代码的前缀或后缀。

$lexer->tokenise('abc');
[
    [
        'type' => 'literal',
        'string' => 'abc'
    ]
]

上述模式命令字符串必须是"abc"。

范围

范围可以是数字或ASCII。

$lexer->tokenise('[a-z]');

[a-z]示例中,字符串必须是"abcdefghijklmnopqrstuvwxyz"的字符。

[
    [
        'type' => 'range',
        'token' => 'a-z',
        'repetition' => 1
    ]
]

带有重复的范围

如果字符必须出现多次,请使用重复。

$lexer->tokenise('[a-c]{3}');

[a-z]{3}示例中,字符串必须由"abc"的3个字符组成。

[
    [
        'type' => 'range',
        'token' => 'a-c',
        'repetition' => 3
    ]
]

字符类

可以使用预定义的字符类代替范围。

带有重复的字符类

类似于带有重复的范围,字符类可以与重复一起使用,例如。

$lexer->tokenise('\U{3}');

限制

  • 模式不能包含[]{}字符。
  • 模式不能包含ASCII之外的字符。