pdobrovolny/quantity

Physical quantities and formulas

Fund package maintenance!
Patreon

Installs: 8 617

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Forks: 0

2.1.0 2022-01-16 11:08 UTC

This package is auto-updated.

Last update: 2024-04-07 22:03:28 UTC


README

Php version

pipeline status coverage report

Donate to this project using Patreon

Physical quantities and formulas

Installation

To install, use composer:

composer require pdobrovolny/quantity

Documentation and usage

A library to represent various quantities as value objects with the ability to convert from one Unit of Measurement to another.

International System of Units

Base units

  • Amount of substance
  • Electric current
  • Length
  • Luminous intensity
  • Mass
  • Thermodynamic temperature
  • Time

Derived units

  • Absorbed dose
  • Acceleration
  • Angle
  • Area
  • Capacitance
  • Catalytic activity
  • Electric resistance
  • Electric charge
  • Electric conductance
  • Equivalent dose
  • Force
  • Frequency
  • Illuminance
  • Inductance
  • Luminous flux
  • Magnetic flux
  • Magnetic flux density
  • Power
  • Pressure
  • Radioactivity
  • Solid angle
  • Velocity
  • Voltage
  • Volume
  • Volumetric flow rate
  • Work

Sub-units

  • Angle
    • ArcMinute
    • ArcSecond
    • Degree
  • Length
    • Astronomical unit
    • Light year
    • Parsec
  • Mass
    • Atomic mass unit
    • Tonne
  • Time
    • Day
    • Hour
    • Minute
  • Celsius
  • Hectare
  • Liter
  • Optical power
  • Volt-ampere

Imperial units

  • Area
    • Square Chain
    • Square Foot
    • Square Furlong
    • Square Inch
    • Square League
    • Square Mile
    • Square Yard
  • Length
    • Chain
    • Foot
    • Furlong
    • Inch
    • League
    • Mile
    • Yard
  • Mass
    • Grain
    • Hundredweight
    • Ounce
    • Pound
    • Stone
    • Ton
  • Volume
    • Cubic Chain
    • Cubic Foot
    • Cubic Furlong
    • Cubic Inch
    • Cubic League
    • Cubic Mile
    • Cubic Yard
    • UK
      • FluidOunce
      • Gallon
      • Gill
      • Pint
      • Quart
    • US
      • FluidOunce
      • Gallon
      • Gill
      • Pint
      • Quart
  • Fahrenheit

Examples

Main examples

Basic usage:

$length = new Length(1.);

\var_dump($length->getValue());     // double(1)
\var_dump($length::UNIT);           // string(1) "m"

Create a quantity by the factory:

$quantityFactory = new QuantityFactory();

$length = $quantityFactory->createWithValue(ILength::class, 1., EMetricExponent::KILO);

\var_dump($length->getValue());     // double(1000)
\var_dump($length::class);          // string(50) "pdobrovolny\quantity\container\metric\basic\Length"

Create a quantity by a formula:

$quantityFactory = new QuantityFactory();
$length = $quantityFactory->createWithValue(ILength::class, 100000);
$time = $quantityFactory->createWithValue(ITime::class, 3600);

$velocity = $quantityFactory->create(IVelocity::class, [$length, $time]);

\var_dump($velocity->getValue());   // double(27.777777777778)
\var_dump($velocity::class);        // string(54) "pdobrovolny\quantity\container\metric\derived\Velocity"

Customization factory:

final class MyLength implements ILength
{
    public function __construct(private float $value)
    {
    }

    public function getValue(): float
    {
        return $this->value;
    }
}

$quantityFactory = new QuantityFactory([
    ILength::class => autowire(MyLength::class),
]);

$length = $quantityFactory->createWithValue(ILength::class, 1.);
\var_dump($length::class);          // string(16) "example\MyLength"

Converting examples

Mile to Length:

$quantityFactory = new QuantityFactory();
$mile = $quantityFactory->createWithValue(IMile::class, 1);

$length = $quantityFactory->create(ILength::class, [$mile]);

\var_dump($length->getValue());     // double(1609.344)

Celsius to Fahrenheit:

$quantityFactory = new QuantityFactory();

$celsius = $quantityFactory->createWithValue(ICelsius::class, 20);
$thermodynamicTemperature = $quantityFactory->create(IThermodynamicTemperature::class, [$celsius]);
$fahrenheit = $quantityFactory->create(IFahrenheit::class, [$thermodynamicTemperature]);

\var_dump($fahrenheit->getValue());     // double(68)

Formatting example

Creating of QuantityFormatter:

$formatter = new QuantityFormatter(
    new \NumberFormatter('cs_CZ', \NumberFormatter::PATTERN_DECIMAL)
);

Scaling a quantity:

$quantity = new Length(1.);

echo $formatter->format($quantity) . "\n\n";

echo $formatter->format($quantity, EMetricExponent::MILI) . "\n";
echo $formatter->format($quantity, EMetricExponent::BASE) . "\n";
echo $formatter->format($quantity, EMetricExponent::DEKA) . "\n";
echo $formatter->format($quantity, EMetricExponent::KILO) . "\n";

Output:

1 m

1 000 mm
1 m
0,1 dam
0,001 km

Support

I prefer to keep my work available to everyone. In order to do so I rely on voluntary contributions on Patreon.