Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jan 23, 2024
1 parent 83f3498 commit 1ef1f3c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 41 deletions.
17 changes: 13 additions & 4 deletions docs/advanced-usage/creating-a-cast.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 7 additions & 1 deletion docs/advanced-usage/creating-a-rule-inferrer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 0 additions & 31 deletions docs/advanced-usage/custom-collections.md

This file was deleted.

6 changes: 1 addition & 5 deletions src/RuleInferrers/RuleInferrer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 1ef1f3c

Please sign in to comment.