In previous sections we've already seen how
to create data objects where the keys of the
payload differ from the property names of the data object. It is also possible
to transform data objects to an
array/json/... where the keys of the payload differ from the property names of the data object.
These mappings can be set manually put the package also provide a set of mappers that can be used to automatically map
property names:
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:
ContractData::from([
'name' => 'Rick Astley',
'record_company' => 'RCA Records',
'country field' => 'Belgium',
'CityName' => 'Antwerp',
'addressline1' => 'some address line 1',
'ADDRESSLINE2' => 'some address line 2',
]);
When transforming such a data object the payload will look like this:
{
"name" : "Rick Astley",
"record_company" : "RCA Records",
"country field" : "Belgium",
"CityName" : "Antwerp",
"addressline1" : "some address line 1",
"ADDRESSLINE2" : "some address line 2"
}