PHP code example of knppy / units

1. Go to this page and download the library: Download knppy/units library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

knppy / units example snippets


use Knppy\Units\Unit;
use Knppy\Units\UnitOfMeasurement;
use Knppy\Units\UnitOfMeasurements\SIUnitOfMeasurements;

$kilogram = new UnitOfMeasurement('kg');
$weight = new Unit(100, $kilogram);

$si = new SIUnitOfMeasurements();
$si->contains($kilogram);    // true
$si->nameFor($kilogram);     // "kilogram"
$si->quantityFor($kilogram); // "mass"

use Knppy\Units\UnitOfMeasurements\CookingUnitOfMeasurements;

$cooking = new CookingUnitOfMeasurements();
$cooking->nameFor(new UnitOfMeasurement('pinch'));    // "pinch"
$cooking->quantityFor(new UnitOfMeasurement('tsp'));  // "volume"

use Knppy\Units\Converter\SIUnitConverter;

$km = new Unit(5, new UnitOfMeasurement('km'));

$km->convertTo(new UnitOfMeasurement('m'));   // 5000 m
$km->convertTo(new UnitOfMeasurement('mm'));  // 5000000 mm

$m = new Unit(1, new UnitOfMeasurement('m'));
$m->convertTo(new UnitOfMeasurement('km'));   // throws InexactConversionException, 0.001 is not a whole number
(new SIUnitConverter)->convertToDecimal($m, new UnitOfMeasurement('km')); // "0.001"

use Knppy\Units\Converter\CookingUnitConverter;

$cooking = new CookingUnitConverter();

(new Unit(1, new UnitOfMeasurement('tbsp')))->convertTo(new UnitOfMeasurement('tsp'), $cooking); // 3 tsp
(new Unit(1, new UnitOfMeasurement('lb')))->convertTo(new UnitOfMeasurement('oz'), $cooking);    // 16 oz
$cooking->convertToDecimal(new Unit(1, new UnitOfMeasurement('oz')), new UnitOfMeasurement('g')); // "28.349523125"

use Knppy\Units\UnitNames\CookingUnitNames;
use Knppy\Units\UnitOfMeasurement;

$names = new CookingUnitNames;

$names->nameFor(new UnitOfMeasurement('tsp'), 'nl');              // theelepel
$names->nameFor(new UnitOfMeasurement('tsp'), 'nl', 2);            // theelepels
$names->parse('Theelepels', 'nl');                                // UnitOfMeasurement('tsp')
$names->parse('tl', 'nl');                                        // UnitOfMeasurement('tsp')

$names->nameFor(new UnitOfMeasurement('tsp'), 'ru', 1);   // чайная ложка
$names->nameFor(new UnitOfMeasurement('tsp'), 'ru', 22);  // чайные ложки
$names->nameFor(new UnitOfMeasurement('tsp'), 'ru', 5);   // чайных ложек
$names->nameFor($unit->getUnitOfMeasurement(), 'ar', $unit->getAmount());

use Knppy\Units\Prefix;
use Knppy\Units\UnitNames\PrefixNames;
use Knppy\Units\UnitNames\SIUnitNames;

$si = new SIUnitNames;

$si->nameFor(new UnitOfMeasurement('cm'), 'de');  // Zentimeter
$si->nameFor(new UnitOfMeasurement('km'), 'es');  // kilómetro
$si->parse('milligramm', 'de');                   // UnitOfMeasurement('mg')
$si->parse('千米', 'zh');                          // UnitOfMeasurement('km')

(new PrefixNames)->nameFor(Prefix::Centi, 'de');  // zenti
(new PrefixNames)->parse('Zenti', 'de');          // Prefix::Centi

use Knppy\Units\Parser\AggregateUnitParser;
use Knppy\Units\Parser\LocalizedUnitParser;
use Knppy\Units\Parser\SymbolUnitParser;
use Knppy\Units\UnitNames\CookingUnitNames;

(new SymbolUnitParser)->parse('5 kg');                                      // Unit(5, kg)
(new SymbolUnitParser)->parse('5', new UnitOfMeasurement('m'));             // Unit(5, m), the fallback fills a missing unit
(new LocalizedUnitParser(new CookingUnitNames, 'nl'))->parse('2 theelepels'); // Unit(2, tsp)

$parser = new AggregateUnitParser([
    new LocalizedUnitParser(new CookingUnitNames, 'nl'),
    new SymbolUnitParser,
]);
$parser->parse('2 theelepels'); // the first parser that succeeds wins

use Knppy\Units\Formatter\AggregateUnitFormatter;
use Knppy\Units\Formatter\DecimalUnitFormatter;
use Knppy\Units\Formatter\LocalizedUnitFormatter;
use Knppy\Units\Formatter\SymbolUnitFormatter;

$unit = new Unit(1500, new UnitOfMeasurement('tsp'));

(new DecimalUnitFormatter)->format($unit);                                   // "1500"
(new SymbolUnitFormatter)->format($unit);                                    // "1500 tsp"
(new LocalizedUnitFormatter(new CookingUnitNames, 'nl'))->format($unit);     // "1.500 theelepels"
(new LocalizedUnitFormatter(new CookingUnitNames, 'nl', useName: false))->format($unit); // "1.500 tsp"

$formatter = new AggregateUnitFormatter(
    ['tsp' => new LocalizedUnitFormatter(new CookingUnitNames, 'nl')],
    new SymbolUnitFormatter, // default for other symbols, otherwise a FormatterException is thrown
);