定制 tuupola/base62 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tuupola/base62

Composer 安装命令:

composer require tuupola/base62

包简介

Base62 encoder and decoder for arbitrary data

关键字:

README 文档

README

This library implements base62 encoding. In addition to integers it can encode and decode any arbitrary data. This is useful for example when generating url safe random tokens for database identifiers.

Latest Version Packagist Software License Build Status Coverage

Install

Install with composer.

$ composer require tuupola/base62

This branch requires PHP 7.1 or up. The older 1.x branch supports also PHP 5.6 and 7.0.

$ composer require "tuupola/base62:^1.0"

Usage

This package has both pure PHP and GMP based encoders. By default encoder and decoder will use GMP functions if the extension is installed. If GMP is not available pure PHP encoder will be used instead.

$base62 = new Tuupola\Base62();

$encoded = $base62->encode(random_bytes(128));
$decoded = $base62->decode($encoded);

If you are encoding to and from integer use the implicit decodeInteger() and encodeInteger() methods.

$integer = $base62->encodeInteger(987654321); /* 14q60P */
print $base62->decodeInteger("14q60P"); /* 987654321 */

Note that encoding a string and an integer will yield different results.

$string = $base62->encode("987654321"); /* KHc6iHtXW3iD */
$integer = $base62->encodeInteger(987654321); /* 14q60P */

Character sets

By default Base62 uses GMP style character set. Shortcut is provided for the inverted character set which is also commonly used. You can also use any custom character set of 62 unique characters.

use Tuupola\Base62;

print Base62::GMP; /* 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz */
print Base62::INVERTED; /* 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ */

$default = new Base62(["characters" => Base62::GMP]);
$inverted = new Base62(["characters" => Base62::INVERTED]);
print $default->encode("Hello world!"); /* T8dgcjRGuYUueWht */
print $inverted->encode("Hello world!"); /* t8DGCJrgUyuUEwHT */
print $default->encodeInteger(27);  /* R */
print $inverted->encodeInteger(27);  /* r */

Speed

Install GMP if you can. It is much faster pure PHP encoder. Below benchmarks are for encoding random_bytes(128) data. BCMatch encoder is also included but it is mostly just a curiosity. It is too slow to be usable.

$ php --version
PHP 8.0.7 (cli) (built: Jun  4 2021 03:50:01) ( NTS )

$ make bench

+-----------------------+------------------+-----------+
| subject               | mean             | diff      |
+-----------------------+------------------+-----------+
| benchGmpDecoder       | 140,409.997ops/s | 1.10x     |
| benchGmpDecoderCustom | 154,607.297ops/s | 1.00x     |
| benchPhpDecoder       | 721.147ops/s     | 214.39x   |
| benchBcmathDecoder    | 72.191ops/s      | 2,141.64x |
+-----------------------+------------------+-----------+

+-----------------------+------------------+-----------+
| subject               | mean             | diff      |
+-----------------------+------------------+-----------+
| benchGmpEncoder       | 352,609.309ops/s | 1.00x     |
| benchGmpEncoderCustom | 350,140.056ops/s | 1.01x     |
| benchPhpEncoder       | 669.959ops/s     | 526.31x   |
| benchBcmathEncoder    | 72.956ops/s      | 4,833.21x |
+-----------------------+------------------+-----------+

If you are sure you have GMP installed you can use the GmpEncoder directly.

if (function_exists("gmp_init")) {
    $base62 = new Tuupola\Base62\GmpEncoder();
    $base62->encode("Hello world!"); /* T8dgcjRGuYUueWht */
} else {
    /* Handle error... */
}

Static Proxy

If you prefer to use static syntax use the provided static proxy.

use Tuupola\Base62Proxy as Base62;

$encoded = Base62::encode(random_bytes(128));
$decoded = Base62::decode($encoded);

$encoded2 = Base62::encodeInteger(987654321);
$decoded2 = Base62::decodeInteger($encoded2);

Testing

You can run tests either manually or automatically on every code change. Automatic tests require entr to work.

$ make test
$ brew install entr
$ make watch

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker.

License

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

tuupola/base62 适用场景与选型建议

tuupola/base62 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.54M 次下载、GitHub Stars 达 202, 最近一次更新时间为 2016 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「base62」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tuupola/base62 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tuupola/base62 我们能提供哪些服务?
定制开发 / 二次开发

基于 tuupola/base62 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.54M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 206
  • 点击次数: 25
  • 依赖项目数: 37
  • 推荐数: 2

GitHub 信息

  • Stars: 202
  • Watchers: 6
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-05