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.
Merge pull request #927 from andrey-helldar/patch/2025-01-10/19-55
New mappers added: `LowerCaseMapper` and `UpperCaseMapper`
- Loading branch information
Showing
6 changed files
with
84 additions
and
0 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 |
---|---|---|
@@ -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', | ||
]); | ||
``` |
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,13 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Mappers; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
class LowerCaseMapper implements NameMapper | ||
{ | ||
public function map(int|string $name): string|int | ||
{ | ||
return Str::lower($name); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Spatie\LaravelData\Mappers; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
class UpperCaseMapper implements NameMapper | ||
{ | ||
public function map(int|string $name): string|int | ||
{ | ||
return Str::upper($name); | ||
} | ||
} |
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