Convert all kinds of standard units of measurement from one to another with this highly customizable, easy to use, lightweight PHP component.
Table of Contents:
This unit converter component aims to be modern and follow best practices. It also aims to be fully SI compliant (eventually...).
It supports the following types of measurement by default (support for more measurement types are on the roadmap).
- 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!
The best way to install the component is with Composer. For other supported methods, see the wiki artile on installation.
$ composer require jordanbrauer/unit-converter
Using the component is very easy, especially if you have used the Symfony or Laravel frameworks before.
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,
use UnitConverter\UnitConverter;
$converter = UnitConverter::default(); # simple calculator
$converter = UnitConverter::binary(); # binary calculator (BC math)
use UnitConverter\UnitConverter;
$converter = UnitConverter::createBuilder()
->addSimpleCalculator()
->addDefaultRegistry()
->build();
and use it like this,
$converter->convert(1)->from("in")->to("cm"); # (float) 2.54
and you're done! For a more in-depth setup guide, check the wiki.
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.
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.
Here we construct a new unit & give it a value representing how many of the unit exists,
$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.
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.
$isOverSpeedLimit = function (SpeedUnit $speed) {
return $speed->as(new KilometrePerHour) > 50;
};
if ($isOverSpeedLimit($capturedSpeed)) { # (bool) true
TrafficCamera::snapPhoto();
}
There are two kinds of in-depth documentation for this project: user & API documentation. Use whichever one you need to help answer your questions!
Setup guides, in-depth examples, tutorials, and explanations on the component for users who are looking to integrate it into their project, as-is.
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!