PHPackages                             vesper/unit-conversion - 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. vesper/unit-conversion

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

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.

1.0.2(1y ago)34MITPHPPHP ^8.3CI passing

Since Apr 4Pushed 1y ago3 watchersCompare

[ Source](https://github.com/hivesper/unit-conversion)[ Packagist](https://packagist.org/packages/vesper/unit-conversion)[ Docs](https://github.com/hivesper/unit-conversion)[ RSS](/packages/vesper-unit-conversion/feed)WikiDiscussions master Synced 1mo ago

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

Unit Conversion Library for PHP
===============================

[](#unit-conversion-library-for-php)

Introduction
------------

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

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

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

[](#installation)

Install via Composer:

```
  composer require vesper/unit-conversion
```

Usage
-----

[](#usage)

### Parsing a Unit

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

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

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

[](#creating-and-adding-custom-units)

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

#### Using `RegistryBuilder::build()`

[](#using-registrybuilderbuild)

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

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

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

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

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

[](#license)

This package is open-source and available under the MIT License. See the [LICENSE](./LICENSE) file for more details.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance48

Moderate activity, may be stable

Popularity7

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% 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 ~6 days

Total

3

Last Release

397d ago

### Community

Maintainers

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

---

Top Contributors

[![vovabndrnk](https://avatars.githubusercontent.com/u/156594454?v=4)](https://github.com/vovabndrnk "vovabndrnk (74 commits)")[![tomhooijenga](https://avatars.githubusercontent.com/u/1853235?v=4)](https://github.com/tomhooijenga "tomhooijenga (69 commits)")[![egedarici](https://avatars.githubusercontent.com/u/65875411?v=4)](https://github.com/egedarici "egedarici (5 commits)")

---

Tags

conversionlaravelunitvesperunitconversionmeasurement

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/vesper-unit-conversion/health.svg)

```
[![Health](https://phpackages.com/badges/vesper-unit-conversion/health.svg)](https://phpackages.com/packages/vesper-unit-conversion)
```

###  Alternatives

[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[php-unit-conversion/php-unit-conversion

A fully PSR-4 compatible PHP library for converting between standard units of measure.

681.2M1](/packages/php-unit-conversion-php-unit-conversion)[florianv/swap

Exchange rates library for PHP

1.3k6.4M16](/packages/florianv-swap)[spatie/color

A little library to handle color conversions

38118.9M28](/packages/spatie-color)[php-units-of-measure/php-units-of-measure

A PHP library for converting between standard units of measure.

3123.4M20](/packages/php-units-of-measure-php-units-of-measure)[florianv/laravel-swap

Currency exchange rates library for Laravel and Lumen

3342.0M2](/packages/florianv-laravel-swap)

PHPackages © 2026

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