2017 © Pedro Peláez
 

component unit-converter

Convert standard units from one to another with this easy to use, lightweight package

image

jordanbrauer/unit-converter

Convert standard units from one to another with this easy to use, lightweight package

  • Sunday, June 10, 2018
  • by jordanbrauer
  • Repository
  • 2 Watchers
  • 9 Stars
  • 26 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 10 Forks
  • 14 Open issues
  • 29 Versions
  • 136 % Grown

The README.md

Unit Converter

Latest Stable Version Latest Unstable Version PHP from Packagist composer.lock available license, (*1)

CI Workflow Code Maintainability Code Coverage Technical Debt Maintenance Packagist, (*2)


, (*3)

Convert all kinds of standard units of measurement from one to another with this highly customizable, easy to use, lightweight PHP component., (*4)

Table of Contents:, (*5)

  1. About the Component
  2. Installing the Component
  3. Basic Usage
  4. Documentation

1. About the Component

This unit converter component aims to be modern and follow best practices. It also aims to be fully SI compliant (eventually...)., (*6)

It supports the following types of measurement by default (support for more measurement types are on the roadmap)., (*7)

  • Area
  • Data Transfer Rates Coming Soon!
  • Digital Storage New!
  • Energy (Power)
  • Frequency
  • Fuel Economy
  • Length
  • Mass (Weight)
  • Plane Angle (Rotation)
  • Pressure
  • Speed
  • Temperature
  • Time
  • Volume

You also have the ability to override & customize the default units, as well as add your own!, (*8)

2. Installing the Component

The best way to install the component is with Composer. For other supported methods, see the wiki artile on installation., (*9)

$ composer require jordanbrauer/unit-converter

3. Basic Usage

Using the component is very easy, especially if you have used the Symfony or Laravel frameworks before., (*10)

Quick-Start

If you'd like to skip the minutiae of this component's setup and get right down to business, you can get started by constructing a pre-configured converter via static constructors or the builder object, like so,, (*11)

Static Constructors
use UnitConverter\UnitConverter;

$converter = UnitConverter::default(); # simple calculator
$converter = UnitConverter::binary(); # binary calculator (BC math)
Builder
use UnitConverter\UnitConverter;

$converter = UnitConverter::createBuilder()
    ->addSimpleCalculator()
    ->addDefaultRegistry()
    ->build();

and use it like this,, (*12)

$converter->convert(1)->from("in")->to("cm"); # (float) 2.54

and you're done! For a more in-depth setup guide, check the wiki., (*13)

Usage Examples

Here are where some usage examples of something that may fit more along the lines of "real-life", are found. Keep in mind that the code examples in each use-case, while working & valid, do contain some pseudo-code in them for demonstration purposes., (*14)

The Traffic Camera

In this example, pretend we have a traffic camera that only captures speeds in Imperial measurement of miles per hour. The traffic camera records each passing car's speed to determine if they were speeding & if so, snap a photo of their license plate as proof to serve a ticket. In this case, the camera caught a speed of 59 miles per hour., (*15)

Here we construct a new unit & give it a value representing how many of the unit exists,, (*16)

$capturedSpeed = new MilePerHour(59);

Next, a conversion of units needs to take place, because this traffic camera model is being used in a country that uses the metric system., (*17)

As you can see in this example, we are leveraging the power of typehints to ensure we only receive units of the desired measurement. Inside of the closure, we are using one of the unit's most convenient & powerful methods: as(). It allows us to convert units without the direct use of the UnitConverter & UnitRegistry objects – giving the benefit of even cleaner code & type safety., (*18)

$isOverSpeedLimit = function (SpeedUnit $speed) {
    return $speed->as(new KilometrePerHour) > 50;
};

if ($isOverSpeedLimit($capturedSpeed)) { # (bool) true
    TrafficCamera::snapPhoto();
}

Conversion Results as Words

Sometimes you might need localization support for values. This component makes that a breeze by making using the intl extension. Simply opt for using the spellout method in lieu of to. You may also provide an optional locale as the second parameter to translate., (*19)

$converter->convert(1)->from('in')->spellout('cm');       # (string) two point five four
$converter->convert(1)->from('in')->spellout('cm', 'fr'); # (string) deux virgule cinq quatre

4. Documentation

There are two kinds of in-depth documentation for this project: user & API documentation. Use whichever one you need to help answer your questions!, (*20)

User Documentation

Setup guides, in-depth examples, tutorials, and explanations on the component for users who are looking to integrate it into their project, as-is., (*21)

User Documentation, (*22)

API Documentation

If you are looking to extend and hack on this component for your own project, these pages will give you insight into all about how the component works, through the awesome power of dockblocks!, (*23)

API Documentation, (*24)

The Versions

10/06 2018

dev-master

9999999-dev

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

09/06 2018

dev-array-dot-notation

dev-array-dot-notation

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

07/05 2018

v0.7.0

0.7.0.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

07/05 2018

dev-fully-loaded-converter

dev-fully-loaded-converter

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

07/05 2018

dev-codesmell-removals

dev-codesmell-removals

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

07/05 2018

dev-debugging-tools

dev-debugging-tools

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

07/05 2018

dev-codeclimate-badges

dev-codeclimate-badges

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

07/05 2018

dev-repo-files

dev-repo-files

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

dev-debug-tools

dev-debug-tools

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

dev-time-helpers

dev-time-helpers

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

dev-si-units

dev-si-units

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

v0.6.7

0.6.7.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

v0.6.6

0.6.6.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

v0.6.5

0.6.5.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

v0.6.3

0.6.3.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

v0.6.4

0.6.4.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

v0.6.2

0.6.2.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

v0.6.2-rc

0.6.2.0-RC

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

06/05 2018

v0.6.1

0.6.1.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

13/04 2018

dev-robofile-taskrunner

dev-robofile-taskrunner

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

07/04 2018

dev-develop

dev-develop

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

05/03 2018

dev-unit-collection

dev-unit-collection

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

04/02 2018

0.6.0

0.6.0.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

01/02 2018

dev-github-templates

dev-github-templates

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

15/11 2017

dev-readme-badges

dev-readme-badges

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

by Avatar jordanbrauer

14/11 2017

0.5.1

0.5.1.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

13/11 2017

0.5.0

0.5.0.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

12/11 2017

0.4.1

0.4.1.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer

12/11 2017

0.4.0

0.4.0.0

Convert standard units from one to another with this easy to use, lightweight package

  Sources   Download

MIT

The Development Requires

by Avatar jordanbrauer