Download the PHP package nextapps/unique-codes without Composer

On this page you can find all versions of the php package nextapps/unique-codes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package unique-codes

Unique Codes

Latest Version on Packagist Total Downloads

This package generates unique, random-looking codes. These codes can be used for vouchers, coupons, ... You can now generate thousands or millions of codes without having to check if a code has already been generated in the past.

Installation

You can install the package via composer:

Do not use v1 of this package, as it contains bugs. If you currently use v1, you should upgrade to v2 (Read the upgrading guide!).

Usage

Generate() method

To generate unique codes, you provide a start and end number. It will generate codes using the numbers in that range. Each number will always generate the same unique, random-looking code. This means you should ensure you never use the same numbers again. This could be achieved by saving which number ranges you already used or by always using the next auto-incrementing id in your codes database table.

If a lot of codes need to be generated at the same time, it can cause a lot of memory usage. In order to prevent this, a Generator is returned by default. If you want an array instead, you can set the third parameter of the generate method to true. If you want to generate one code based on one number, you only have to set the first parameter of the generate method.

Setters

Certain setters are required to generate unique codes:

setObfuscatingPrime($number)

This prime number is used to obfuscate a number between 1 and the max prime number. This prime number must be bigger than your max prime number (which you provide to the setMaxPrime method).

setMaxPrime($number)

The max prime determines the maximum amount of unique codes you can generate. If you provide 101, then you can generate codes from 1 to 100. This prime number must be smaller than the prime number you provide to the setObfuscatingPrime method.

setCharacters($string)

The character list contains all the characters that can be used to build a unique code.

setLength($number)

The length of each unique code.

setPrefix($string)

The prefix of each unique code.

setSuffix($string)

The suffix of each unique code.

setDelimiter($string, $number)

The code can be split in different pieces and glued together using the specified delimiter.

How does it work?

The code generation consists of 2 steps:

Obfuscating sequential numbers

If you encode sequential numbers, you will still see that the encoded strings are sequential. To remove the sequential nature, we use 'modular multiplicative inverse'.

You define the upper limit of your range. This determines the maximum number you can obfuscate. Then every number is mapped to a unique obfuscated number between 1 and the upper limit. You multiply the input number with a random (larger) prime number, and you determine the remainder of the division of your multiplied input number by the upper limit of the range.

Encoding the obfuscated number

In the next step, the obfuscated number is encoded to string. This is just a base conversion using division and modulo.

Testing

Linting

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

This package is heavily inspired by 2 articles written by Jim Mischel:

License

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


All versions of unique-codes with dependencies

PHP Build Version
Package Version
Requires php Version ^7.3|^7.4|^8.0|^8.1|^8.2|^8.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package nextapps/unique-codes contains the following files

Loading the files please wait ....