diff --git a/docs/advanced-usage/creating-a-cast.md b/docs/advanced-usage/creating-a-cast.md index be6e09122..31f656aa7 100644 --- a/docs/advanced-usage/creating-a-cast.md +++ b/docs/advanced-usage/creating-a-cast.md @@ -10,15 +10,24 @@ A cast implements the following interface: ```php interface Cast { - public function cast(DataProperty $property, mixed $value, array $context): mixed; + public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed; } ``` -The value that should be cast is given, and a `DataProperty` object which represents the property for which the value is cast. You can read more about the internal structures of the package [here](/docs/laravel-data/v4/advanced-usage/internal-structures). +A cast receives the following: -Within the `context` array the complete payload is given. +- **property** a `DataProperty` object which represents the property for which the value is cast. You can read more about the internal structures of the package [here](/docs/laravel-data/v4/advanced-usage/internal-structures) +- **value** the value that should be cast +- **properties** an array of the current properties that will be used to create the data object +- **creationContext** the context in which the data object is being created you'll find the following info here: + - **dataClass** the data class which is being created + - **validationStrategy** the validation strategy which is being used + - **mapPropertyNames** whether property names should be mapped + - **disableMagicalCreation** whether to use the magical creation methods or not + - **ignoredMagicalMethods** the magical methods which are ignored + - **casts** a collection of global casts -In the end, the cast should return a casted value. Please note that the given value of a cast can never be `null`. +In the end, the cast should return a casted value. When the cast is unable to cast the value, an `Uncastable` object should be returned. diff --git a/docs/advanced-usage/creating-a-rule-inferrer.md b/docs/advanced-usage/creating-a-rule-inferrer.md index 10a39b5f4..be6cfa279 100644 --- a/docs/advanced-usage/creating-a-rule-inferrer.md +++ b/docs/advanced-usage/creating-a-rule-inferrer.md @@ -10,12 +10,18 @@ A rule inferrer can be created by implementing the `RuleInferrer` interface: ```php interface RuleInferrer { - public function handle(DataProperty $property, RulesCollection $rules): array; + public function handle(DataProperty $property, PropertyRules $rules, ValidationContext $context): PropertyRules; } ``` A collection of previous inferred rules is given, and a `DataProperty` object which represents the property for which the value is transformed. You can read more about the internal structures of the package [here](/docs/laravel-data/v4/advanced-usage/internal-structures). +The `ValidationContext` is also injected, this contains the following info: + +- **payload** the current payload respective to the data object which is being validated +- **fullPayload** the full payload which is being validated +- **validationPath** the path from the full payload to the current payload + The `RulesCollection` contains all the rules for the property represented as `ValidationRule` objects. You can add new rules to it: diff --git a/docs/advanced-usage/custom-collections.md b/docs/advanced-usage/custom-collections.md deleted file mode 100644 index abb505470..000000000 --- a/docs/advanced-usage/custom-collections.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -title: Custom collections -weight: 12 ---- - -Laravel-data ships with three collections: - -- **DataCollection** (used for collecting arrays with data ) -- **PaginatedDataCollection** (used for collecting paginators with data) -- **CursorPaginatedDataCollection** (used for collecting cursor paginators with data) - -When calling the `collection` method on a data object, one of these collections will be returned depending on the value given to the method. - -It is possible to return a custom collection in such a scenario. First, you must create a new collection class extending from `DataCollection`, `PaginatedDataCollection`, or `CursorPaginatedDataCollection` depending on what kind of collection you want to build. You can add methods and properties, but the constructor should keep the same signature. - -Next, you need to define that your custom collection should be used when collecting data. Which can be done by setting one of the collection properties in your data object: - -```php -class SongData extends Data -{ - protected static string $_collectionClass = MyCustomDataCollection::class; - - public function __construct( - public string $title, - public string $artist, - ) { - } -} -``` - -For a `PaginatedDataCollection`, you need to set the `$_paginatedCollectionClass` property, and for a `CursorPaginatedDataCollection` the `$_cursorPaginatedCollectionClass` property. diff --git a/src/RuleInferrers/RuleInferrer.php b/src/RuleInferrers/RuleInferrer.php index acc9fefda..8ad05016e 100644 --- a/src/RuleInferrers/RuleInferrer.php +++ b/src/RuleInferrers/RuleInferrer.php @@ -8,9 +8,5 @@ interface RuleInferrer { - public function handle( - DataProperty $property, - PropertyRules $rules, - ValidationContext $context, - ): PropertyRules; + public function handle(DataProperty $property, PropertyRules $rules, ValidationContext $context): PropertyRules; }