Skip to content

binsoul/common-measurement

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

common-measurement

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.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages