binsoul/common-measurement

Measurements and their corresponding units and dimensions

dev-master / 1.0.x-dev 2016-04-28 17:33 UTC

This package is auto-updated.

Last update: 2022-06-14 16:56:34 UTC


README

Latest Version on Packagist Software License Total Downloads

This package provides classes for modeling and working with measurements and their corresponding units and dimensions.

Install

Via composer:

$ composer require binsoul/common-measurement

Usage

Output a length in metres.

<?php

use BinSoul\Common\Measurement\Measure;
use BinSoul\Common\Measurement\Measure\LengthMeasure;
use BinSoul\Common\Measurement\SystemOfUnits\StandardUnits;
use BinSoul\Common\Measurement\SystemOfUnits\USCustomaryUnits;

include 'vendor/autoload.php';

function output(LengthMeasure $length)
{
    $standardUnits = new StandardUnits();
    $converted = $length->to($standardUnits->METRE);

    echo sprintf(
        "%s %s = %s %s\n",
        $length->getValue(),
        $length->getUnit()->getSymbol(),
        $converted->getValue(),
        $converted->getUnit()->getSymbol()
    );
}

$standardUnits = new StandardUnits();
$usUnits = new USCustomaryUnits($standardUnits);

output(Measure::valueOf(1, $standardUnits->CENTIMETRE)); // 1 cm = 0.01 m
output(Measure::valueOf(1, $standardUnits->KILOMETRE)); // 1 km = 1000 m
output(Measure::valueOf(1, $usUnits->INCH)); // 1 in = 0.0254 m
output(Measure::valueOf(1, $usUnits->MILE)); // 1 mi = 1609.344 m

Testing

$ composer test

License

The MIT License (MIT). Please see License File for more information.