burobo/ukon

Unit conversion library.

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:project

v1.0.0 2019-08-18 15:58 UTC

This package is auto-updated.

Last update: 2025-07-19 05:40:34 UTC


README

Ukon is a unit conversion calculator library. You can make your own unit conversion settings easily with it.

Installation

composer require burobo/ukon

Usage

  1. Create your own Unit class.

    use Ukon\Unit;
    
    class Metre extends Unit
    {
        /**
         * @inheritDoc
         */
        protected function languageSpecificFormats(): array
        {
            return [
                'default' => '%s metre',
            ];
        }
    
        /**
         * @inheritDoc
         */
        protected function globalFormats(): array
        {
            return [
                'abbr' => '%sm',
            ];
        }
    
        /**
         * @inheritDoc
         */
        protected function domain(): string
        {
            return 'messages';
        }
    }
  2. Create your own Type class.

    use Ukon\Type;
    
    class Length extends Type
    {
        /**
         * @inheritDoc�
         */
        public function __construct(int $scale)
        {
            parent::__construct($scale);
            $this->registerUnitRatio(Metre::class, 1000);
            $this->registerUnitRatio(Centimetre::class, 10);
            $this->registerUnitRatio(Millimetre::class, 1);
        }
    }
  3. Now you can convert and calculate your own unit!

    $height = (new Length(1))
        ->addMetre(1.7)
        ->addMillimetre(1);
    
    $height->stringify(function (Metre $metre, Centimetre $centimetre, Millimetre $millimetre) {
        return $metre->fmtAbbr() . ' ' . $centimetre->fmtAbbr() . ' ' . $millimetre->fmtAbbr();
    }); // 1m 70cm 1mm