nextapps/unique-codes
最新稳定版本:3.1.0
Composer 安装命令:
composer require nextapps/unique-codes
包简介
Generate unique, random-looking codes
README 文档
README
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.
use Wotz\UniqueCodes\UniqueCodes; // Generate 100 unique codes for numbers 1 to 100 $codes = (new UniqueCodes()) ->setObfuscatingPrime(9006077) ->setMaxPrime(7230323) ->setCharacters('LQJCKZMWDPTSXRGANYVBHF') ->setLength(6) ->generate(1, 100); // Result: LWXNHJ (1), LACSVK (2), QLNMNM (3), ... , LYMJHL (100), QJVBVJ (101), LQXGQC (102), ... , LJQ5DJ (7230320), LC17CS (7230321), LZ8J8H (7230322)
Installation
You can install the package via composer:
composer require wotz/unique-codes
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.
// Returns generator to create codes for numbers 1 to 100. ->generate(1, 100); // Returns array with created codes for numbers 1 to 100. ->generate(1, 100, true); // Returns string with created code for number 100. ->generate(100);
Setters
Certain setters are required to generate unique codes:
setObfuscatingPrime()setMaxPrime()setCharacters()setLength()
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
- Encoding the obfuscated number
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.
$obfuscatedNumber = ($inputNumber * $obfuscatingPrimeNumber) % $maxPrimeNumber
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
composer test
Linting
composer lint
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.
nextapps/unique-codes 适用场景与选型建议
nextapps/unique-codes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 124.32k 次下载、GitHub Stars 达 136, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「unique codes」 「voucher codes」 「coupon codes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nextapps/unique-codes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nextapps/unique-codes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nextapps/unique-codes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tesco Clubcard driver for the Omnipay payment processing library
Payment Gateway driver for DigiTickets vouchers
Pattern interpreter for generating random strings.
Laravel queue connection that prevents identical jobs from being queued
ZipField is a form field for entering and validating postal codes in ATK14 applications
Provides a HasSlug trait that will generate a unique slug when saving your Laravel Eloquent model.
统计信息
- 总下载量: 124.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 136
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 未知