Validatable records with an API inspired by Immutable Record (but without the memory efficiency..)
composer require prewk/record
- Extend
\Prewk\Record - Define fields by implementing
getFields() - Define (optional) defaults by implementing
getDefaults() - Construct a base record
- Create new records from that base record
<?phpclass FooRecord extends \Prewk\Record
{
/** * Get record fields * @return string[] */protectedfunctiongetFields(): array
{
return ["foo", "bar", "baz"];
}
/** * Get defaults * @return array */protectedfunctiongetDefaults(): array
{
return ["foo" => 123, "bar" => null, "baz" => 456];
}
}
$fooRecord = newFooRecord;
// 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]- Implement a validator class that extends
\Prewk\Record\ValidatorInterface - Define rules on your record by implementing
getRules() - Every mutation on your record will be routed through your validator
class MyValidator implements \Prewk\Record\ValidatorInterface
{
/** * Validates a value against a rule * @param mixed $value * @param mixed $rule * @return bool */publicfunctionvalidate($value, $rule): bool
{
switch ($rule) {
case"numeric":
returnis_numeric($value);
default:
thrownew \Exception("Invalid rule!");
}
}
class FooRecord extends \Prewk\Record
{
/** * Get record fields * @return string[] */protectedfunctiongetFields(): array
{
return ["foo", "bar", "baz"];
}
/** * Get defaults * @return array */protectedfunctiongetDefaults(): array
{
return ["foo" => 123, "bar" => null, "baz" => 456];
}
/** * Get rules * @return array */protectedfunctiongetRules(): array
{
return ["foo" => "numeric"];
}
}
$fooRecord = newFooRecord(newMyValidator);
$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"<?phpclass FooRecord extends \Prewk\Record\Laravel\Record
{
protectedfunctiongetFields(): array
{
return ["foo", "bar"];
}
protectedfunctiongetRules(): array
{
return ["foo" => "in:1,2,3", "bar" => "numeric"];
}
}
class FooController extends BaseController
{
private$fooRecord;
publicfunction__construct(FooRecord$fooRecord)
{
$this->fooRecord = $fooRecord;
}
publicfunctioncreate(FooRequest$request)
{
$record = $this->fooRecord->make($request->all());
}
}// 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
Use 1.1 for PHP versions < 7.0.