burobo/ukon

单位转换库。

安装: 4

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

公开问题: 0

类型:项目

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

This package is auto-updated.

Last update: 2024-09-19 04:04:38 UTC


README

Ukon是一个单位转换计算器库。您可以使用它轻松创建自己的单位转换设置。

安装

composer require burobo/ukon

使用方法

  1. 创建您自己的单位类。

    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. 创建您自己的类型类。

    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. 现在您可以转换和计算您自己的单位了!

    $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