zenstruck/dimension
Composer 安装命令:
composer require zenstruck/dimension
包简介
Wrap quantity and unit of measure with conversions/humanizers.
README 文档
README
Wrap quantity and unit of measure with conversions/humanizers.
Installation
composer require zenstruck/dimension
Usage
A dimension consists of a quantity (numeric) and a unit of measure (string).
Dimension Object
use Zenstruck\Dimension; // create $dimension = new Dimension(45.458, 'ft'); $dimension = Dimension::from('45.458ft'); // equivalent to above $dimension->quantity(); // 45.458 $dimension->unit(); // "ft" // render $dimension->toString(); // "45.46 ft" (max 2 decimal places) (string) $dimension; // equivalent to above $dimension->toArray(); // [45.458, "ft"] json_encode($dimension); // '[45.458, "ft"]' // use your own formatter vsprintf('%.4f%s', $dimension->toArray()); // "45.4580ft"
Conversions
A dimension object can be converted to alternate units. The following converters are available:
- Mass
- Length
- Temperature
- Duration (length of time)
- Information (bytes)
- Propose additional converters via issue/PR
Use the convertTo() method to perform conversions:
use Zenstruck\Dimension; $dimension = Dimension::from('45ft'); $converted = $dimension->convertTo('m'); // Zenstruck\Dimension $converted->quantity(); // 13.716 $converted->unit(); // "m" $converted->toString(); // "13.71 m" $dimension->convertTo('g'); // throws ConversionNotPossible - cannot convert feet to grams
Comparisons
Several comparison methods are available:
use Zenstruck\Dimension; $dimension = Dimension::from('45ft'); $dimension->isEqualTo('6m'); // false $dimension->isLargerThan('6m'); // true $dimension->isLargerThanOrEqualTo('6m'); // true $dimension->isSmallerThan('6m'); // false $dimension->isSmallerThanOrEqualTo('6m'); // false $dimension->isWithin('6m', '1km'); // true $dimension->isOutside('6m', '1km'); // false
Mathematical Operations
use Zenstruck\Dimension; $dimension = Dimension::from('45ft'); $dimension->add('6m')->toString(); // "64.69 ft" $dimension->subtract('6m')->toString(); // "25.31 ft"
Information Object
Zenstruck\Dimension\Information extends Zenstruck\Dimension so it has the same API with
some additional features related to humanizing bytes.
use Zenstruck\Dimension\Information; $info = Information::from('4568897B'); $info = Information::from(4568897); // equivalent to above (can create from bytes directly) $info->bytes(); // 4568897 // "humanize" (string) $info->humanize(); // "4.57 MB" (defaults to the decimal system) (string) $info->asBinary()->humanize(); // "4.36 MiB" (convert to binary system before humanizing) (string) Information::binary(4568897)->humanize(); // "4.36 MiB" (explicitly create in binary system) // when creating with a unit of measure, the system is detected (string) Information::from('4570 kb')->humanize(); // "4.57 MB" (string) Information::from('4570 KiB')->humanize(); // "4.46 MiB"
Duration Object
Zenstruck\Dimension\Duration extends Zenstruck\Dimension so it has the same API with
the ability to humanize a duration.
use Zenstruck\Dimension\Duration; $duration = Duration::from('8540 s'); $duration = Duration::from(8540); // equivalent to above (can create from seconds directly) (string) $duration->humanize(); // "2 hrs" (string) Duration::from(0)->humanize(); // "0 secs" (string) Duration::from(1)->humanize(); // "1 sec" (string) Duration::from(10)->humanize(); // "10 secs" (string) Duration::from(65)->humanize(); // "1 min"
Bridge
Twig Extension
A Twig extension is provided with dimension, information, and duration filters.
Manual Activation:
/* @var Twig\Environment $twig */ $twig->addExtension(new \Zenstruck\Dimension\Bridge\Twig\DimensionExtension());
Symfony full-stack activation:
# config/packages/zenstruck_dimension.yaml Zenstruck\Dimension\Bridge\Twig\DimensionExtension: ~ # If not using auto-configuration: Zenstruck\Dimension\Bridge\Twig\DimensionExtension: tag: twig.extension
Usage:
{{ '45.458ft'|dimension.convertTo('m') }} {# "13.71 m" #}
{{ 4568897|information.humanize() }} {# "4.57 MB" #}
{{ 8540|duration.humanize() }} {# "2 hrs" #}
zenstruck/dimension 适用场景与选型建议
zenstruck/dimension 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.3k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2022 年 07 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「convert」 「unit」 「measure」 「dimension」 「humanize」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zenstruck/dimension 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zenstruck/dimension 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zenstruck/dimension 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Akeneo Measure Bundle
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
Provides a simple decimal/roman number converter
Zend Framework 1 Measure package
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Symfony bundle for unigen test generator
统计信息
- 总下载量: 1.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-21