wearesho-team/token-generator

Library for generating random tokens

1.2.0 2023-11-29 14:31 UTC

This package is auto-updated.

Last update: 2024-10-29 16:28:48 UTC


README

Test & Lint codecov

This package should be used for generating different kinds of random token values.

Installation

composer require wearesho-team/token-generator:^1.2

Usage

Number

<?php

use Wearesho\Token;

$generator = new Token\Generator\Number($length = 5);
$value = $generator->generate();

// value will contain number with 5 digits that can be converted to int 

Char

<?php

use Wearesho\Token;

$generator = new Token\Generator\Char($length = 5, $range = ['a', 'b', 'c']);
$value = $generator->generate();

// value will contain 5 characters from passed range in random sequence. 

Numeric

<?php

use Wearesho\Token;

$generator = new Token\Generator\Numeric($length = 5);
$value = $generator->generate();

// value will contain string from 5 digits and can start with `0` 

Environment

<?php

use Wearesho\Token;

$generator = new Token\Generator\Environment($length = 5);
putenv('TOKEN_GENERATOR_VALUE=z');
$value = $generator->generate();

// value will contain 5 characters `z` that will be taken from environment.

You can also pass default generator for environment generator that will be used in case of exception

<?php

use Wearesho\Token;

$generator = new Token\Generator\Environment($length = 5, 'ENV_PREFIX_', $default = new Token\Generator\Char(5, 'a'));

// In case your environment has not key ENV_PREFIX_VALUE, default generator will be used
// It will be also used in case of any exception in Environment generator

$value = $generator->generate();

// value be generated by default generator ('aaaaa')