Sometimes you might want to change the name of a property in the transformed payload, with attributes this is possible:
class ContractData extends Data
{
public function __construct(
public string $name,
#[MapOutputName('record_company')]
public string $recordCompany,
) {
}
}
Now our array looks like this:
[
'name' => 'Rick Astley',
'record_company' => 'RCA Records',
]
Changing all property names in a data object to snake_case as output data can be done as such:
#[MapOutputName(SnakeCaseMapper::class)]
class ContractData extends Data
{
public function __construct(
public string $name,
public string $recordCompany,
) {
}
}
You can also use the MapName
attribute when you want to combine input and output property name mapping:
#[MapName(SnakeCaseMapper::class)]
class ContractData extends Data
{
public function __construct(
public string $name,
public string $recordCompany,
) {
}
}
You can now create a data object as such:
$contract = new ContractData(
name: 'Rick Astley',
recordCompany: 'RCA Records',
);
And a transformed version of the data object will look like this:
[
'name' => 'Rick Astley',
'record_company' => 'RCA Records',
]