定制 webboy/measurement-units 二次开发

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

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

webboy/measurement-units

Composer 安装命令:

composer require webboy/measurement-units

包简介

Set of DTOs to deal with various physical units and conversions.

README 文档

README

Latest Stable Version Total Downloads Build Status Coverage Status PHP Version License PHPStan Level

Set of DTOs to deal with various physical units and conversions.

This repository contains a collection of Data Transfer Objects (DTOs) that are designed to handle various physical unit representations and conversions effectively. The DTOs ensure consistent unit management, making it easier to work with different measurement systems in software applications.

Installation

To install the DTOs, you can use the following command:

composer require webboy/measurement-units

Usage

The DTOs are designed to be easy to use and understand. Here is an example of how you can use the Distance DTO:

use Webboy\MeasurementUnits\Measurements\DistanceMeasurementDto;

// Create a new distance measurement.
$measurement = new DistanceMeasurementDto();

// Set the value of the measurement.
$distance_value = $measurement->createValue(100, DistanceUnitEnum::KILOMETER->value);

// Print the distance value.
echo ("My distance in km is: " . $distance_value . PHP_EOL);

// Convert the distance value to meters.
$converted_distance_value = $distance_value->to(DistanceUnitEnum::MILE->value);

// Print the converted distance value.
echo ("My distance in miles is: " . $converted_distance_value . PHP_EOL);

Available DTOs

The following DTOs are available in this package:

  • AreaMeasurementDto
  • DistanceMeasurementDto
  • MassMeasurementDto
  • SpeedMeasurementDto
  • TemperatureMeasurementDto
  • VolumeMeasurementDto
  • PressureMeasurementDto
  • TorqueMeasurementDto
  • PowerMeasurementDto
  • FuelConsumptionMeasurementDto
  • BatteryCapacityMeasurementDto
  • VoltageMeasurementDto

Customizing the DTOs

The DTOs are designed to be easily customizable. You can extend the DTOs to add new units or modify existing ones. Here is an example of how you can extend the Distance DTO:

use Webboy\MeasurementUnits\Enums\Units\DistanceUnitEnum;
use Webboy\MeasurementUnits\Measurements\DistanceMeasurementDto;

// Create a new CUSTOM distance measurement with a custom base unit.
$measurement = new CustomMeasurementDto(
    'custom',
    'Length',
    'ft',
    [
        // You can use a DistanceUnitEnum value as the ID.
        new Webboy\MeasurementUnits\Units\DistanceUnitDto(DistanceUnitEnum::FOOT),

        // You can use a CustomUnitDto to create a completely custom unit.
        new Webboy\MeasurementUnits\Units\CustomUnitDto(
            id: 'miles',
            name: 'Miles',
            symbol: 'mi',
            toBase: fn($value) => $value * 5280,
            fromBase: fn($value) => $value / 5280,
            validIds: false
        )
    ]
);

// Set the value of the measurement.
$distance_value = $measurement->createValue(1000, DistanceUnitEnum::FOOT->value);

// Print the value.
echo ("My distance in feet is: " . $distance_value . PHP_EOL);

// Convert the value.
$converted_distance_value = $distance_value->to('miles');

// Print the converted value.
echo ("My distance in miles is: " . $converted_distance_value . PHP_EOL);

Fancy Customization

You can also use the CustomUnitDto to create a completely custom unit. This allows you to define your own conversion functions and custom units. Here is an example of how you can use the CustomUnitDto:

use Webboy\MeasurementUnits\Measurements\CustomMeasurementDto;
use Webboy\MeasurementUnits\Units\CustomUnitDto;

// Create a new CUSTOM measurement with a custom base unit.
$wizardness = new CustomMeasurementDto(
    id:'Wizardness',
    name: 'Wizardness level',
    units: [
        // Set the base unit.
        new CustomUnitDto(
            'merlin',
            'Merlin',
            'MRLN',
            fn($value) => $value,
            fn($value) => $value,
            true
        ),
        new CustomUnitDto(
            'harry',
            'Harry',
            'HRRY',
            fn($value) => $value / 3.223,
            fn($value) => $value * 3.223,
        ),
        new CustomUnitDto(
            'gandalf',
            'Gandalf',
            'GND',
            fn($value) => $value * 2.122,
            fn($value) => $value / 2.122,
        ),
    ]
);

// Set the value of the measurement.
$wizardness_value = $wizardness->createValue(1, 'gandalf');

// Print the wizardness value.
echo ("My wizardness in gandalf is: " . $wizardness_value . PHP_EOL);

// Convert the wizardness value to harry.
$converted_wizardness_value = $wizardness_value->to('harry');

// Print the converted wizardness value.
echo ("My wizardness in harry is: " . $converted_wizardness_value . PHP_EOL);

Extending the DTOs

You can also extend the DTOs to add new functionality or modify existing behavior. Here is an example of how you can extend the Distance DTO:

class FuelCapacity extends CustomMeasurementDto
{
    // It will load unit definitions from a Definitions/FuelCapacity/FuelCapacityDefinitions.php file.
    public function __construct()
    {
        parent::__construct(
            id:'fuel-capacity',
            name:'Fuel Capacity',
            base_unit_id:VolumeUnitEnum::LITRE->value,
            units: [
                new Webboy\MeasurementUnits\Units\VolumeUnitDto(VolumeUnitEnum::LITRE),
                new Webboy\MeasurementUnits\Units\VolumeUnitDto(VolumeUnitEnum::GALLON),
            ]

        );
    }
}

// Create a new value with a custom measurement.
$measurement = new FuelCapacity();

$fuel_capacity = $measurement->createValue(100, VolumeUnitEnum::GALLON->value);

// Print the fuel capacity value.
echo ("My fuel capacity in gallons is: " . $fuel_capacity . PHP_EOL);

// Convert the fuel capacity value to liters.
$converted_fuel_capacity = $fuel_capacity->to(VolumeUnitEnum::LITRE->value);

// Print the converted fuel capacity value.
echo ("My fuel capacity in liters is: " . $converted_fuel_capacity . PHP_EOL);

Even more fancy extensions

You can also extend the existing units, or extend the basic units to create new ones. You can also create new DTOs that extend the existing ones. Here is an example of how you can create your own measurements and units

class MyVolumeUnit extends UnitDto
{
    public function __construct()
    {
        parent::__construct(
            id:'my-volume-unit',
            name: 'My Volume Unit',
            symbol: 'mvu',
            toBase: fn($value) => $value,
            fromBase: fn($value) => $value,
            isBase: true,
            validIds: false
        );
    }
}

class MyKiloVolumeUnit extends UnitDto
{
    public function __construct()
    {
        parent::__construct(
            id:'my-kilo-volume-unit',
            name: 'My Kilo Volume Unit',
            symbol: 'Kmvu',
            toBase: fn($value) => $value * 1000,
            fromBase: fn($value) => $value / 1000,
        );
    }
}

class ImaginaryMeasurement extends MeasurementDto
{
    // It will load unit definitions from a Definitions/FuelCapacity/FuelCapacityDefinitions.php file.
    public function __construct()
    {
        parent::__construct(
            id: 'imaginary-measurement',
            name: 'ImaginaryMeasurement',
            units:[
                new MyVolumeUnit(),
                new MyKiloVolumeUnit()
            ]
        );
    }
}

// Create a new value with a custom measurement.
$measurement = new ImaginaryMeasurement();

$value = $measurement->createValue(100, 'my-volume-unit');

// Print the value.
echo ("My value in {$value->unit->symbol} is: " . $value . PHP_EOL);

// Convert value to my-kilo-volume-unit.
$converted_value = $value->to('my-kilo-volume-unit');

// Print the converted value.
echo ("My fuel value in {$converted_value->unit->symbol} is: " . $converted_value . PHP_EOL);

Testing

To run the tests, you can use the following command:

composer run-script test

Contributing

If you would like to contribute to this project, please feel free to submit a pull request. We welcome contributions

webboy/measurement-units 适用场景与选型建议

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

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

围绕 webboy/measurement-units 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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