承接 ailixter/gears-value 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

ailixter/gears-value

最新稳定版本:0.1.2

Composer 安装命令:

composer require ailixter/gears-value

包简介

The project that gears values

README 文档

README

The project that gears values.

  1. Value class implements [value, error] pattern.
  2. It proposes simple and clear ValueInterface.
  3. It provides protected validation method to override.
  4. And simple rules
    • value may only be constructed, not set
    • invalid value cannot be constructed
    • value with errors cannot be returned
    • you can get both if you're willing to

Basic Usage

    use Ailixter\Gears\IntValue;
    use Ailixter\Gears\ValueInterface;

    class Positive extends IntValue
    {
        protected function validate()
        {
            if ($this->getValue() < 1) {
                $this->addError('value', 'is not positive');
            }
        }
    }

    function calculate(int $int): Positive
    {
        $result = new Positive($int * $int);
        if ($result->getValue() > 81) {
            $result->addError('value', 'too big');
        }
        return $result;
    }

    function readyForUnexpected(IntValue $int)
    {
        if ($int->getErrors()) {
            $result = 81;
        } else {
            $result = $int->getValue();
        }
        echo $result - 1;
    }

    function wantNoUnexpected(int $int)
    {
        echo $int - 1;
    }

    readyForUnexpected(calculate(8));       // 63
    readyForUnexpected(calculate(10));      // 80

    wantNoUnexpected(calculate(8)->get());  // 63

    try {
        wantNoUnexpected(calculate(10)->get()); // exception - too big
    } catch (Ailixter\Gears\Exceptions\ValueException $ve) {
        echo PHP_EOL, $ve->getMessage(), ': ';
        print_r($ve->getValue()->getErrors());
    }
    try {
        calculate(0);                      // exception - not positive
    } catch (Ailixter\Gears\Exceptions\ValueException $ve) {
        echo PHP_EOL, $ve->getMessage(), ': ';
        print_r($ve->getValue()->getErrors());
    }

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-16