定制 pdobrovolny/quantity 二次开发

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

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

pdobrovolny/quantity

Composer 安装命令:

composer require pdobrovolny/quantity

包简介

Physical quantities and formulas

README 文档

README

Php version

pipeline status coverage report

Donate to this project using Patreon

A PHP library to represent various physical quantities as value objects with the ability to convert between units and format them according to International System of Units (SI) or Imperial standards.

Requirements

  • PHP: ^8.5
  • Extensions:
    • ext-ds
    • ext-intl

Installation

To install, use composer:

composer require pdobrovolny/quantity

Setup & Development

Setup

Clone the repository and install dependencies:

git clone https://gitlab.com/pdobrovolny/quantity.git
cd quantity
composer install

Running Tests

The project uses Pest for testing.

vendor/bin/pest

Static Analysis

Check code quality with PHPStan:

vendor/bin/phpstan analyse

Refactoring & Code Style

The project uses Rector for automated refactoring:

vendor/bin/rector process

Project Structure

  • src/: Core library logic.
    • Container/: Quantity value object implementations.
    • Contracts/: Interfaces for quantities and factories.
    • Enums/: Enumerations for dimensions and exponents.
    • Factory/: Factories for creating quantities.
    • Formatter/: Logic for unit and quantity formatting.
  • tests/: Test suite.
    • unit/: Unit tests.
    • integration/: Integration tests.

Documentation and usage

A library to represent various quantities as value objects with the ability to convert from one Unit of Measurement to another.

International System of Units

Base units

  • Amount of substance
  • Electric current
  • Length
  • Luminous intensity
  • Mass (base unit is kilogram)
  • Thermodynamic temperature
  • Time

Derived units

  • Absorbed dose
  • Acceleration
  • Angle
  • Area
  • Capacitance
  • Catalytic activity
  • Electric resistance (Resistance)
  • Electric charge (Charge)
  • Electric conductance (Conductance)
  • Equivalent dose
  • Force
  • Frequency
  • Illuminance
  • Inductance
  • Luminous flux
  • Magnetic flux
  • Magnetic flux density
  • Power
  • Pressure
  • Radioactivity
  • Solid angle
  • Velocity
  • Voltage
  • Volume
  • Volumetric flow rate
  • Work

Sub-units

  • Angle
    • ArcMinute
    • ArcSecond
    • Degree
  • Length
    • Astronomical unit
    • Light year
    • Parsec
  • Mass
    • Atomic mass unit
    • Dalton
    • Tonne
  • Time
    • Day
    • Hour
    • Minute
  • Celsius
  • Bel
  • Hectare
  • Liter
  • Optical power
  • Volt-ampere

Imperial units

  • Area
    • Square Chain
    • Square Foot
    • Square Furlong
    • Square Inch
    • Square League
    • Square Mile
    • Square Yard
  • Length
    • Chain
    • Foot
    • Furlong
    • Inch
    • League
    • Mile
    • Yard
  • Mass
    • Grain
    • Hundredweight
    • Ounce
    • Pound
    • Stone
    • Ton
  • Volume
    • Cubic Chain
    • Cubic Foot
    • Cubic Furlong
    • Cubic Inch
    • Cubic League
    • Cubic Mile
    • Cubic Yard
    • UK
      • FluidOunce
      • Gallon
      • Gill
      • Pint
      • Quart
    • US
      • FluidOunce
      • Gallon
      • Gill
      • Pint
      • Quart
  • Fahrenheit

Examples

Main examples

Basic usage:

$length = new Length(1.);

\var_dump($length->value);                            // double(1)
\var_dump(EUnitSymbol::fromQuantity($length)->value); // string(1) "m"

Create a quantity by the factory:

$quantityFactory = new QuantityFactory();

$length = $quantityFactory->createWithValue(ILength::class, 1., EMetricExponent::KILO);

\var_dump($length->value);     // double(1000)
\var_dump($length::class);     // string(50) "PDobrovolny\Quantity\Container\Metric\Basic\Length"

Create a quantity by a formula:

$quantityFactory = new QuantityFactory();
$length = $quantityFactory->createWithValue(ILength::class, 100000);
$time = $quantityFactory->createWithValue(ITime::class, 3600);

$velocity = $quantityFactory->create(IVelocity::class, \compact('length', 'time'));

\var_dump($velocity->value);        // double(27.777777777778)
\var_dump($velocity::class);        // string(54) "PDobrovolny\Quantity\Container\Metric\Derived\Velocity"

Customization factory:

final readonly class MyLength implements ILength
{
    public function __construct(public float $value)
    {
    }
}

$quantityFactory = new QuantityFactory([
    ILength::class => autowire(MyLength::class),
]);

$length = $quantityFactory->createWithValue(ILength::class, 1.);
\var_dump($length::class);          // string(16) "example\MyLength"

Converting examples

Mile to Length:

$quantityFactory = new QuantityFactory();
$mile = $quantityFactory->createWithValue(IMile::class, 1);

$length = $quantityFactory->create(ILength::class, \compact('mile'));

\var_dump($length->value);      // double(1609.344)

Celsius to Fahrenheit:

$quantityFactory = new QuantityFactory();

$celsius = $quantityFactory->createWithValue(ICelsius::class, 20);
$thermodynamicTemperature = $quantityFactory->create(IThermodynamicTemperature::class, compact('celsius'));
$fahrenheit = $quantityFactory->create(IFahrenheit::class, compact('thermodynamicTemperature'));

\var_dump($fahrenheit->value);      // double(68)

Formatting example

Creating of QuantityFormatter:

$formatter = new QuantityFormatter(
    new \NumberFormatter('cs_CZ', \NumberFormatter::PATTERN_DECIMAL)
);

Scaling a quantity:

$quantity = new Length(1.);

echo $formatter->format($quantity) . "\n\n";

echo $formatter->format($quantity, EMetricExponent::MILLI) . "\n";
echo $formatter->format($quantity, EMetricExponent::BASE) . "\n";
echo $formatter->format($quantity, EMetricExponent::DECA) . "\n";
echo $formatter->format($quantity, EMetricExponent::KILO) . "\n";

Output:

1 m

1 000 mm
1 m
0,1 dam
0,001 km

Mass formatting (base unit is kilogram):

$quantity = new Mass(1.); // 1 kg

echo $formatter->format($quantity) . "\n";
echo $formatter->format($quantity, EMetricExponent::BASE) . "\n";
echo $formatter->format($quantity, EMetricExponent::DECI) . "\n";

Output:

1 kg
1 000 g
100 dkg

Support

I prefer to keep my work available to everyone. In order to do so I rely on voluntary contributions on Patreon.

License

This project is licensed under the MIT License. See the LICENSE file for details.

pdobrovolny/quantity 适用场景与选型建议

pdobrovolny/quantity 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.17k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 14.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-12