2017 © Pedro Peláez
 

library units

Converts units using a graph of conversions

image

rmasters/units

Converts units using a graph of conversions

  • Sunday, September 8, 2013
  • by rmasters
  • Repository
  • 1 Watchers
  • 2 Stars
  • 18 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 6 % Grown

The README.md

Units Latest Stable Version master Coverage Status Dependency status

Units is a small unit-of-measure conversion library written in PHP. It aims to be simple to use and extend., (*1)

Installation

Units is installable via Composer. Add the following line to your project's composer.json under the require section, and run composer update., (*2)

"rmasters/units": "dev-master"

Installable revisions of the package are listed at Packagist and documented on the releases page., (*3)

Usage

The conversion class can either be instantiated directly, or used as a singleton using the supplied facade class. Each Convert instance has its own registry of units and conversions., (*4)

Once units and conversions have been registered (see below), values can be converted as so:, (*5)

use Units\Facade as Units;

Units::convert('kg', 'lb', 42); // => 92.568

A number of standard conversions are supplied with the library. These functions register with the Facade singleton instance by default, or with a Convert instance if given., (*6)

// Defined in src/Units/conversions/
Units\register_weights(); // Metric and imperial weights
Units\register_distances(); // Metric and imperial distances

// Registering with a specific Convert instance
$convert = new Convert;
Units\register_distances($convert);

Extending with additional conversions/units

Units uses a graph model for converting between different units. For example, the edges (connections) in the graph below are defined conversions (to go from unit A to B, do X). This makes it possible to convert across a range of units without defining lots of conversions., (*7)

mg - g - kg
           \
            lb - oz
              \
               st

In this graph, to convert from mg to st is possibly by performing the intermediary conversions to g, kg and lb, without a specific mg->st conversion being defined., (*8)

To define new units and conversions, use the following code (accessed using the Facade):, (*9)

use \Units\Facade as Units;

// Register new units
Units::register(new Unit('Minute', 'min'));
Units::register(new Unit('Second', 'sec'));

// Record some one way conversions
Units::conversion('min', 'sec', function($min) { return $min * 60; });
Units::conversion('sec', 'min', function($sec) { return $sec / 60; });

// Passing a Unit instance automatically registers it, if not already registered
Units::conversion(new Unit('Hour', 'hr'), 'min', function($hr) { return $hr * 60; });
Units::conversion('min', 'hr', function($min) { return $min / 60; });

Units::convert('min', 'hr', 90); // => 1.5

The Versions

08/09 2013

dev-rule-refactoring

dev-rule-refactoring https://github.com/rmasters/units

Converts units using a graph of conversions

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

units

30/08 2013

dev-master

9999999-dev https://github.com/rmasters/units

Converts units using a graph of conversions

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

units