prewk / record
不可变且可验证的记录
2.1.1
2018-02-28 18:29 UTC
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^6
- satooshi/php-coveralls: ^1.0
README
受 Immutable Record 启发但内存效率较低的 API 的可验证记录
安装
composer require prewk/record
简单用法
- 扩展
\Prewk\Record
- 通过实现
getFields()
定义字段 - 通过实现
getDefaults()
定义(可选)默认值 - 构建基本记录
- 从该基本记录创建新的记录
<?php class FooRecord extends \Prewk\Record { /** * Get record fields * @return string[] */ protected function getFields(): array { return ["foo", "bar", "baz"]; } /** * Get defaults * @return array */ protected function getDefaults(): array { return ["foo" => 123, "bar" => null, "baz" => 456]; } } $fooRecord = new FooRecord; // Create a FooRecord $record1 = $fooRecord->make(["foo" => 777, "bar" => 888]); print_r($record1->asArray()); // -> ["foo" => 777, "bar" => 888, "baz" => 456] // Immutibility $record1->set("foo", "This value will disappear into the void"); print_r($record1->asArray()); // -> ["foo" => 777, "bar" => 888, "baz" => 456] $record2 = $record1->set("foo", "This value will end up in record2"); print_r($record2->asArray()); // -> ["foo" => "Yay", "bar" => 888, "baz" => 456]
验证
- 实现一个扩展
\Prewk\Record\ValidatorInterface
的验证器类 - 通过实现
getRules()
在您的记录上定义规则 - 您的记录上的每一次更改都会通过您的验证器路由
class MyValidator implements \Prewk\Record\ValidatorInterface { /** * Validates a value against a rule * @param mixed $value * @param mixed $rule * @return bool */ public function validate($value, $rule): bool { switch ($rule) { case "numeric": return is_numeric($value); default: throw new \Exception("Invalid rule!"); } } class FooRecord extends \Prewk\Record { /** * Get record fields * @return string[] */ protected function getFields(): array { return ["foo", "bar", "baz"]; } /** * Get defaults * @return array */ protected function getDefaults(): array { return ["foo" => 123, "bar" => null, "baz" => 456]; } /** * Get rules * @return array */ protected function getRules(): array { return ["foo" => "numeric"]; } } $fooRecord = new FooRecord(new MyValidator); $record1 = $fooRecord->make(["foo" => 100]); print_r($record1->asArray()); // -> ["foo" => 777, "bar" => 888, "baz" => 456] $record2 = $fooRecord->make(["foo" => "Will throw exception"]); // -> throws exception "Field name foo didn't validate according to its rules"
可注入的 Laravel 验证记录
<?php class FooRecord extends \Prewk\Record\Laravel\Record { protected function getFields(): array { return ["foo", "bar"]; } protected function getRules(): array { return ["foo" => "in:1,2,3", "bar" => "numeric"]; } } class FooController extends BaseController { private $fooRecord; public function __construct(FooRecord $fooRecord) { $this->fooRecord = $fooRecord; } public function create(FooRequest $request) { $record = $this->fooRecord->make($request->all()); } }
API
// Make a new record from an existing record $record->make(["foo" => "bar"]); // Make a new record from setting $newRecord = $record->set("foo", "bar"); // Check if a field has a value (if field has a default value this returns true) $fooIsSet = $record->has("foo"); // Merge with an array $newRecord = $record->merge(["baz" => "qux"]); // ..or with an existing record $newRecord = $record->merge($record2);
许可证
MIT
PHP 5
对于 <7.0 的 PHP 版本使用 1.1