kwaadpepper/serial-int-caster
Composer 安装命令:
composer require kwaadpepper/serial-int-caster
包简介
Convert int To as serial number and reverse
README 文档
README
This Library allows encoding an integer to a serial number and the other way around decode it to retrieve the integer.
This library is compatible with BCMath and GMP extensions to handle large numbers.
Unit tests
Unit tests are available:
composer install
composer test
Usage
composer require kwaadpepper/serial-int-caster
For large numbers (BCMath or GMP)
Use the BCMathBaseConverter or GmpBaseConverter to handle numbers that exceed PHP's integer capacity. One of these extensions must be installed.
use Kwaadpepper\\Serial\\SerialCaster; use Kwaadpepper\\Serial\\SerialCasterBuilder; use Kwaadpepper\\Serial\\Converters\\BCMathBaseConverter; use Kwaadpepper\\Serial\\Converters\\GmpBaseConverter; use Kwaadpepper\\Serial\\Shufflers\\FisherYatesShuffler; // Using BCMathBaseConverter $int_to_encode = 9223372036854775807; // PHP_INT_MAX $seed = 1492; $length = 12; $chars = 'ABCDEFabcdef0123456789'; $caster_bcmath = (new SerialCasterBuilder(new BCMathBaseConverter())) ->withShuffler(new FisherYatesShuffler()) ->withChars($chars) ->withLength($length) ->withSeed($seed) ->build(); $encoded_number_bcmath = $caster_bcmath->encode($int_to_encode); // Prints TRUE print_r($int_to_encode === $caster_bcmath->decode($encoded_number_bcmath)); // Using GmpBaseConverter $int_to_encode = 9223372036854775807; // PHP_INT_MAX $seed = 1492; $length = 12; $caster_gmp = (new SerialCasterBuilder(new GmpBaseConverter())) ->withShuffler(new FisherYatesShuffler()) ->withChars($chars) ->withLength($length) ->withSeed($seed) ->build(); $encoded_number_gmp = $caster_gmp->encode($int_to_encode); // Prints TRUE print_r($int_to_encode === $caster_gmp->decode($encoded_number_gmp));
For small numbers (without BCMath/GMP)
If you are working with numbers that do not exceed PHP's maximum integer value (PHP_INT_MAX), you can use the NativeBaseConverter. This is a faster solution because it does not rely on external extensions, but it is limited to initial base conversions of 10 or less.
use Kwaadpepper\\Serial\\SerialCaster; use Kwaadpepper\\Serial\\SerialCasterBuilder; use Kwaadpepper\\Serial\\Converters\\NativeBaseConverter; use Kwaadpepper\\Serial\\Shufflers\\FisherYatesShuffler; $int_to_encode_native = 15; $seed = 1492; $length = 6; $chars = '01234ABCDE'; $caster_native = (new SerialCasterBuilder(new NativeBaseConverter())) ->withShuffler(new FisherYatesShuffler()) ->withChars($chars) ->withLength($length) ->withSeed($seed) ->build(); $encoded_number_native = $caster_native->encode($int_to_encode_native); // Prints TRUE print_r($int_to_encode_native === $caster_native->decode($encoded_number_native));
kwaadpepper/serial-int-caster 适用场景与选型建议
kwaadpepper/serial-int-caster 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.74k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 kwaadpepper/serial-int-caster 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kwaadpepper/serial-int-caster 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.74k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-14