Skip to content

Commit 635d813

Browse files
committed
Merge commit '516eec79268b07a8459e19bd6517866d480cee14' into feature-property-morphable
# Conflicts: # tests/CreationTest.php # tests/ValidationTest.php
2 parents cc76f20 + 516eec7 commit 635d813

File tree

106 files changed

+3251
-899
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+3251
-899
lines changed

.github/workflows/dependabot-auto-merge.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
- name: Dependabot metadata
1515
id: metadata
16-
uses: dependabot/fetch-metadata@v2.2.0
16+
uses: dependabot/fetch-metadata@v2.3.0
1717
with:
1818
github-token: "${{ secrets.GITHUB_TOKEN }}"
1919

.github/workflows/phpstan.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Setup PHP
2222
uses: shivammathur/setup-php@v2
2323
with:
24-
php-version: '8.2'
24+
php-version: '8.4'
2525
coverage: none
2626

2727
- name: Install composer dependencies

.github/workflows/run-tests.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
fail-fast: true
1010
matrix:
1111
os: [ubuntu-latest]
12-
php: [8.1, 8.2, 8.3]
12+
php: [8.1, 8.2, 8.3, 8.4]
1313
laravel: [10.*, 11.*]
1414
stability: [prefer-lowest, prefer-stable]
1515
include:
@@ -20,7 +20,8 @@ jobs:
2020
exclude:
2121
- php: 8.1
2222
laravel: 11.*
23-
23+
- php: 8.4
24+
laravel: 10.*
2425

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

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ node_modules
1313
.php-cs-fixer.cache
1414
.phpbench
1515
.DS_Store
16+
/.phpunit.cache/

CHANGELOG.md

+97
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,103 @@
22

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

5+
## 4.13.0 - 2025-01-24
6+
7+
### What's Changed
8+
9+
* Auto lazy by @rubenvanassche in https://github.com/spatie/laravel-data/pull/831
10+
11+
**Full Changelog**: https://github.com/spatie/laravel-data/compare/4.12.0...4.13.0
12+
13+
## 4.12.0 - 2025-01-24
14+
15+
What a release! Probably to biggest minor release we've ever done!
16+
17+
Some cool highlights:
18+
19+
#### Disabling optional values
20+
21+
Optional values are great, but sometimes a `null` value is desirable from now on you can do the following:
22+
23+
```php
24+
class SongData extends Data {
25+
public function __construct(
26+
public string $title,
27+
public string $artist,
28+
public Optional|null|string $album,
29+
) {
30+
}
31+
}
32+
33+
SongData::factory()
34+
->withoutOptionalValues()
35+
->from(['title' => 'Never gonna give you up', 'artist' => 'Rick Astley']); // album will `null` instead of `Optional`
36+
37+
38+
```
39+
#### Injecting property values
40+
41+
It was already possible to inject a Laravel route parameter when creating a data object, we've now extended this functionality quite a bit and also allow injecting dependencies from the container and the authenticated user.
42+
43+
```php
44+
class SongData extends Data {
45+
#[FromAuthenticatedUser]
46+
public UserData $user;
47+
}
48+
49+
50+
```
51+
#### Merging manual rules
52+
53+
In the past when the validation rules of a property were manually defined, the automatic validation rules for that property were omitted. From now on, you can define manual validation rules and merge them with the automatically generated validation rules:
54+
55+
```php
56+
```php
57+
#[MergeValidationRules]
58+
class SongData extends Data
59+
{
60+
public function __construct(
61+
public string $title,
62+
public string $artist,
63+
) {
64+
}
65+
66+
public static function rules(): array
67+
{
68+
return [
69+
'title' => ['max:20'],
70+
'artist' => ['max:20'],
71+
];
72+
}
73+
}
74+
75+
76+
```
77+
#### New property mappers:
78+
79+
We now ship by default a `Uppercase` and `Lowercase` mapper for mapping property names.
80+
81+
### All changes:
82+
83+
* Fix GitHub action fail by @rust17 in https://github.com/spatie/laravel-data/pull/918
84+
* Point to the right problem on ArgumentCountError exception by @nsvetozarevic in https://github.com/spatie/laravel-data/pull/884
85+
* Fix an issue where anonymous classes in castables were serialized (#903) by @rubenvanassche in https://github.com/spatie/laravel-data/pull/923
86+
* Add the ability to optionally merge automatically inferred rules with manual rules by @CWAscend in https://github.com/spatie/laravel-data/pull/848
87+
* Implement enum json serialization by @dont-know-php in https://github.com/spatie/laravel-data/pull/896
88+
* Use comments instead of docblocks in configuration file. by @edwinvdpol in https://github.com/spatie/laravel-data/pull/904
89+
* Casting DateTimeInterface: Truncate nanoseconds to microseconds (first 6 digits) / with Tests by @yob-yob in https://github.com/spatie/laravel-data/pull/908
90+
* Use container to call `Data::authorize()` to allow for dependencies by @cosmastech in https://github.com/spatie/laravel-data/pull/910
91+
* Improve type for CreationContextFactory::alwaysValidate by @sanfair in https://github.com/spatie/laravel-data/pull/925
92+
* Removed comma character from Data Rule stub by @andrey-helldar in https://github.com/spatie/laravel-data/pull/926
93+
* Use BaseData contract i/o Data concrete in CollectionAnnotation by @riesjart in https://github.com/spatie/laravel-data/pull/928
94+
* New mappers added: `LowerCaseMapper` and `UpperCaseMapper` by @andrey-helldar in https://github.com/spatie/laravel-data/pull/927
95+
* Allow disabling default Optional values in CreationContext by @ragulka in https://github.com/spatie/laravel-data/pull/931
96+
* Fix introduction.md by @pikant in https://github.com/spatie/laravel-data/pull/937
97+
* General code health improvements by @xHeaven in https://github.com/spatie/laravel-data/pull/920
98+
* Filling properties from current user by @c-v-c-v in https://github.com/spatie/laravel-data/pull/879
99+
100+
**Full Changelog**: https://github.com/spatie/laravel-data/compare/4.11.1...4.12.0
101+
5102
## 4.11.1 - 2024-10-23
6103

7104
- Fix an issue where the cache structures command did not work if the directory did not exist (#892)

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
"fakerphp/faker": "^1.14",
2727
"friendsofphp/php-cs-fixer": "^3.0",
2828
"inertiajs/inertia-laravel": "^1.2",
29+
"larastan/larastan": "^2.7",
2930
"livewire/livewire": "^3.0",
3031
"mockery/mockery": "^1.6",
3132
"nesbot/carbon": "^2.63",
32-
"nunomaduro/larastan": "^2.0",
3333
"orchestra/testbench": "^8.0|^9.0",
3434
"pestphp/pest": "^2.31",
3535
"pestphp/pest-plugin-laravel": "^2.0",

config/data.php

+26-24
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
<?php
22

33
return [
4-
/**
4+
/*
55
* The package will use this format when working with dates. If this option
66
* is an array, it will try to convert from the first format that works,
77
* and will serialize dates using the first format from the array.
88
*/
99
'date_format' => DATE_ATOM,
1010

11-
/**
11+
/*
1212
* When transforming or casting dates, the following timezone will be used to
1313
* convert the date to the correct timezone. If set to null no timezone will
1414
* be passed.
1515
*/
1616
'date_timezone' => null,
1717

18-
/**
18+
/*
1919
* It is possible to enable certain features of the package, these would otherwise
2020
* be breaking changes, and thus they are disabled by default. In the next major
2121
* version of the package, these features will be enabled by default.
2222
*/
2323
'features' => [
2424
'cast_and_transform_iterables' => false,
2525

26-
/**
26+
/*
2727
* When trying to set a computed property value, the package will throw an exception.
2828
* You can disable this behaviour by setting this option to true, which will then just
2929
* ignore the value being passed into the computed property and recalculate it.
3030
*/
3131
'ignore_exception_when_trying_to_set_computed_property_value' => false,
3232
],
3333

34-
/**
34+
/*
3535
* Global transformers will take complex types and transform them into simple
3636
* types.
3737
*/
@@ -41,7 +41,7 @@
4141
BackedEnum::class => Spatie\LaravelData\Transformers\EnumTransformer::class,
4242
],
4343

44-
/**
44+
/*
4545
* Global casts will cast values into complex types when creating a data
4646
* object from simple types.
4747
*/
@@ -51,7 +51,7 @@
5151
// Enumerable::class => Spatie\LaravelData\Casts\EnumerableCast::class,
5252
],
5353

54-
/**
54+
/*
5555
* Rule inferrers can be configured here. They will automatically add
5656
* validation rules to properties of a data object based upon
5757
* the type of the property.
@@ -64,7 +64,7 @@
6464
Spatie\LaravelData\RuleInferrers\AttributesRuleInferrer::class,
6565
],
6666

67-
/**
67+
/*
6868
* Normalizers return an array representation of the payload, or null if
6969
* it cannot normalize the payload. The normalizers below are used for
7070
* every data object, unless overridden in a specific data object class.
@@ -78,22 +78,22 @@
7878
Spatie\LaravelData\Normalizers\JsonNormalizer::class,
7979
],
8080

81-
/**
81+
/*
8282
* Data objects can be wrapped into a key like 'data' when used as a resource,
8383
* this key can be set globally here for all data objects. You can pass in
8484
* `null` if you want to disable wrapping.
8585
*/
8686
'wrap' => null,
8787

88-
/**
88+
/*
8989
* Adds a specific caster to the Symphony VarDumper component which hides
9090
* some properties from data objects and collections when being dumped
9191
* by `dump` or `dd`. Can be 'enabled', 'disabled' or 'development'
9292
* which will only enable the caster locally.
9393
*/
9494
'var_dumper_caster_mode' => 'development',
9595

96-
/**
96+
/*
9797
* It is possible to skip the PHP reflection analysis of data objects
9898
* when running in production. This will speed up the package. You
9999
* can configure where data objects are stored and which cache
@@ -119,14 +119,14 @@
119119
],
120120
],
121121

122-
/**
122+
/*
123123
* A data object can be validated when created using a factory or when calling the from
124124
* method. By default, only when a request is passed the data is being validated. This
125125
* behaviour can be changed to always validate or to completely disable validation.
126126
*/
127127
'validation_strategy' => \Spatie\LaravelData\Support\Creation\ValidationStrategy::OnlyRequests->value,
128128

129-
/**
129+
/*
130130
* A data object can map the names of its properties when transforming (output) or when
131131
* creating (input). By default, the package will not map any names. You can set a
132132
* global strategy here, or override it on a specific data object.
@@ -136,46 +136,48 @@
136136
'output' => null,
137137
],
138138

139-
/**
139+
/*
140140
* When using an invalid include, exclude, only or except partial, the package will
141141
* throw an exception. You can disable this behaviour by setting this option to true.
142142
*/
143143
'ignore_invalid_partials' => false,
144144

145-
/**
145+
/*
146146
* When transforming a nested chain of data objects, the package can end up in an infinite
147147
* loop when including a recursive relationship. The max transformation depth can be
148148
* set as a safety measure to prevent this from happening. When set to null, the
149149
* package will not enforce a maximum depth.
150150
*/
151151
'max_transformation_depth' => null,
152152

153-
/**
153+
/*
154154
* When the maximum transformation depth is reached, the package will throw an exception.
155155
* You can disable this behaviour by setting this option to true which will return an
156156
* empty array.
157157
*/
158158
'throw_when_max_transformation_depth_reached' => true,
159159

160-
/**
161-
* When using the `make:data` command, the package will use these settings to generate
162-
* the data classes. You can override these settings by passing options to the command.
163-
*/
160+
/*
161+
* When using the `make:data` command, the package will use these settings to generate
162+
* the data classes. You can override these settings by passing options to the command.
163+
*/
164164
'commands' => [
165-
/**
165+
166+
/*
166167
* Provides default configuration for the `make:data` command. These settings can be overridden with options
167168
* passed directly to the `make:data` command for generating single Data classes, or if not set they will
168169
* automatically fall back to these defaults. See `php artisan make:data --help` for more information
169170
*/
170171
'make' => [
171-
/**
172+
173+
/*
172174
* The default namespace for generated Data classes. This exists under the application's root namespace,
173175
* so the default 'Data` will end up as '\App\Data', and generated Data classes will be placed in the
174176
* app/Data/ folder. Data classes can live anywhere, but this is where `make:data` will put them.
175177
*/
176178
'namespace' => 'Data',
177179

178-
/**
180+
/*
179181
* This suffix will be appended to all data classes generated by make:data, so that they are less likely
180182
* to conflict with other related classes, controllers or models with a similar name without resorting
181183
* to adding an alias for the Data object. Set to a blank string (not null) to disable.
@@ -184,7 +186,7 @@
184186
],
185187
],
186188

187-
/**
189+
/*
188190
* When using Livewire, the package allows you to enable or disable the synths
189191
* these synths will automatically handle the data objects and their
190192
* properties when used in a Livewire component.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: Available property mappers
3+
weight: 19
4+
---
5+
6+
In previous sections we've already seen how
7+
to [create](/docs/laravel-data/v4/as-a-data-transfer-object/mapping-property-names) data objects where the keys of the
8+
payload differ from the property names of the data object. It is also possible
9+
to [transform](/docs/laravel-data/v4/as-a-resource/mapping-property-names) data objects to an
10+
array/json/... where the keys of the payload differ from the property names of the data object.
11+
12+
These mappings can be set manually put the package also provide a set of mappers that can be used to automatically map
13+
property names:
14+
15+
```php
16+
class ContractData extends Data
17+
{
18+
public function __construct(
19+
#[MapName(CamelCaseMapper::class)]
20+
public string $name,
21+
#[MapName(SnakeCaseMapper::class)]
22+
public string $recordCompany,
23+
#[MapName(new ProvidedNameMapper('country field'))]
24+
public string $country,
25+
#[MapName(StudlyCaseMapper::class)]
26+
public string $cityName,
27+
#[MapName(LowerCaseMapper::class)]
28+
public string $addressLine1,
29+
#[MapName(UpperCaseMapper::class)]
30+
public string $addressLine2,
31+
) {
32+
}
33+
}
34+
```
35+
36+
Creating the data object can now be done as such:
37+
38+
```php
39+
ContractData::from([
40+
'name' => 'Rick Astley',
41+
'record_company' => 'RCA Records',
42+
'country field' => 'Belgium',
43+
'CityName' => 'Antwerp',
44+
'addressline1' => 'some address line 1',
45+
'ADDRESSLINE2' => 'some address line 2',
46+
]);
47+
```
48+
49+
When transforming such a data object the payload will look like this:
50+
51+
```json
52+
{
53+
"name" : "Rick Astley",
54+
"record_company" : "RCA Records",
55+
"country field" : "Belgium",
56+
"CityName" : "Antwerp",
57+
"addressline1" : "some address line 1",
58+
"ADDRESSLINE2" : "some address line 2"
59+
}
60+
```

0 commit comments

Comments
 (0)