Skip to content

Commit

Permalink
New mappers added: LowerCaseMapper and UpperCaseMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jan 10, 2025
1 parent 88ba6b8 commit 8b55ad6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
33 changes: 33 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,36 @@ And a transformed version of the data object will look like this:
'record_company' => 'RCA Records',
]
```

### Available mappers:

```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,
) {
}
}
```
```php
[
'name' => 'Rick Astley',
'record_company' => 'RCA Records',
'country field' => 'Belgium',
'CityName' => 'Antwerp',
'addressline1' => 'some address line 1',
'ADDRESSLINE2' => 'some address line 2',
]
```
15 changes: 15 additions & 0 deletions src/Mappers/LowerCaseMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Spatie\LaravelData\Mappers;

use Illuminate\Support\Str;

class LowerCaseMapper implements NameMapper
{
public function map(int|string $name): string|int
{
return Str::lower($name);
}
}
15 changes: 15 additions & 0 deletions src/Mappers/UpperCaseMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

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 8b55ad6

Please sign in to comment.