Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data v4 #410

Merged
merged 137 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
137 commits
Select commit Hold shift + click to select a range
e1e491d
Allow accessors to be used when creating data from models
rubenvanassche Feb 14, 2023
3b5f29f
Refactor partial trees
rubenvanassche Feb 16, 2023
c2ef223
wip
rubenvanassche Feb 16, 2023
e6d7ab7
wip
rubenvanassche Feb 21, 2023
30b03b6
wip
rubenvanassche Feb 21, 2023
51ac1d4
wip
rubenvanassche Feb 22, 2023
7f3c82e
wip
rubenvanassche Feb 22, 2023
44000c6
wip
rubenvanassche Feb 22, 2023
c873e89
wip
rubenvanassche Feb 22, 2023
68d954e
wip
rubenvanassche Feb 22, 2023
fb9125a
Fix styling
rubenvanassche Feb 22, 2023
a87d406
wip
rubenvanassche Mar 2, 2023
4043510
Merge branch 'main' into beyond-data
rubenvanassche Apr 7, 2023
7de8cb2
Add returns method
rubenvanassche Apr 7, 2023
4b82481
Fix styling
rubenvanassche Apr 7, 2023
c494c1a
Fix benchmarks
rubenvanassche Apr 7, 2023
f613694
Merge main
rubenvanassche Apr 7, 2023
71a31c2
Fix styling
rubenvanassche Apr 7, 2023
8cc637c
Merge branch 'main' into beyond-data
rubenvanassche May 5, 2023
6562acf
Merge
rubenvanassche May 5, 2023
f2f8df7
Fix styling
rubenvanassche May 5, 2023
5b49805
Merge branch 'main' into beyond-data
rubenvanassche May 12, 2023
76bc861
Merge branch 'main' into beyond-data
rubenvanassche Jun 2, 2023
6a85dbb
Add support for constructor parameter types
rubenvanassche Jun 21, 2023
4d1176f
Fix styling
rubenvanassche Jun 21, 2023
83c46bf
Merge branch 'main' into beyond-data
rubenvanassche Jul 6, 2023
3e887dd
Use ts transformer 3
rubenvanassche Aug 1, 2023
4eea39e
Remove old ts transformer stuff
rubenvanassche Aug 1, 2023
53de7d0
Fix styling
rubenvanassche Aug 1, 2023
5f56653
wip
rubenvanassche Aug 4, 2023
0ea20b3
Fix collection partials
rubenvanassche Aug 4, 2023
4988f2a
Merge branch 'main' into beyond-data
rubenvanassche Aug 4, 2023
79c57dd
Remove test
rubenvanassche Aug 4, 2023
8dbd8a7
Fix styling
rubenvanassche Aug 4, 2023
8100f56
wip
rubenvanassche Sep 15, 2023
ad1ac82
Fix styling
rubenvanassche Sep 15, 2023
4bccae7
Move tests
rubenvanassche Sep 21, 2023
1cab4a5
wip
rubenvanassche Sep 21, 2023
603f8b1
Fix styling
rubenvanassche Sep 21, 2023
d13e532
wip
rubenvanassche Sep 22, 2023
c53bf7c
Fix styling
rubenvanassche Sep 22, 2023
d56e076
Merge branch 'main' into beyond-data
rubenvanassche Oct 13, 2023
c02fbfa
Small fixes after merge
rubenvanassche Oct 13, 2023
b57f99d
Merge branch 'main' into beyond-data
rubenvanassche Dec 1, 2023
9a310d9
Merge updates
rubenvanassche Dec 1, 2023
75f5c6f
Benchmarking
rubenvanassche Dec 4, 2023
1462196
Fix styling
rubenvanassche Dec 4, 2023
1b920f3
Performance updates
rubenvanassche Dec 7, 2023
beaab0d
Merge branch 'main' into beyond-data
rubenvanassche Dec 21, 2023
a0a163b
Fixes for merge
rubenvanassche Dec 21, 2023
c7a4be1
Rollback to typescript transformer 2
rubenvanassche Dec 21, 2023
2178ea9
Fix styling
rubenvanassche Dec 21, 2023
8ccfbb4
Add better formatting
rubenvanassche Dec 21, 2023
07cf351
Replace partial trees system
rubenvanassche Jan 5, 2024
ed8d9e4
Fix styling
rubenvanassche Jan 5, 2024
fdeddba
Fix partials
rubenvanassche Jan 5, 2024
aa48d7d
Fix styling
rubenvanassche Jan 5, 2024
49a4c6e
Move to custom collections
rubenvanassche Jan 8, 2024
b3c899f
Wrapping fixes
rubenvanassche Jan 8, 2024
3553d88
Fix styling
rubenvanassche Jan 8, 2024
310d711
Latest performance fixes
rubenvanassche Jan 8, 2024
e1d19de
Latest performance fixes
rubenvanassche Jan 8, 2024
c84dd51
Add support for transforms in context
rubenvanassche Jan 8, 2024
4d1b413
Data config changes
rubenvanassche Jan 9, 2024
2317333
Fix styling
rubenvanassche Jan 9, 2024
66b845f
Data config changes
rubenvanassche Jan 9, 2024
425d7c0
Small fixes
rubenvanassche Jan 9, 2024
ad355a3
Fix styling
rubenvanassche Jan 9, 2024
830ec87
Fix tests
rubenvanassche Jan 9, 2024
e599870
Fix styling
rubenvanassche Jan 9, 2024
180a8e8
Fix tests
rubenvanassche Jan 9, 2024
09756f1
Update composer
rubenvanassche Jan 9, 2024
7acf70b
Update composer
rubenvanassche Jan 9, 2024
a72ec14
Move interfaces
rubenvanassche Jan 10, 2024
d150fe2
A better way of creating data
rubenvanassche Jan 10, 2024
6a1251d
Baseline updated
rubenvanassche Jan 11, 2024
0e9b0a5
Fix styling
rubenvanassche Jan 11, 2024
76c7522
Creation context in casts
rubenvanassche Jan 11, 2024
82f1983
Fix styling
rubenvanassche Jan 11, 2024
7cc6e0e
wip
rubenvanassche Jan 15, 2024
443439d
Fix styling
rubenvanassche Jan 15, 2024
0fc8124
Test cleanup
rubenvanassche Jan 15, 2024
3d00623
Fix styling
rubenvanassche Jan 15, 2024
06fcfe6
Partial tests
rubenvanassche Jan 16, 2024
1c1bde5
wip
rubenvanassche Jan 16, 2024
6d8b6ad
Fix styling
rubenvanassche Jan 16, 2024
3597df0
Better testing
rubenvanassche Jan 16, 2024
49a184f
Fix styling
rubenvanassche Jan 16, 2024
0346ce4
wip
rubenvanassche Jan 17, 2024
9e2ea32
Fix styling
rubenvanassche Jan 17, 2024
a32f364
Fix CI
rubenvanassche Jan 17, 2024
05a1098
Replace type system
rubenvanassche Jan 19, 2024
c5fd058
Fix styling
rubenvanassche Jan 19, 2024
2d430c3
Better benchmarks
rubenvanassche Jan 19, 2024
994836d
Asert benchmarks
rubenvanassche Jan 19, 2024
5ec98aa
Update actions
rubenvanassche Jan 19, 2024
b1bf847
Fix actions
rubenvanassche Jan 19, 2024
c7b4f97
Fix actions
rubenvanassche Jan 19, 2024
d122f0a
Fix actions
rubenvanassche Jan 19, 2024
f82bd4c
Remove resolved partial
rubenvanassche Jan 19, 2024
8bb91eb
Fix styling
rubenvanassche Jan 19, 2024
5b34518
Speed improvements
rubenvanassche Jan 19, 2024
ee4dff3
Fix styling
rubenvanassche Jan 19, 2024
2dc1de6
wip
rubenvanassche Jan 19, 2024
03ec7cf
Speed improvements, upgrade guide, changelog
rubenvanassche Jan 19, 2024
3e53091
Fix styling
rubenvanassche Jan 19, 2024
253d8ca
wip
rubenvanassche Jan 19, 2024
b26206a
wip
rubenvanassche Jan 19, 2024
8b19bb6
wip
rubenvanassche Jan 22, 2024
174b6e5
Refactor types even more
rubenvanassche Jan 22, 2024
cad4c6a
Fix styling
rubenvanassche Jan 22, 2024
1b7ba58
Docs refactor
rubenvanassche Jan 23, 2024
1d5d767
Permanent includes
rubenvanassche Jan 23, 2024
2761d73
Fix styling
rubenvanassche Jan 23, 2024
fcc0d8f
Fix differences between contexts
rubenvanassche Jan 23, 2024
a529ea4
Fix styling
rubenvanassche Jan 23, 2024
83f3498
Small fixes
rubenvanassche Jan 23, 2024
1ef1f3c
Small fixes
rubenvanassche Jan 23, 2024
d70d51a
Cleanup
rubenvanassche Jan 23, 2024
0d11df7
Fix styling
rubenvanassche Jan 23, 2024
d70d4d5
Fix PHPStan
rubenvanassche Jan 23, 2024
46e2d19
More test coverage
rubenvanassche Jan 24, 2024
3d65eb2
Fix styling
rubenvanassche Jan 24, 2024
d6c4804
Use validation payload as data payload
rubenvanassche Jan 24, 2024
6a3aa4f
Fix styling
rubenvanassche Jan 24, 2024
7c3c4f2
Add property mapping context
rubenvanassche Jan 24, 2024
5933f40
Fix small issues
rubenvanassche Jan 25, 2024
a48b4cf
Fix PHPStan
rubenvanassche Jan 25, 2024
410c179
Fix styling
rubenvanassche Jan 25, 2024
ee4540e
Merge branch 'main' into beyond-data
rubenvanassche Jan 25, 2024
df20b07
Merge branch 'beyond-data' of github.com:spatie/laravel-data-resource…
rubenvanassche Jan 25, 2024
19e5560
Add extra serialization casts and transformerts
rubenvanassche Jan 25, 2024
5e9d5db
Fix styling
rubenvanassche Jan 25, 2024
bfb3479
Add support for passing in creation contexts within the factory
rubenvanassche Jan 25, 2024
3c3ffcd
Fix styling
rubenvanassche Jan 25, 2024
23ea333
wip
rubenvanassche Jan 25, 2024
b8ea8ab
wip
rubenvanassche Jan 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
30 changes: 0 additions & 30 deletions .github/workflows/benchmark.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
coverage: none

- name: Install composer dependencies
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: run-tests
name: Run Tests

on: ['push', 'pull_request']

Expand All @@ -10,13 +10,11 @@ jobs:
matrix:
os: [ubuntu-latest]
php: [8.1, 8.2, 8.3]
laravel: [9.*, 10.*]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: 7.*
testbench: ^8.20

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

All notable changes to `laravel-data` will be documented in this file.

## 4.0.0

- Allow arrays, Collections, Paginators, ... to be used as DataCollections
- Add support for magically creating data collections
- Rewritten transformation system with respect to includeable properties
- Addition of collect method
- Removal of collection method
- Add support for using Laravel Model attributes as data properties
- Allow creating data objects using `from` without parameters
- Add support for a Dto and Resource object
- It is now a lot easier to validate all the payloads added to laravel-data
- Added contexts to the creation and transformation process
- Allow creating a data object or collection using a factory
- Speed up the process of creating and transforming data objects
- Add support for BNF syntax
- Laravel 10 requirement
- Rewritten docs

**Some more "internal" changes**

- Restructured tests for the future we have ahead
- The Type system was completely rewritten, allowing for a better performance and more flexibility in the future
- Benchmarks added to make data even faster

## 3.11.0 - 2023-12-21

- Add support for data structure caching #622
Expand Down
254 changes: 238 additions & 16 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,227 @@
# Upgrading

Because there are many breaking changes an upgrade is not that easy. There are many edge cases this guide does not cover. We accept PRs to improve this guide.
Because there are many breaking changes an upgrade is not that easy. There are many edge cases this guide does not
cover. We accept PRs to improve this guide.

## From v3 to v4

The following things are required when upgrading:

**Dependency changes (Likelihood Of Impact: High)**

The package now requires Laravel 10 as a minimum.

**Data::collection removal (Likelihood Of Impact: High)**

We've removed the Data::collection method in favour of the collect method. The collect method will return as type what
it gets as a type, so passing in an array will return an array of data objects. With the second parameter the output
type can be overwritten:

```php
// v3
SomeData::collection($items); // DataCollection

// v4
SomeData::collect($items); // array of SomeData

SomeData::collect($items, DataCollection::class); // DataCollection
```

If you were using the `$_collectionClass`, `$_paginatedCollectionClass` or `$_cursorPaginatedCollectionClass` properties
then take a look at the magic collect functionality on information about how to replace these.

If you want to keep the old behaviour, you can use the `WithDeprecatedCollectionMethod` trait and `DeprecatedData`
interface on your data objects which adds the collection method again, this trait will probably be removed in v5.

**Transformers (Likelihood Of Impact: High)**

If you've implemented a custom transformer, then add the new `TransformationContext` parameter to the `transform` method
signature.

```php
// v3
public function transform(DataProperty $property, mixed $value): mixed
{
// ...
}

// v4
public function transform(DataProperty $property,mixed $value,TransformationContext $context): mixed
{
// ...
}
```

**Casts (Likelihood Of Impact: High)**

If you've implemented a custom cast, then add the new $context `CreationContext` parameter to the `cast` method
signature and rename the old $context parameter to $properties.

```php
// v3
public function cast(DataProperty $property, mixed $value, array $context): mixed;
{
// ...
}

// v4
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed
{
// ...
}
```

**The transform method (Likelihood Of Impact: Medium)**

The transform method signature was changed to use a factory pattern instead of parameters:

```php
// v3
$data->transform(
transformValues:true,
wrapExecutionType:WrapExecutionType::Disabled,
mapPropertyNames:true,
);

// v4
$data->transform(
TransformationContextFactory::create()
->transformValues()
->wrapExecutionType(WrapExecutionType::Disabled)
->mapPropertyNames()
);
```

**Data Pipes (Likelihood Of Impact: Medium)**

If you've implemented a custom data pipe, then add the new `CreationContext` parameter to the `handle` method
signature and change the type of the $properties parameter from `Collection` to `array`. Lastly, return an `array` of
properties instead of a `Collection`.

```php
// v3
public function handle(mixed $payload, DataClass $class, Collection $properties): Collection
{
// ...
}

// v4
public function handle(mixed $payload, DataClass $class, array $properties, CreationContext $creationContext): array
{
// ...
}

```

**Invalid partials (Likelihood Of Impact: Medium)**

Previously when trying to include, exclude, only or except certain data properties that did not exist the package
continued working. From now on an exception will be thrown, these exceptions can be silenced by
setting `ignore_invalid_partials` to `true` within the config file

**Validated payloads (Likelihood Of Impact: Medium)**

Previously when validating data, the data object was being created from the payload provided, not the validated payload.

This payload can differ once you start using `exclude` rules which will exclude certain properties after validation.

Be sure that these properties are now typed as `Optional` since they can be missing from the payload.

```php
// v3

class SomeData extends Data {
#[ExcludeIf('excludeProperty', true)]
public string $property;
public bool $excludeProperty;
}
// Providing ['property' => 'something', 'excludeProperty' => true] will result in both fields set

// v4

class SomeData extends Data {
#[ExcludeIf('excludeProperty', true)]
public string|Optional $property;
public bool $excludeProperty;
}
// Providing ['property' => 'something', 'excludeProperty' => true] will result in only the excludeProperty field set, the property field will be optional
```

Also notice, nested data objects will use the `required` rule, Laravel validation will not include the nested array in the validated payload when this array is empty.

**Internal data structure changes (Likelihood Of Impact: Low)**

If you use internal data structures like `DataClass` and `DataProperty` then take a look at these classes, a lot as
changed and the creation process now uses factories instead of static constructors.

**Data interfaces and traits (Likelihood Of Impact: Low)**

We've split up some interfaces and traits, if you were manually using these on your own data implementation then take a
look what has changed:

- DataTrait was removed, you can easily build it yourself if required
- PrepareableData (interface and trait) were merged with BaseData
- An EmptyData (interface and trait) was added
- An ContextableData (interface and trait) was added

**ValidatePropertiesDataPipe (Likelihood Of Impact: Low)**

If you've used the `ValidatePropertiesDataPipe::allTypes` parameter to validate all types, then please use Spatie\LaravelData\Attributes\Validation\ExcludeIf;use Spatie\LaravelData\Optional;use the new
context when creating a data object to enable this or update your `data.php` config file with the new default.

**Removal of `withoutMagicalCreationFrom` (Likelihood Of Impact: Low)**

If you used the `withoutMagicalCreationFrom` method on a data object, the now use the context to disable magical data
object creation:

```php
// v3
SomeData::withoutMagicalCreationFrom($payload);

// v4
SomeData::factory()->withoutMagicalCreation()->from($payload);
```

**Partials (Likelihood Of Impact: Low)**

If you we're manually setting the `$_includes`, ` $_excludes`, `$_only`, `$_except` or `$_wrap` properties on a data
object, these have now been removed. Instead, you should use the new DataContext and add the partial.

**Removal of `DataCollectableTransformer` and `DataTransformer` (Likelihood Of Impact: Low)**

If you were using the `DataCollectableTransformer` or `DataTransformer` then please use the `TransformedDataCollectableResolver` and `TransformedDataResolver` instead.

**Removal of `DataObject` and `DataCollectable` (Likelihood Of Impact: Low)**

If you were using the `DataObject` or `DataCollectable` interfaces then please replace the interfaces based upon the `Data` and `DataCollection` interfaces to your preference.

**ValidationPath changes (Likelihood Of Impact: Low)**

If you were manually constructing a `ValidationPath` then please make sure to use an array instead of a string or null for the root level.

**Some advice with this new version of laravel-data**

We advise you to take a look at the following things:

- If you've cached data structures, be sure to clear the cache
- Take a look within your data objects if `DataCollection`'s, `DataPaginatedCollection`'s
and `DataCursorPaginatedCollection`'s can be replaced with regular arrays, Laravel Collections and Paginators making code a lot more readable
- Replace `DataCollectionOf` attributes with annotations, providing IDE completion and more info for static analyzers
- Replace some `extends Data` definitions with `extends Resource` or `extends Dto` for more minimal data objects
- When using `only` and `except` at the same time on a data object/collection, previously only the except would be
executed. From now on, we first execute the except and then the only.

## From v2 to v3

Upgrading to laravel data shouldn't take long, we've documented all possible changes just to provide the whole context. You probably won't have to do anything:
Upgrading to laravel data shouldn't take long, we've documented all possible changes just to provide the whole context.
You probably won't have to do anything:

- Laravel 9 is now required
- validation is completely rewritten
- rules are now generated based upon the payload provided, not what a payload possibly could be. This means rules can change depending on the provided payload.
- When you're injecting a `$payload`, `$relativePayload` or `$path` parameter in a custom rules method in your data object, then remove this and use the new `ValidationContext`:
- validation is completely rewritten
- rules are now generated based upon the payload provided, not what a payload possibly could be. This means rules
can change depending on the provided payload.
- When you're injecting a `$payload`, `$relativePayload` or `$path` parameter in a custom rules method in your data
object, then remove this and use the new `ValidationContext`:

```php
class SomeData extends Data {
Expand All @@ -29,14 +241,19 @@ class SomeData extends Data {
}
}
```
- The type of `$rules` in the RuleInferrer handle method changed from `RulesCollection` to `PropertyRules`
- RuleInferrers now take a $context parameter which is a `ValidationContext` in their handle method
- Validation attributes now keep track where they are being evaluated when you have nested data objects. Now field references are relative to the object and not to the root validated object
- Some resolvers are removed like: `DataClassValidationRulesResolver`, `DataPropertyValidationRulesResolver`
- The default order of rule inferrers has been changed
- The $payload parameter in the `getValidationRules` method is now required
- The $fields parameter was removed from the `getValidationRules` method, this now should be done outside of the package
- all data specific properties are now prefixed with _, to avoid conflicts with properties with your own defined properties. This is especially important when overwriting `$collectionClass`, `$paginatedCollectionClass`, `$cursorPaginatedCollectionClass`, be sure to add the extra _ within your data classes.

- The type of `$rules` in the RuleInferrer handle method changed from `RulesCollection` to `PropertyRules`
- RuleInferrers now take a $context parameter which is a `ValidationContext` in their handle method
- Validation attributes now keep track where they are being evaluated when you have nested data objects. Now field
references are relative to the object and not to the root validated object
- Some resolvers are removed like: `DataClassValidationRulesResolver`, `DataPropertyValidationRulesResolver`
- The default order of rule inferrers has been changed
- The $payload parameter in the `getValidationRules` method is now required
- The $fields parameter was removed from the `getValidationRules` method, this now should be done outside of the package
- all data specific properties are now prefixed with _, to avoid conflicts with properties with your own defined
properties. This is especially important when
overwriting `$collectionClass`, `$paginatedCollectionClass`, `$cursorPaginatedCollectionClass`, be sure to add the
extra _ within your data classes.
- Serialization logic is now updated and will only include data specific properties

## From v1 to v2
Expand All @@ -46,7 +263,9 @@ High impact changes
- Please check the most recent `data.php` config file and change yours accordingly
- The `Cast` interface now has a `$context` argument in the `cast` method
- The `RuleInferrer` interface now has a `RulesCollection` argument instead of an `array`
- By default, it is now impossible to include/exclude properties using a request parameter until manually specified. This behaviour can be overwritten by adding these methods to your data object:
- By default, it is now impossible to include/exclude properties using a request parameter until manually specified.
This behaviour can be overwritten by adding these methods to your data object:

```php
public static function allowedRequestIncludes(): ?array
{
Expand All @@ -58,8 +277,10 @@ High impact changes
return null;
}
```

- The `validate` method on a data object will not create a data object after validation, use `validateAndCreate` instead
- `DataCollection` is now being split into a `DataCollection`, `PaginatedDataCollection` and `CursorPaginatedDataCollection`
- `DataCollection` is now being split into a `DataCollection`, `PaginatedDataCollection`
and `CursorPaginatedDataCollection`

Low impact changes

Expand All @@ -72,4 +293,5 @@ Low impact changes
- The `DataTypeScriptTransformer` is updated for this new version, if you extend this then please take a look
- The `DataTransformer` and `DataCollectionTransformer` now use a `WrapExecutionType`
- The `filter` method was removed from paginated data collections
- The `through` and `filter` operations on a `DataCollection` will now happen instant instead of waiting for the transforming process
- The `through` and `filter` operations on a `DataCollection` will now happen instant instead of waiting for the
transforming process
Loading
Loading