creativecrafts/laravel-secure-random-number-generator

A handy package to generate a secure unique random number for a model

1.0.0 2024-03-17 06:22 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A handy package to generate a secure unique random number for a model.

Installation

You can install the package via composer:

composer require creativecrafts/laravel-secure-random-number-generator

You can publish the config file with:

php artisan vendor:publish --tag="secure-random-number-generator-config"

This is the contents of the published config file:

return [
    /*
    |--------------------------------------------------------------------------
    | Default Number Range
    |--------------------------------------------------------------------------
    |
    | This option controls the default number range that will be used
    | when generating a random number.
    |
    */

    'from_number_range' => 1,
    'to_number_range' => 9999999,
];

Usage

use CreativeCrafts\SecureRandomNumberGenerator\SecureRandomNumberGenerator;
 // Generate a random unique number for a model
 // To make the number unique for a model, the package uses the model's table name and column name to check if the number is unique in the model's table
 // The from and to number range can be empty and the package will use the default number range set in the configuration file
 $tableName = 'users'
 $tableColumn = 'registration_number';
 $fromNumberRange = 100;
 $toNumberRange = 9999999;
 
 $secureUniqueRandomNumber = SecureRandomNumberGenerator::forModel($fromNumberRange, $toNumberRange, $tableName, $tableColumn,)->generate();
 
 // Generate a random number with a default value set in the configuration file
 $secureRandomNumber = SecureRandomNumberGenerator::useDefaultConfigNumberRange()->generate();
 
 // Generate a random number with a custom number range
 $fromNumberRange = 100;
 $toNumberRange = 9999999;
 $secureRandomNumber = SecureRandomNumberGenerator::setNumberRange($fromNumberRange, $toNumberRange)->generate();
 
 // Generate a random unique number for a model using the default number range set in the configuration file
 $tableName = 'users'
 $tableColumn = 'registration_number';
 
 $secureUniqueRandomNumber = SecureRandomNumberGenerator::forModelUsingDefaultConfigNumberRange($tableName, $tableColumn)->generate();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.