Skip to content

PHP package for converting weights between different units of measurement.

License

Notifications You must be signed in to change notification settings

JacobFitzp/weight-conversions

Repository files navigation

PHP Weight Conversions

Latest Version on Packagist Tests Total Downloads

Simple PHP package to convert between different units of weight measurement.

Supports gram, kilogram, milligram, ounce, pound, stone and ton... and can easily be extended to add additional units.

echo (new Pound(27.8))
    ->to(Kilogram::class)
    ->round()
    ->formatted()

// 12.6kg

Installation

You can install the package via composer:

composer require jacobfitzp/weight-conversions

Usage

// Create a new measurement unit
$kilograms = new Kilogram();

// Set weight amount
// Can also be passed to the constructor
$kilograms->setAmount(5.75);

// Convert to a different unit type
$ounce = $kilograms->to(Ounce::class);

// Get weight amount
$ounce->amount();

// Round the amount
// Defaults to a precision of 1 decimal place
$ounce->round();

// Get a formatted human-readable version of the amount
// For example "2kgG", "12st 5" 
$ounce->formatted();

Extending

You can create your own units of measurement by extending the AbstractUnit class:

class Hobnobs extends AbstractUnit
{
    public const RELATIVE_TO_KG = 50;
    
    public function formatted(string $format = '%s hobnobs'): string
    {
        return sprintf($format, $this->amount());
    }
}

Let's break down how this works... The main thing to be aware of is the RELATIVE_TO_KG constant, this represents the amount of the unit that is relative to 1kg - For exampe, above we are saying that 50 hobnobs = 1kg. This is used as the basis for all conversion calculations.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

About

PHP package for converting weights between different units of measurement.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

Contributors 3

  •  
  •  
  •  

Languages