swindon/code-generator
Composer 安装命令:
composer require swindon/code-generator
包简介
Random code generator
README 文档
README
Install:
composer require swindon/code-generator
Usage:
Single Code Generation:
/** * Generates a random code * * @param $length int * @return string */ ->generate(int $length = 8) : string
Example #1 Generates 12 character long string
(new CodeGenerator)->generate(12); // output: 4CF9O7XP
Example #2 Generates 8 character long string, with alpha characters only (lower and upper case)
(new CodeGenerator)->setCharacters(CodeGenerator::CHAR_ALPHA)->generate(8); // output: JTFRujQJ
Example #3 Generates 10 character long string, with all characters and symbols, removing ambiguous characters (eg S, 5, 0, O etc.)
(new CodeGenerator)->setCharacters(CodeGenerator::CHAR_ALL)->generate(10); // output: J\TTu@<ab4
Example #4 Generates 6 character long string, using only characters stated
(new CodeGenerator)->setCharacters('ABCXYZabcxyz')->generate(6); // output: XcZxbc
Batch Code Generation:
/** * Generates an array of random codes * * @param $maxNum int * @param $length int * @return array */ ->bulk(int $maxNum, int $length = 8) : array
Example #1 Generates array of 6 x 8 character long unique codes
(new CodeGenerator)->bulk(6) // output: [ // "d3MEJqNq", // "Dr4mYKxP", // "J0RqiCJ9", // "NZuaPUVC", // "aZgMjn2m", // "EnyQKrat", // ];
Example #2 Generates array of 6 x 6 character long unique codes appended to original array of codes
(new CodeGenerator)->bulk(3, 6) // output: [ // "OsphQo", // "LPKCs2", // "DCv0QS", // ];
Example #3 Generates array of 6 x 8 character long unique codes, using only characters stated
(new CodeGenerator)->setCharacters('ABCXYZabcxyz')->bulk(6) // output: [ // "xcXAcYbb", // "YZXAYBBX", // "ZZzYxZyy", // "ZYZacACa", // "BYzzBYaB", // "xZxbacAz", // ];
Exceptions
CodeGeneratorException
The CodeGeneratorException can be thrown for multiple reasons, on main reason for this will be from the bulk generation method. If the amount to codes required to be generated exceeds the amount which can be generated (based on character set and code length) then an exception will be thrown.
Example Try generating array of 100 x 3 character long unique codes, using only characters stated
(new CodeGenerator)->setCharacters('ABC')->bulk(100, 3); // throws: 'Cannot generate more than 27 possible unique codes. Try increasing the code length.'
Show some support!
swindon/code-generator 适用场景与选型建议
swindon/code-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 swindon/code-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 swindon/code-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 49
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-18