承接 scriptfusion/byte-formatter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

scriptfusion/byte-formatter

Composer 安装命令:

composer require scriptfusion/byte-formatter

包简介

Formats byte values as human-readable strings.

README 文档

README

Latest version Total downloads Build status Test coverage Code style

ByteFormatter formats byte values as human-readable strings. An appropriate exponent is calculated automatically such that the value never exceeds the base. For example, in base 1024, format(1023) gives 1023 B but format(1024) gives 1 KiB instead of 1024 B.

Usage

By default, bytes are divided using Base::BINARY into multiples of 1024.

(new ByteFormatter)->format(0x80000);

512 KiB

Bytes can be divided into multiples of 1000 by specifying Base::DECIMAL as the base.

(new ByteFormatter)->setBase(Base::DECIMAL)->format(500000);

500 KB

Precision

By default, all values are rounded to the nearest integer.

(new ByteFormatter)->format(0x80233);

513 KiB

Increasing the default precision with setPrecision() allows the specified number of digits after the decimal point.

(new ByteFormatter)->setPrecision(2)->format(0x80233);

512.55 KiB

Increasing the precision will increase the maximum digits allowed, but the formatter will only display as many as needed.

(new ByteFormatter)->setPrecision(2)->format(0x80200);

512.5 KiB

Automatic precision scaling can be disabled if this behaviour is undesired.

(new ByteFormatter)->setPrecision(2)->disableAutomaticPrecision()->format(0x80200);

512.50 KiB

The default precision can be overridden by passing the second argument to format().

(new ByteFormatter)->setPrecision(2)->format(0x80233, 4);

512.5498 KiB

Significant figures

Formatting by the specified number of significant figures by calling setSignificantFigures(). This is mutually exclusive with precision scaling such that whichever method is called last will be used.

(new ByteFormatter)->setBase(Base::DECIMAL)->setSignificantFigures(2)->format(123);

120

(new ByteFormatter)->setBase(Base::DECIMAL)->setSignificantFigures(2)->format(1234);

1.2K

(new ByteFormatter)->setBase(Base::DECIMAL)->setSignificantFigures(3)->format(1234);

1.23K

This is particularly useful for keeping the display width of formatted numbers predicable.

Output format

The format can be changed by calling setFormat() which takes a string format parameter. The default format is '%v %u'. Occurrences of %v and %u in the format string will be replaced with the calculated value and units respectively.

(new ByteFormatter)->setFormat('%v%u')->format(0x80000);

512KiB

Fixed exponent

One of the main benefits of the formatter is an appropriate exponent is calculated automatically, however it is also possible to fix the exponent to a specific value using setFixedExponent().

(new ByteFormatter)->setFixedExponent(1)->format(1024 * 1024);

1024 KiB

Normally we would expect the above example to output 1 MiB but because the exponent is locked to 1 the output will always be in KiB. Consult the following table to see how exponents map to symbols.

Exponent Symbol
0 B
1 K
2 M
3 G
4 T
5 P
6 E
7 Z
8 Y

Unit customization

Units are provided by decorators extending UnitDecorator. Two implementations are provided: the default SymbolDecorator and an optional NameDecorator.

Unit decorators receive the base of the formatter when asked to decorate a value so that different units can be returned for different bases. For example, the default decorator outputs KiB in base 1024 for 210 < bytes < 220 but outputs KB in base 1000 for 1000 < bytes < 1000000. This behaviour can be suppressed by callingSymbolDecorator::setSuffix() with the desired SymbolDecorator suffix constant to prevent units changing when the base is changed. Decorators also receive the exponent and scaled byte value.

Symbol decorator

SymbolDecorator is the default unit decorator and returns units like B, KB, MB, etc. The symbol's suffix can be changed using one of the class constants from the following table.

Constant B K M G T P E Z Y
SUFFIX_NONE K M G T P E Z Y
SUFFIX_METRIC B KB MB GB TB PB EB ZB YB
SUFFIX_IEC B KiB MiB GiB TiB PiB EiB ZiB YiB

The following example uses base 1024 but displays the metric suffix, like Windows Explorer.

(new ByteFormatter(new SymbolDecorator(SymbolDecorator::SUFFIX_METRIC)))
    ->format(0x80000)

512 KB

If you prefer terse notation the suffix may be removed with SUFFIX_NONE.

(new ByteFormatter(new SymbolDecorator(SymbolDecorator::SUFFIX_NONE)))
    ->format(0x80000)

512 K

Note that no unit is displayed for bytes when the suffix is disabled. If this is undesired, byte units can be forced with SymbolDecorator::alwaysShowUnit().

(new ByteFormatter(new SymbolDecorator(SymbolDecorator::SUFFIX_NONE)))
    ->format(512)

512

(new ByteFormatter(
    (new SymbolDecorator(SymbolDecorator::SUFFIX_NONE))
        ->alwaysShowUnit()
))
    ->format(512)

512 B

Name decorator

NameDecorator can be used to replace the default decorator and returns units like byte, kilobyte, megabyte, etc.

(new ByteFormatter(new NameDecorator))
    ->format(0x80000)

512 kibibytes

Using decimal base:

(new ByteFormatter(new NameDecorator))
    ->setBase(Base::DECIMAL)
    ->format(500000)

500 kilobytes

Testing

This library is fully unit tested. Run the tests with composer test from the command line. All examples in this document can be found in DocumentationTest.

scriptfusion/byte-formatter 适用场景与选型建议

scriptfusion/byte-formatter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 326.01k 次下载、GitHub Stars 达 42, 最近一次更新时间为 2014 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 scriptfusion/byte-formatter 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 326.01k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 46
  • 点击次数: 30
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 42
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-08