githubjeka/value-object 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

githubjeka/value-object

Composer 安装命令:

composer require githubjeka/value-object

包简介

Help to create immutable Value Objects

README 文档

README

Help to create immutable Value Objects

Build Status

Default API

Two methods compareTo and changeTo:

// Value Object is a Metre(['1', 'centimeter'])
$metre = new Metre(['1', 'cm']);
$metre->compareTo([2,'cm']); // returns -1
$metre->compareTo([.1,'m']); // returns 0
$metre->compareTo([1,'mm']); // returns 1
$metre->changeTo([1,'m']) //returns new  Metre(['1', 'm']), $metre is  Metre(['1', 'centimeter'])

//user API via changeTo
$metre->toMillimeter(); // new Metre(['10', 'mm']), $metre is  Metre(['1', 'centimeter'])
$metre->toMillimeter()->add([1,'mm'])->getAmount();  //returns 11

// rewrite __toString()
echo $metre // returns '1'

a little more

final class Money
{
    use ImmutableValueObject;
    
    private $amount;
    private $currency;
        
    protected function compare($valueObject)
    {
        return bccomp($this->toUsd()->amount, $valueObject->toUsd()->amount);
    }
    
    protected function setAttributes($value)
    {
        // ... validate value
        
        $this->currency = $value[1];
        $this->amount = $value[0];
    }
    
    private function getCurrencies()
    {
        return [
            'usd' => 1,
            'rub' => 60,
            //...
        ];
    }
    
    public function toUsd()
    {
        if ($this->currency === 'usd') {
            return $this->changeTo([$this->amount, 'usd']);
        }        
        $k = $this->getCurrencies()[$this->currency];
        return $this->changeTo([$this->amount * $k, 'usd']);
    }
    
    public function toRub()
    {            
        $amount = $this->toUsd()->amount;
        $k = $this->getCurrencies()['rub'];
        return $this->changeTo([$amount / $k, 'rub']);
    }
}

统计信息

  • 总下载量: 30
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-01-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固