pdobrovolny/quantity

Physical quantities and formulas

1.2.0 2018-09-15 21:21 UTC

README

Php version Latest Stable Version Latest Unstable Version composer.lock License

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 = Length::main(1.);

\var_dump($length->getMainValue());     // double(1)            Main scale of the unit
\var_dump($length->getUnit());          // string(1) "m"
\var_dump((string)$length);             // string(2) "1m"       (debug only)

Scaling (scalable units only):

$length = Length::centi(1.)->asMili();

\var_dump($length->getMainValue());     // double(0.01)         Main scale of the unit
\var_dump($length->getValue());         // double(10)           Selected scale value
\var_dump($length->getUnit());          // string(2) "mm"       Selected unit
\var_dump((string)$length);             // string(4) "10mm"

Converting examples

Mile to Length:

$mile = Mile::main(1.);
$length = $mile->toSILength();

\var_dump($mile->getMainValue());           // double(1)
\var_dump($length->getMainValue());         // double(1609.344)

Celsius to Fahrenheit:

$celsius    = Celsius::main(20.);
$fahrenheit = Fahrenheit::fromThermodynamicTemperature($celsius->toThermodynamicTemperature());

\var_dump($celsius->getMainValue());        // double(20)
\var_dump($fahrenheit->getMainValue());     // double(68)

Liter to Mass:

$volume = Liter::main(1.)->toVolume();

$massOfWater = $volume->toMass(Density::water());
\var_dump($massOfWater->getMainValue());            // double(0.998)

$massOfMercury = $volume->toMass(Density::mercury());
\var_dump($massOfMercury->getMainValue());          // double(13.57904)

Gallon(UK) to cubic inch and fluid ounce(UK):

$volume = Gallon::main(1.)->toSIVolume();

\var_dump(CubicInch::fromSIVolume($volume)->getMainValue());   // double(277.41943279162)
\var_dump(FluidOunce::fromSIVolume($volume)->getMainValue());  // double(160)

Cubic foot to cubic inch:

$feet = CubicFoot::main(2.)->toRootLength();

\var_dump($feet->getMainValue());                               // double(1.2599210498949)
\var_dump(CubicInch::fromRootLength($feet)->getMainValue());    // double(3456)

Calculation of acceleration:

$velocity = Velocity::fromLengthTime(Length::kilo(100), Time::main(3600.));     // 100km/h
\var_dump($velocity->getMainValue());                                           // double(27.777777777778)

$acceleration = Acceleration::fromVelocityTime($velocity, Time::main(5.));
\var_dump($acceleration->getMainValue());                                       // double(5.5555555555556)

Support

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