generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # src/Support/Factories/DataPropertyFactory.php # tests/CreationTest.php # tests/Support/DataPropertyTest.php
- Loading branch information
Showing
155 changed files
with
3,930 additions
and
1,010 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ node_modules | |
.php-cs-fixer.cache | ||
.phpbench | ||
.DS_Store | ||
/.phpunit.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
title: Available property mappers | ||
weight: 19 | ||
--- | ||
|
||
In previous sections we've already seen how | ||
to [create](/docs/laravel-data/v4/as-a-data-transfer-object/mapping-property-names) data objects where the keys of the | ||
payload differ from the property names of the data object. It is also possible | ||
to [transform](/docs/laravel-data/v4/as-a-resource/mapping-property-names) data objects to an | ||
array/json/... where the keys of the payload differ from the property names of the data object. | ||
|
||
These mappings can be set manually put the package also provide a set of mappers that can be used to automatically map | ||
property names: | ||
|
||
```php | ||
class ContractData extends Data | ||
{ | ||
public function __construct( | ||
#[MapName(CamelCaseMapper::class)] | ||
public string $name, | ||
#[MapName(SnakeCaseMapper::class)] | ||
public string $recordCompany, | ||
#[MapName(new ProvidedNameMapper('country field'))] | ||
public string $country, | ||
#[MapName(StudlyCaseMapper::class)] | ||
public string $cityName, | ||
#[MapName(LowerCaseMapper::class)] | ||
public string $addressLine1, | ||
#[MapName(UpperCaseMapper::class)] | ||
public string $addressLine2, | ||
) { | ||
} | ||
} | ||
``` | ||
|
||
Creating the data object can now be done as such: | ||
|
||
```php | ||
ContractData::from([ | ||
'name' => 'Rick Astley', | ||
'record_company' => 'RCA Records', | ||
'country field' => 'Belgium', | ||
'CityName' => 'Antwerp', | ||
'addressline1' => 'some address line 1', | ||
'ADDRESSLINE2' => 'some address line 2', | ||
]); | ||
``` | ||
|
||
When transforming such a data object the payload will look like this: | ||
|
||
```json | ||
{ | ||
"name" : "Rick Astley", | ||
"record_company" : "RCA Records", | ||
"country field" : "Belgium", | ||
"CityName" : "Antwerp", | ||
"addressline1" : "some address line 1", | ||
"ADDRESSLINE2" : "some address line 2" | ||
} | ||
``` |
Oops, something went wrong.