Skip to content

Commit

Permalink
Merge pull request #927 from andrey-helldar/patch/2025-01-10/19-55
Browse files Browse the repository at this point in the history
New mappers added: `LowerCaseMapper` and `UpperCaseMapper`
  • Loading branch information
rubenvanassche authored Jan 23, 2025
2 parents e7a45db + 1f2bf3a commit bd7b609
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/advanced-usage/available-mappers.md
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',
]);
```
1 change: 1 addition & 0 deletions docs/as-a-data-transfer-object/mapping-property-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
2 changes: 2 additions & 0 deletions docs/as-a-resource/mapping-property-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
13 changes: 13 additions & 0 deletions src/Mappers/LowerCaseMapper.php
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);
}
}
13 changes: 13 additions & 0 deletions src/Mappers/UpperCaseMapper.php
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);
}
}
14 changes: 14 additions & 0 deletions tests/MappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
use Spatie\LaravelData\Attributes\MapOutputName;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Mappers\CamelCaseMapper;
use Spatie\LaravelData\Mappers\LowerCaseMapper;
use Spatie\LaravelData\Mappers\ProvidedNameMapper;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
use Spatie\LaravelData\Mappers\StudlyCaseMapper;
use Spatie\LaravelData\Mappers\UpperCaseMapper;
use Spatie\LaravelData\Support\Transformation\TransformationContextFactory;
use Spatie\LaravelData\Tests\Fakes\DataWithMapper;
use Spatie\LaravelData\Tests\Fakes\SimpleData;
Expand Down Expand Up @@ -328,6 +330,12 @@ public function __construct(
#[MapName(StudlyCaseMapper::class)]
public string $studly_case = 'StudlyCase';

#[MapName(LowerCaseMapper::class)]
public string $lowercase = 'lowercase';

#[MapName(UpperCaseMapper::class)]
public string $uppercase = 'UPPERCASE';

#[MapName(new ProvidedNameMapper('i_provided'))]
public string $provided = 'provided';
};
Expand All @@ -336,17 +344,23 @@ public function __construct(
'camelCase' => 'camelCase',
'snake_case' => 'snake_case',
'StudlyCase' => 'StudlyCase',
'lowercase' => 'lowercase',
'UPPERCASE' => 'UPPERCASE',
'i_provided' => 'provided',
]);

expect($data::from([
'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');
});

0 comments on commit bd7b609

Please sign in to comment.