定制 vesper/unit-conversion 二次开发

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

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

vesper/unit-conversion

Composer 安装命令:

composer require vesper/unit-conversion

包简介

This package provides a powerful and flexible way to handle unit conversion in PHP. It supports parsing units from strings, performing conversions, and working with both simple and compound units.

README 文档

README

Introduction

This package provides a powerful and flexible way to handle unit conversion in PHP. It supports parsing units from strings, performing conversions, and working with both simple and compound units.

Supported Conversions

Our library supports a wide range of unit conversions, covering both fundamental and derived physical quantities.

  • 📏 Length (Distance)
  • 📐 Area
  • ⚖️ Mass
  • Time
  • 🌡 Temperature
  • Energy
  • 🔥 Power
  • 📊 Pressure
  • 📡 Frequency
  • 🏋️ Force
  • 🔆 Luminous Intensity
  • 🧪 Amount of Substance
  • 🥤 Volume

Why Use This Library?

  • Simple string-based unit parsing
  • Supports SI prefixes and derived units
  • Handles compound unit conversions (e.g., speed, density, force)
  • Extendable - Easily add new units and dimensions
  • Integration with Laravel

Installation

Install via Composer:

  composer require vesper/unit-conversion

Usage

Parsing a Unit

use Vesper\UnitConversion\Parser;
use Vesper\UnitConversion\Registry;

$registry = new Registry();
$parser = new Parser($registry);

$unit = $parser->parse('m/s'); // Returns an instance of Unit

Regular Conversion

use Vesper\UnitConversion\Converter;

$converter = new Converter();
$cm = $parser->parse('cm');
$m = $parser->parse('m');

$result = $converter->convert($cm, $m, 100); // Converts 100cm to 1m

Compound Unit Conversion

$ms = $parser->parse('m/s');
$kmh = $parser->parse('km/h');

$result = $converter->convert($ms, $kmh, 10); // Converts 10m/s to 36km/h

Creating and Adding Custom Units

The RegistryBuilder class provides a set of predefined units and allows for easy extension.

Using RegistryBuilder::build()

The build() method initializes a Registry instance with common units like length, mass, time, temperature, force, etc.

use Vesper\UnitConversion\Registry;
use Vesper\UnitConversion\RegistryBuilder;

$registry = new Registry();
RegistryBuilder::build($registry);

This method ensures that a wide range of units are available for use immediately.

Registering a Custom Unit

The register() method is used to add new units to the registry, and alias() allows you to specify alternative names.

use Vesper\UnitConversion\Registry;
use Vesper\UnitConversion\Unit;
use Vesper\UnitConversion\Dimension;
use Vesper\UnitConversion\UnitPart;

$registry = new Registry();

$registry->register(
    'furlong',
    new Unit(new UnitPart(201.168, Dimension::LENGTH, 1) // 1 furlong = 201.168 meter
);
$registry->alias('furlong', ['fur']);

Using registerSiUnit()

The registerSiUnit() method allows you to define SI units along with their prefixed versions (e.g., kilogram, milligram, etc.).

use Vesper\UnitConversion\RegistryBuilder;
use Vesper\UnitConversion\Unit;
use Vesper\UnitConversion\UnitPart;
use Vesper\UnitConversion\Dimension;

RegistryBuilder::registerSiUnit(
    $registry,
    'gram', // Base unit
    ['g'], // Aliases
    new Unit(new UnitPart(0.001, Dimension::MASS, 1)) // 1 gram = 0.001 kilogram
);

This automatically registers gram and generates prefixed versions like kilogram (kg), milligram (mg), etc.

Using in Laravel

This package provides a service provider, ConversionServiceProvider, which is auto-discovered by Laravel. This means you don’t need to manually register it. ConversionServiceProvider include Converter, Parser and Registry classes.

Additionally, a facade Converter is provided for easy access.

Example Usage

use Vesper\UnitConversion\Converter;
use Vesper\UnitConversion\Parser;

class UnitController
{
    public function __construct(
        protected Converter $converter,
        protected Parser $parser
    ) {}

    public function convert()
    {
        $from = $this->parser->parse('kg');
        $to = $this->parser->parse('g');
        $value = 5;

        return $this->converter->convert($from, $to, $value); // Converts 5kg to 5000g
    }
}

License

This package is open-source and available under the MIT License. See the LICENSE file for more details.

vesper/unit-conversion 适用场景与选型建议

vesper/unit-conversion 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 vesper/unit-conversion 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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