PHPackages                             webboy/measurement-units - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. webboy/measurement-units

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

webboy/measurement-units
========================

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

1.4.1(1y ago)1417MITPHPPHP ^8.2CI failing

Since Jan 12Pushed 1y ago1 watchersCompare

[ Source](https://github.com/webboy/measurement-units)[ Packagist](https://packagist.org/packages/webboy/measurement-units)[ RSS](/packages/webboy-measurement-units/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (24)Used By (0)

Measurement Units
=================

[](#measurement-units)

[![Latest Stable Version](https://camo.githubusercontent.com/d22fc193e2086b4ba14e7eb5460cf7f9673fccdfc12767e106948bab82bfc972/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f776562626f792f6d6561737572656d656e742d756e6974732e737667)](https://packagist.org/packages/webboy/measurement-units)[![Total Downloads](https://camo.githubusercontent.com/945b9453644205d8ab15a406bfeaf807d7ce0af3f20dce22ffdabf28142180b5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f776562626f792f6d6561737572656d656e742d756e6974732e737667)](https://packagist.org/packages/webboy/measurement-units)[![Build Status](https://github.com/webboy/measurement-units/actions/workflows/php.yml/badge.svg)](https://github.com/webboy/measurement-units/actions)[![Coverage Status](https://camo.githubusercontent.com/0f2c335fcaa252e5dc499d4a7b050c6568b501286eccca38400313cd9b160a48/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f776562626f792f6d6561737572656d656e742d756e6974732f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/webboy/measurement-units?branch=main)[![PHP Version](https://camo.githubusercontent.com/850f602f0b6ccbfb91448828198b71231b0c86eca36fe00c0c9df414f61d841a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f776562626f792f6d6561737572656d656e742d756e6974732e737667)](https://packagist.org/packages/webboy/measurement-units)[![License](https://camo.githubusercontent.com/0f400ccccfc51e82f2f24ac52e16b774b0a120550cfb28dfb9f5de5079f33844/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f776562626f792f6d6561737572656d656e742d756e6974732e737667)](https://packagist.org/packages/webboy/measurement-units)[![PHPStan Level](https://camo.githubusercontent.com/6b52a7f665fea8d365884df5ba6592bc1c4e033253b2225cacc617e1ac07d944/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d737563636573733f7374796c653d666c6174)](https://camo.githubusercontent.com/6b52a7f665fea8d365884df5ba6592bc1c4e033253b2225cacc617e1ac07d944/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d737563636573733f7374796c653d666c6174)

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
------------

[](#installation)

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

```
composer require webboy/measurement-units
```

Usage
-----

[](#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
--------------

[](#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
--------------------

[](#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
-------------------

[](#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
------------------

[](#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
--------------------------

[](#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
-------

[](#testing)

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

```
composer run-script test
```

Contributing
------------

[](#contributing)

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

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance50

Moderate activity, may be stable

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Recently: every ~30 days

Total

18

Last Release

365d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a51d3fc2b38653d6a521a0e32585fc99036711f9aa89a59200c6891abaad99e3?d=identicon)[webboy](/maintainers/webboy)

---

Top Contributors

[![webboy](https://avatars.githubusercontent.com/u/1636797?v=4)](https://github.com/webboy "webboy (46 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/webboy-measurement-units/health.svg)

```
[![Health](https://phpackages.com/badges/webboy-measurement-units/health.svg)](https://phpackages.com/packages/webboy-measurement-units)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
