Skip to content

Commit 66c59db

Browse files
Merge branch 'main' into auto-lazy
# Conflicts: # src/Support/Factories/DataPropertyFactory.php # tests/CreationTest.php # tests/Support/DataPropertyTest.php
2 parents 19131a3 + b6f63fa commit 66c59db

File tree

155 files changed

+3930
-1010
lines changed

Some content is hidden

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

155 files changed

+3930
-1010
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,57 @@
22

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

5+
## 4.11.1 - 2024-10-23
6+
7+
- Fix an issue where the cache structures command did not work if the directory did not exist (#892)
8+
9+
## 4.11.0 - 2024-10-22
10+
11+
### What's Changed
12+
13+
* feat: support "null to optional" by @innocenzi in https://github.com/spatie/laravel-data/pull/881
14+
* Register optimize commands by @erikgaal in https://github.com/spatie/laravel-data/pull/880
15+
16+
**Full Changelog**: https://github.com/spatie/laravel-data/compare/4.10.1...4.11.0
17+
18+
## 4.10.1 - 2024-10-07
19+
20+
- Fix an issue where optional default values would disable validation
21+
22+
## 4.10.0 - 2024-10-04
23+
24+
It has been a fews weeks, a mostly bugfix release with one new feature, enjoy!
25+
26+
- Fix an issue where required rules could not be combined with optional (#844)
27+
- Fix Livewire return type to make sure it can return everything in the data object (#836)
28+
- Fix issue where validation messages where ignored by collections nested in collections (#867)
29+
- Fix Resource to include Contextable data inteface (#868)
30+
- Stop NormalizedModel from initializing itself and try to lazy load properties when required (#870)
31+
- Passing an enum to caster handle without an error (#841)
32+
- Passing date objects to caster handle without an error (#842)
33+
- Allow setting a default mapping strategy (#846)
34+
35+
## 4.9.0 - 2024-09-10
36+
37+
- Move some interfaces around in order to avoid a circular chaos
38+
39+
## 4.8.2 - 2024-08-30
40+
41+
- Remove a circular dependency
42+
43+
## 4.81 - 2024-08-13
44+
45+
- Fix a missing dependency
46+
47+
## 4.8.0 - 2024-08-13
48+
49+
### What's Changed
50+
51+
* Detect data from collection by @clementbirkle in https://github.com/spatie/laravel-data/pull/812
52+
* Fix an issue where dd or dump did not work
53+
54+
**Full Changelog**: https://github.com/spatie/laravel-data/compare/4.7.2...4.8.0
55+
556
## 4.7.2 - 2024-07-25
657

758
- Fix issue where an exception was not always thrown while it should (#809)

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
"require" : {
1919
"php": "^8.1",
2020
"illuminate/contracts": "^10.0|^11.0",
21-
"phpdocumentor/type-resolver": "^1.5",
21+
"phpdocumentor/reflection": "^6.0",
2222
"spatie/laravel-package-tools": "^1.9.0",
2323
"spatie/php-structure-discoverer": "^2.0"
2424
},
2525
"require-dev" : {
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",
@@ -38,7 +38,7 @@
3838
"phpstan/extension-installer": "^1.1",
3939
"phpunit/phpunit": "^10.0",
4040
"spatie/invade": "^1.0",
41-
"spatie/laravel-typescript-transformer": "^2.3",
41+
"spatie/laravel-typescript-transformer": "^2.5",
4242
"spatie/pest-plugin-snapshots": "^2.1",
4343
"spatie/test-time": "^1.2"
4444
},

config/data.php

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
<?php
22

3-
use Illuminate\Support\Enumerable;
4-
53
return [
6-
/**
4+
/*
75
* The package will use this format when working with dates. If this option
86
* is an array, it will try to convert from the first format that works,
97
* and will serialize dates using the first format from the array.
108
*/
119
'date_format' => DATE_ATOM,
1210

13-
/**
11+
/*
1412
* When transforming or casting dates, the following timezone will be used to
1513
* convert the date to the correct timezone. If set to null no timezone will
1614
* be passed.
1715
*/
1816
'date_timezone' => null,
1917

20-
/**
18+
/*
2119
* It is possible to enable certain features of the package, these would otherwise
2220
* be breaking changes, and thus they are disabled by default. In the next major
2321
* version of the package, these features will be enabled by default.
2422
*/
2523
'features' => [
2624
'cast_and_transform_iterables' => false,
2725

28-
/**
26+
/*
2927
* When trying to set a computed property value, the package will throw an exception.
3028
* You can disable this behaviour by setting this option to true, which will then just
3129
* ignore the value being passed into the computed property and recalculate it.
3230
*/
3331
'ignore_exception_when_trying_to_set_computed_property_value' => false,
3432
],
3533

36-
/**
34+
/*
3735
* Global transformers will take complex types and transform them into simple
3836
* types.
3937
*/
@@ -43,7 +41,7 @@
4341
BackedEnum::class => Spatie\LaravelData\Transformers\EnumTransformer::class,
4442
],
4543

46-
/**
44+
/*
4745
* Global casts will cast values into complex types when creating a data
4846
* object from simple types.
4947
*/
@@ -53,7 +51,7 @@
5351
// Enumerable::class => Spatie\LaravelData\Casts\EnumerableCast::class,
5452
],
5553

56-
/**
54+
/*
5755
* Rule inferrers can be configured here. They will automatically add
5856
* validation rules to properties of a data object based upon
5957
* the type of the property.
@@ -66,7 +64,7 @@
6664
Spatie\LaravelData\RuleInferrers\AttributesRuleInferrer::class,
6765
],
6866

69-
/**
67+
/*
7068
* Normalizers return an array representation of the payload, or null if
7169
* it cannot normalize the payload. The normalizers below are used for
7270
* every data object, unless overridden in a specific data object class.
@@ -80,22 +78,22 @@
8078
Spatie\LaravelData\Normalizers\JsonNormalizer::class,
8179
],
8280

83-
/**
81+
/*
8482
* Data objects can be wrapped into a key like 'data' when used as a resource,
8583
* this key can be set globally here for all data objects. You can pass in
8684
* `null` if you want to disable wrapping.
8785
*/
8886
'wrap' => null,
8987

90-
/**
88+
/*
9189
* Adds a specific caster to the Symphony VarDumper component which hides
9290
* some properties from data objects and collections when being dumped
9391
* by `dump` or `dd`. Can be 'enabled', 'disabled' or 'development'
9492
* which will only enable the caster locally.
9593
*/
9694
'var_dumper_caster_mode' => 'development',
9795

98-
/**
96+
/*
9997
* It is possible to skip the PHP reflection analysis of data objects
10098
* when running in production. This will speed up the package. You
10199
* can configure where data objects are stored and which cache
@@ -121,53 +119,65 @@
121119
],
122120
],
123121

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

131-
/**
129+
/*
130+
* A data object can map the names of its properties when transforming (output) or when
131+
* creating (input). By default, the package will not map any names. You can set a
132+
* global strategy here, or override it on a specific data object.
133+
*/
134+
'name_mapping_strategy' => [
135+
'input' => null,
136+
'output' => null,
137+
],
138+
139+
/*
132140
* When using an invalid include, exclude, only or except partial, the package will
133141
* throw an exception. You can disable this behaviour by setting this option to true.
134142
*/
135143
'ignore_invalid_partials' => false,
136144

137-
/**
145+
/*
138146
* When transforming a nested chain of data objects, the package can end up in an infinite
139147
* loop when including a recursive relationship. The max transformation depth can be
140148
* set as a safety measure to prevent this from happening. When set to null, the
141149
* package will not enforce a maximum depth.
142150
*/
143151
'max_transformation_depth' => null,
144152

145-
/**
153+
/*
146154
* When the maximum transformation depth is reached, the package will throw an exception.
147155
* You can disable this behaviour by setting this option to true which will return an
148156
* empty array.
149157
*/
150158
'throw_when_max_transformation_depth_reached' => true,
151159

152-
/**
153-
* When using the `make:data` command, the package will use these settings to generate
154-
* the data classes. You can override these settings by passing options to the command.
155-
*/
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+
*/
156164
'commands' => [
157-
/**
165+
166+
/*
158167
* Provides default configuration for the `make:data` command. These settings can be overridden with options
159168
* passed directly to the `make:data` command for generating single Data classes, or if not set they will
160169
* automatically fall back to these defaults. See `php artisan make:data --help` for more information
161170
*/
162171
'make' => [
163-
/**
172+
173+
/*
164174
* The default namespace for generated Data classes. This exists under the application's root namespace,
165175
* so the default 'Data` will end up as '\App\Data', and generated Data classes will be placed in the
166176
* app/Data/ folder. Data classes can live anywhere, but this is where `make:data` will put them.
167177
*/
168178
'namespace' => 'Data',
169179

170-
/**
180+
/*
171181
* This suffix will be appended to all data classes generated by make:data, so that they are less likely
172182
* to conflict with other related classes, controllers or models with a similar name without resorting
173183
* to adding an alias for the Data object. Set to a blank string (not null) to disable.
@@ -176,7 +186,7 @@
176186
],
177187
],
178188

179-
/**
189+
/*
180190
* When using Livewire, the package allows you to enable or disable the synths
181191
* these synths will automatically handle the data objects and their
182192
* properties when used in a Livewire component.
Lines changed: 60 additions & 0 deletions
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)