### PHP: Laravel Injectable Record Example Source: https://github.com/prewk/record/blob/master/README.md Illustrates how to integrate Prewk Record with Laravel by extending the Laravel\Record class and using dependency injection in a controller. Shows defining fields and rules within the record and making a record from request data. ```php "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()); } } ``` -------------------------------- ### PHP: Record API Methods Source: https://github.com/prewk/record/blob/master/README.md Provides examples of common API methods for interacting with Prewk Record objects. Demonstrates making new records, setting field values immutably, checking if a field is set, and merging records with arrays or other records. ```php // 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); ``` -------------------------------- ### PHP: Define and Use a Simple Record Source: https://github.com/prewk/record/blob/master/README.md Demonstrates how to extend the Prewk\Record class, define fields and defaults, and create new record instances. Shows basic usage like making a record and accessing its data. ```php 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] ``` -------------------------------- ### PHP: Implement Validation for a Record Source: https://github.com/prewk/record/blob/master/README.md Shows how to implement record validation by creating a custom validator class that adheres to ValidatorInterface and defining validation rules within the record. Demonstrates creating a record with a validator and the expected behavior on valid and invalid data. ```php 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" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.