Download the PHP package knppy/units without Composer
On this page you can find all versions of the php package knppy/units. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package units
Short Description PHP library to make working with units safer, easier, and fun!
License MIT
Homepage https://github.com/knppy/units
Informations about the package units
Units
Knppy units is a PHP library to make working with units safer, easier, and fun!
Installation
You can install the package via Composer:
Usage
Lists implement Knppy\Units\Contract\UnitOfMeasurements, so other lists can be added alongside the International
System of Units. CookingUnitOfMeasurements covers cooking: volumes (tsp, tbsp, cup, ...), masses (oz, lb),
counts (clove, slice, ...) and imprecise amounts (pinch, dash, handful, ...).
Conversion
Each system of units has its own converter implementing Knppy\Units\Contract\UnitConverter. convertTo() uses the
SIUnitConverter by default; pass another converter as the second argument.
All 24 SI prefixes (quecto to quetta) are supported through the Prefix enum and the SIUnitConverter:
Amounts are whole numbers, so convert() throws when the result would have decimals. Use convertToDecimal() for an
exact decimal string instead. Masses can be prefixed grams (mg) even though the SI base unit is the kilogram.
Cooking
The CookingUnitConverter converts volumes and masses using exact US customary ratios:
Units without a fixed size, such as a pinch or a clove, cannot be converted to another unit and throw an
IncompatibleUnitOfMeasurementException.
Languages
Unit names can be localized and localized input can be parsed back to a unit. The symbol stays the canonical identifier, so conversion and equals are unaffected.
The count picks the plural form by the CLDR rules of the locale (via ext-intl), so languages with more than two forms work. Pass a unit's amount directly:
Symbols are matched first (case-sensitive), then names, plurals and aliases (case-insensitive). Unsupported locales and missing units fall back to English. SIUnitNames does the same for SI units.
Available locales: en, nl, de, fr, es, pt, zh, ja, ru, hi, ar. Quantity labels are not localized yet. Requires ext-intl and ext-mbstring.
Prefixes
SIUnitNames also names and parses prefixed units, composing them from the prefix and the unit name:
Parsing ignores case and common Latin accents (kilometro matches kilómetro). Units that cannot take a prefix (kg, °C) are rejected, as in SIUnitConverter.
Spelling that cannot be composed is data, not code. A locale can list prefixed names in si-prefixed.php (Spanish and Portuguese do this for the stress shift in kilómetro), or give a unit a prefixed entry with its own forms and a separator (Arabic does this for ميلي ثانية). Region tags work too: pt is Brazilian (quilômetro) and pt-PT is European (quilómetro).
Parsing
Parsers turn a string into a Unit and implement Knppy\Units\Contract\UnitParser. They throw a ParserException
when the input is not a whole amount with a unit of measurement.
SymbolUnitParser accepts any symbol, so put it last in an AggregateUnitParser.
Formatting
Formatters turn a Unit into a string and implement Knppy\Units\Contract\UnitFormatter, the counterpart of the parsers.
LocalizedUnitFormatter picks the plural form from the amount and groups digits as the locale does (pass grouping: false to
get output LocalizedUnitParser can read back). Amounts beyond the integer range keep every digit.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Thank you for considering contributing to Units! Please review our contributing guide to get started.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Michael Beers
- All Contributors
License
Units is open-sourced software licensed under the MIT license.
All versions of units with dependencies
ext-bcmath Version *
ext-intl Version *
ext-mbstring Version *