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.
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
- 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
Install via Composer:
composer require vesper/unit-conversionuse Vesper\UnitConversion\Parser;
use Vesper\UnitConversion\Registry;
$registry = new Registry();
$parser = new Parser($registry);
$unit = $parser->parse('m/s'); // Returns an instance of Unituse Vesper\UnitConversion\Converter;
$converter = new Converter();
$cm = $parser->parse('cm');
$m = $parser->parse('m');
$result = $converter->convert($cm, $m, 100); // Converts 100cm to 1m$ms = $parser->parse('m/s');
$kmh = $parser->parse('km/h');
$result = $converter->convert($ms, $kmh, 10); // Converts 10m/s to 36km/hThe RegistryBuilder class provides a set of predefined units and allows for easy extension.
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.
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']);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.
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.
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
}
}This package is open-source and available under the MIT License. See the LICENSE file for more details.