ailixter/gears-value
最新稳定版本:0.1.2
Composer 安装命令:
composer require ailixter/gears-value
包简介
The project that gears values
README 文档
README
The project that gears values.
- Value class implements [value, error] pattern.
- It proposes simple and clear ValueInterface.
- It provides protected validation method to override.
- 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
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-16