diff --git a/docs/advanced-usage/available-mappers.md b/docs/advanced-usage/available-mappers.md new file mode 100644 index 000000000..764e327f9 --- /dev/null +++ b/docs/advanced-usage/available-mappers.md @@ -0,0 +1,41 @@ +--- +title: Available mappers +weight: 19 +--- + +Sometimes the property names in the array from which you're creating a data object might be different. +You can use mappers for property names when it is created from an array using attributes: + +```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', +]); +``` diff --git a/docs/as-a-data-transfer-object/mapping-property-names.md b/docs/as-a-data-transfer-object/mapping-property-names.md index ac72a2df4..7348ea379 100644 --- a/docs/as-a-data-transfer-object/mapping-property-names.md +++ b/docs/as-a-data-transfer-object/mapping-property-names.md @@ -91,3 +91,4 @@ SongData::from([ ]); ``` +You can also use other mappers to input and output property name mapping (see [available mappers](/docs/laravel-data/v4/advanced-usage/available-mappers)). diff --git a/docs/as-a-resource/mapping-property-names.md b/docs/as-a-resource/mapping-property-names.md index 770107b6e..6badd3c39 100644 --- a/docs/as-a-resource/mapping-property-names.md +++ b/docs/as-a-resource/mapping-property-names.md @@ -80,3 +80,5 @@ And a transformed version of the data object will look like this: 'record_company' => 'RCA Records', ] ``` + +You can also use other mappers to input and output property name mapping (see [available mappers](/docs/laravel-data/v4/advanced-usage/available-mappers)). diff --git a/src/Mappers/LowerCaseMapper.php b/src/Mappers/LowerCaseMapper.php new file mode 100644 index 000000000..e5aeb088f --- /dev/null +++ b/src/Mappers/LowerCaseMapper.php @@ -0,0 +1,13 @@ + 'camelCase', 'snake_case' => 'snake_case', 'StudlyCase' => 'StudlyCase', + 'lowercase' => 'lowercase', + 'UPPERCASE' => 'UPPERCASE', 'i_provided' => 'provided', ]); @@ -343,10 +353,14 @@ public function __construct( 'camelCase' => 'camelCase', 'snake_case' => 'snake_case', 'StudlyCase' => 'StudlyCase', + 'lowercase' => 'lowercase', + 'UPPERCASE' => 'UPPERCASE', 'i_provided' => 'provided', ])) ->camel_case->toBe('camelCase') ->snakeCase->toBe('snake_case') ->studly_case->toBe('StudlyCase') + ->lowercase->toBe('lowercase') + ->uppercase->toBe('UPPERCASE') ->provided->toBe('provided'); });