Skip to content

Commit fcc0d8f

Browse files
Fix differences between contexts
1 parent 2761d73 commit fcc0d8f

26 files changed

+298
-109
lines changed

docs/advanced-usage/creating-a-cast.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Creating a cast
3-
weight: 8
3+
weight: 6
44
---
55

66
Casts take simple values and cast them into complex types. For example, `16-05-1994T00:00:00+00` could be cast into a `Carbon` object with the same date.

docs/advanced-usage/creating-a-rule-inferrer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Creating a rule inferrer
3-
weight: 10
3+
weight: 8
44
---
55

66
Rule inferrers will try to infer validation rules for properties within a data object.
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Creating a transformer
3-
weight: 9
3+
weight: 7
44
---
55

66
Transformers take complex values and transform them into simple types. For example, a `Carbon` object could be transformed to `16-05-1994T00:00:00+00`.
@@ -10,10 +10,18 @@ A transformer implements the following interface:
1010
```php
1111
interface Transformer
1212
{
13-
public function transform(DataProperty $property, mixed $value): mixed;
13+
public function transform(DataProperty $property, mixed $value, TransformationContext $context): mixed;
1414
}
1515
```
1616

17-
The value that should be transformed 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).
17+
The following parameters are provided:
1818

19-
In the end, the transformer should return a transformed value. Please note that the given value of a transformer can never be `null`.
19+
- **property**: 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)
20+
- **value**: the value that should be transformed, this will never be `null`
21+
- **context**: a `TransformationContext` object which contains the current transformation context with the following properties:
22+
- **transformValues** indicates if values should be transformed or not
23+
- **mapPropertyNames** indicates if property names should be mapped or not
24+
- **wrapExecutionType** the execution type that should be used for wrapping values
25+
- **transformers** a collection of transformers that can be used to transform values
26+
27+
In the end, the transformer should return a transformed value.

docs/advanced-usage/normalizers.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ By default, there are five normalizers for each data object:
2020
- **ArrayNormalizer** will cast arrays
2121
- **JsonNormalizer** will cast json strings
2222

23+
A sixth normalizer can be optionally enabled:
24+
25+
- **FormRequestNormalizer** will normalize a form request by calling the `validated` method
26+
2327
Normalizers can be globally configured in `config/data.php`, and can be configured on a specific data object by overriding the `normalizers` method.
2428

2529
```php

docs/advanced-usage/pipeline.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ By default, the pipeline exists of the following pipes:
1414

1515
- **AuthorizedDataPipe** checks if the user is authorized to perform the request
1616
- **MapPropertiesDataPipe** maps the names of properties
17+
- **FillRouteParameterPropertiesDataPipe** fills property values from route parameters
1718
- **ValidatePropertiesDataPipe** validates the properties
1819
- **DefaultValuesDataPipe** adds default values for properties when they are not set
1920
- **CastPropertiesDataPipe** casts the values of properties
@@ -35,19 +36,20 @@ class SongData extends Data
3536
->into(static::class)
3637
->through(AuthorizedDataPipe::class)
3738
->through(MapPropertiesDataPipe::class)
39+
->through(FillRouteParameterPropertiesDataPipe::class)
3840
->through(ValidatePropertiesDataPipe::class)
3941
->through(DefaultValuesDataPipe::class)
4042
->through(CastPropertiesDataPipe::class);
4143
}
4244
}
4345
```
4446

45-
Each pipe implements the `DataPipe` interface and should return a `Collection` of properties:
47+
Each pipe implements the `DataPipe` interface and should return an `array` of properties:
4648

4749
```php
4850
interface DataPipe
4951
{
50-
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection;
52+
public function handle(mixed $payload, DataClass $class, array $properties, CreationContext $creationContext): array;
5153
}
5254
```
5355

@@ -57,6 +59,13 @@ The `handle` method has several arguments:
5759
- **class** the `DataClass` object for the data
5860
object [more info](/docs/laravel-data/v4/advanced-usage/internal-structures)
5961
- **properties** the key-value properties which will be used to construct the data object
62+
- **creationContext** the context in which the data object is being created you'll find the following info here:
63+
- **dataClass** the data class which is being created
64+
- **validationStrategy** the validation strategy which is being used
65+
- **mapPropertyNames** whether property names should be mapped
66+
- **withoutMagicalCreation** whether to use the magical creation methods or not
67+
- **ignoredMagicalMethods** the magical methods which are ignored
68+
- **casts** a collection of global casts
6069

6170
When using a magic creation methods, the pipeline is not being used (since you manually overwrite how a data object is
6271
constructed). Only when you pass in a request object a minimal version of the pipeline is used to authorize and validate

docs/advanced-usage/typescript.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,3 @@ class DataObject extends Data
128128
}
129129
}
130130
```
131-
132-
You can also make all the properties of a data object optional in TypeScript like this:
133-
134-
```php
135-
class DataObject extends Data
136-
{
137-
#[TypeScriptOptional]
138-
public function __construct(
139-
public int $id,
140-
public string $someString,
141-
public Optional|string $optional,
142-
)
143-
{
144-
}
145-
}
146-
```

docs/advanced-usage/use-with-inertia.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Use with Inertia
3-
weight: 6
3+
weight: 9
44
---
55

66
> Inertia.js lets you quickly build modern single-page React, Vue, and Svelte apps using classic server-side routing and controllers.

docs/advanced-usage/use-with-livewire.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Use with Livewire
3-
weight: 7
3+
weight: 10
44
---
55

66
> Livewire is a full-stack framework for Laravel that makes building dynamic interfaces simple without leaving the comfort of Laravel.

docs/advanced-usage/validation-attributes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Validation attributes
3-
weight: 16
3+
weight: 14
44
---
55

66
These are all the validation attributes currently available in laravel-data.

docs/advanced-usage/working-with-dates.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ within the `data.php` config file:
5454

5555
Now when casting a date, a valid format will be searched. When none can be found, an exception is thrown.
5656

57-
When a transformers hasn't explicitly stated it's format, the first format within the array is used.
57+
When a transformers hasn't explicitly stated its format, the first format within the array is used.
5858

5959
## Casting dates in a different time zone
6060

docs/as-a-resource/transformers.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ It is possible to disable the transformation of values, which will make the `tra
101101
use Spatie\LaravelData\Support\Transformation\TransformationContext;
102102

103103
ArtistData::from($artist)->transform(
104-
TransformationContext::create()->withoutTransformingValues()
104+
TransformationContextFactory::create()->transformValues(false)
105105
);
106106
```
107107

@@ -118,7 +118,7 @@ The [mapping of property names](/docs/laravel-data/v4/as-a-resource/mapping-prop
118118

119119
```php
120120
ArtistData::from($artist)->transform(
121-
TransformationContext::create()->mapPropertyNames(false)
121+
TransformationContextFactory::create()->mapPropertyNames(false)
122122
);
123123
```
124124

@@ -128,7 +128,7 @@ It is possible to enable [wrapping](/docs/laravel-data/v4/as-a-resource/wrapping
128128
use Spatie\LaravelData\Support\Wrapping\WrapExecutionType;
129129

130130
ArtistData::from($artist)->transform(
131-
TransformationContext::create()->wrapExecutionType(WrapExecutionType::Enabled)
131+
TransformationContextFactory::create()->wrapExecutionType(WrapExecutionType::Enabled)
132132
);
133133
```
134134

src/Concerns/BaseDataCollectable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getDataClass(): string
2424
public function getIterator(): ArrayIterator
2525
{
2626
/** @var array<TValue> $data */
27-
$data = $this->transform(TransformationContextFactory::create()->transformValues(false));
27+
$data = $this->transform(TransformationContextFactory::create()->withValueTransformation(false));
2828

2929
return new ArrayIterator($data);
3030
}

src/Concerns/ResponsableData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait ResponsableData
1515
public function toResponse($request)
1616
{
1717
$contextFactory = TransformationContextFactory::create()
18-
->wrapExecutionType(WrapExecutionType::Enabled);
18+
->withWrapExecutionType(WrapExecutionType::Enabled);
1919

2020
$includePartials = DataContainer::get()->requestQueryStringPartialsResolver()->execute(
2121
$this,

src/Concerns/TransformableData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function transform(
3737

3838
public function all(): array
3939
{
40-
return $this->transform(TransformationContextFactory::create()->transformValues(false));
40+
return $this->transform(TransformationContextFactory::create()->withValueTransformation(false));
4141
}
4242

4343
public function toArray(): array

src/DataPipes/DataPipe.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,9 @@
88
interface DataPipe
99
{
1010
/**
11-
* @param mixed $payload
12-
* @param DataClass $class
1311
* @param array<array-key, mixed> $properties
14-
* @param CreationContext $creationContext
1512
*
1613
* @return array<array-key, mixed>
1714
*/
18-
public function handle(
19-
mixed $payload,
20-
DataClass $class,
21-
array $properties,
22-
CreationContext $creationContext
23-
): array;
15+
public function handle(mixed $payload, DataClass $class, array $properties, CreationContext $creationContext): array;
2416
}

src/Support/Creation/CreationContext.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Spatie\LaravelData\CursorPaginatedDataCollection;
1717
use Spatie\LaravelData\DataCollection;
1818
use Spatie\LaravelData\PaginatedDataCollection;
19-
use Spatie\LaravelData\Support\Casting\GlobalCastsCollection;
2019
use Spatie\LaravelData\Support\DataContainer;
2120

2221
/**

src/Support/Creation/CreationContextFactory.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Spatie\LaravelData\CursorPaginatedDataCollection;
1818
use Spatie\LaravelData\DataCollection;
1919
use Spatie\LaravelData\PaginatedDataCollection;
20-
use Spatie\LaravelData\Support\Casting\GlobalCastsCollection;
2120
use Spatie\LaravelData\Support\DataContainer;
2221

2322
/**
@@ -32,7 +31,7 @@ public function __construct(
3231
public string $dataClass,
3332
public ValidationStrategy $validationStrategy,
3433
public bool $mapPropertyNames,
35-
public bool $withoutMagicalCreation,
34+
public bool $disableMagicalCreation,
3635
public ?array $ignoredMagicalMethods,
3736
public ?GlobalCastsCollection $casts,
3837
) {
@@ -48,25 +47,12 @@ public static function createFromConfig(
4847
dataClass: $dataClass,
4948
validationStrategy: ValidationStrategy::from($config['validation_strategy']),
5049
mapPropertyNames: true,
51-
withoutMagicalCreation: false,
50+
disableMagicalCreation: false,
5251
ignoredMagicalMethods: null,
5352
casts: null,
5453
);
5554
}
5655

57-
public static function createFromContext(
58-
CreationContext $context
59-
) {
60-
return new self(
61-
dataClass: $context->dataClass,
62-
validationStrategy: $context->validationStrategy,
63-
mapPropertyNames: $context->mapPropertyNames,
64-
withoutMagicalCreation: $context->withoutMagicalCreation,
65-
ignoredMagicalMethods: $context->ignoredMagicalMethods,
66-
casts: $context->casts,
67-
);
68-
}
69-
7056
public function validationStrategy(ValidationStrategy $validationStrategy): self
7157
{
7258
$this->validationStrategy = $validationStrategy;
@@ -95,6 +81,13 @@ public function alwaysValidate(): self
9581
return $this;
9682
}
9783

84+
public function withPropertyNameMapping(bool $withPropertyNameMapping = true): self
85+
{
86+
$this->mapPropertyNames = $withPropertyNameMapping;
87+
88+
return $this;
89+
}
90+
9891
public function withoutPropertyNameMapping(bool $withoutPropertyNameMapping = true): self
9992
{
10093
$this->mapPropertyNames = ! $withoutPropertyNameMapping;
@@ -104,7 +97,14 @@ public function withoutPropertyNameMapping(bool $withoutPropertyNameMapping = tr
10497

10598
public function withoutMagicalCreation(bool $withoutMagicalCreation = true): self
10699
{
107-
$this->withoutMagicalCreation = $withoutMagicalCreation;
100+
$this->disableMagicalCreation = $withoutMagicalCreation;
101+
102+
return $this;
103+
}
104+
105+
public function withMagicalCreation(bool $withMagicalCreation = true): self
106+
{
107+
$this->disableMagicalCreation = ! $withMagicalCreation;
108108

109109
return $this;
110110
}
@@ -157,7 +157,7 @@ public function get(): CreationContext
157157
dataClass: $this->dataClass,
158158
validationStrategy: $this->validationStrategy,
159159
mapPropertyNames: $this->mapPropertyNames,
160-
withoutMagicalCreation: $this->withoutMagicalCreation,
160+
withoutMagicalCreation: $this->disableMagicalCreation,
161161
ignoredMagicalMethods: $this->ignoredMagicalMethods,
162162
casts: $this->casts,
163163
);

src/Support/Casting/GlobalCastsCollection.php renamed to src/Support/Creation/GlobalCastsCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Spatie\LaravelData\Support\Casting;
3+
namespace Spatie\LaravelData\Support\Creation;
44

55
use ArrayIterator;
66
use IteratorAggregate;

src/Support/DataConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use ReflectionClass;
66
use Spatie\LaravelData\Contracts\BaseData;
77
use Spatie\LaravelData\RuleInferrers\RuleInferrer;
8-
use Spatie\LaravelData\Support\Casting\GlobalCastsCollection;
8+
use Spatie\LaravelData\Support\Creation\GlobalCastsCollection;
99
use Spatie\LaravelData\Support\Transformation\GlobalTransformersCollection;
1010

1111
class DataConfig

src/Support/Transformation/GlobalTransformersCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function add(string $transformable, Transformer $transformer): self
2727
public function findTransformerForValue(mixed $value): ?Transformer
2828
{
2929
if (gettype($value) !== 'object') {
30-
return null;
30+
return $this->transformers[get_debug_type($value)] ?? null;
3131
}
3232

3333
foreach ($this->transformers as $transformable => $transformer) {

0 commit comments

Comments
 (0)