Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New mappers added: LowerCaseMapper and UpperCaseMapper #927

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
Loading