zenstruck/dimension

使用转换/人性化工具包裹数量和度量单位。

资助包维护!
kbond

v0.1.0 2022-07-21 18:12 UTC

This package is auto-updated.

Last update: 2024-08-24 13:37:17 UTC


README

CI Status codecov

使用转换/人性化工具包裹数量和度量单位。

安装

composer require zenstruck/dimension

用法

dimension 由一个 数量 (numeric) 和一个 度量单位 (string) 组成。

维度对象

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"

转换

维度对象可以转换为其他单位。以下是一些可用的转换器:

  • 质量
  • 长度
  • 温度
  • 持续时间 (时间长度)
  • 信息 (字节)
  • 通过问题/PR 提议额外的转换器

使用 convertTo() 方法执行转换

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

比较

提供多种比较方法

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

数学运算

use Zenstruck\Dimension;

$dimension = Dimension::from('45ft');

$dimension->add('6m')->toString(); // "64.69 ft"
$dimension->subtract('6m')->toString(); // "25.31 ft"

信息对象

Zenstruck\Dimension\Information 扩展了 Zenstruck\Dimension,因此它具有相同的 API,并添加了一些与人性化字节相关的附加功能。

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"

持续时间对象

Zenstruck\Dimension\Duration 扩展了 Zenstruck\Dimension,因此它具有相同的 API,并具有 人性化 持续时间的能力。

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"

桥梁

Twig 扩展

提供与 dimensioninformationduration 过滤器一起使用的 Twig 扩展。

手动激活

/* @var Twig\Environment $twig */

$twig->addExtension(new \Zenstruck\Dimension\Bridge\Twig\DimensionExtension());

Symfony 全栈激活

# config/packages/zenstruck_dimension.yaml

Zenstruck\Dimension\Bridge\Twig\DimensionExtension: ~

# If not using auto-configuration:
Zenstruck\Dimension\Bridge\Twig\DimensionExtension:
    tag: twig.extension

用法

{{ '45.458ft'|dimension.convertTo('m') }} {# "13.71 m" #}

{{ 4568897|information.humanize() }} {# "4.57 MB" #}

{{ 8540|duration.humanize() }} {# "2 hrs" #}