PHP code example of gajus / paggern

1. Go to this page and download the library: Download gajus/paggern library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gajus / paggern example snippets


$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);

$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'
    ]
]

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

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

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

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

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