PHP code example of paysera / lib-normalization-bundle
1. Go to this page and download the library: Download paysera/lib-normalization-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
paysera / lib-normalization-bundle example snippets
// ...
class ContactDetailsNormalizer implements NormalizerInterface, ObjectDenormalizerInterface, TypeAwareInterface
{
public function getType(): string
{
return ContactDetails::class;
}
/**
* @param ContactDetails $data
* @param NormalizationContext $normalizationContext
*
* @return array
*/
public function normalize($data, NormalizationContext $normalizationContext)
{
return [
'email' => $data->getEmail(),
// will automatically follow-up with normalization by guessed types:
'residence_address' => $data->getResidenceAddress(),
'shipping_addesses' => $data->getShippingAddresses(),
];
}
public function denormalize(ObjectWrapper $data, DenormalizationContext $context)
{
return (new ContactDetails())
->setEmail($data->getRequiredString('email'))
->setResidenceAddress(
$context->denormalize($data->getRequiredObject('residence_address'), Address::class)
)
->setShippingAddresses(
$context->denormalizeArray($data->getArrayOfObject('shipping_addesses'), Address::class)
)
;
}
}
// ...
class AddressNormalizer implements NormalizerInterface, ObjectDenormalizerInterface, TypeAwareInterface
{
private $countryRepository;
private $addressBuilder;
// ...
public function getType(): string
{
return Address::class;
}
/**
* @param Address $data
* @param NormalizationContext $normalizationContext
*
* @return array
*/
public function normalize($data, NormalizationContext $normalizationContext)
{
return [
'country_code' => $data->getCountry()->getCode(),
'city' => $data->getCity(),
'full_address' => $this->addressBuilder->buildAsText($data->getStreetData()),
];
}
public function denormalize(ObjectWrapper $data, DenormalizationContext $context)
{
$code = $data->getRequiredString('country_code');
$country = $this->countryRepository->findOneByCode($code);
if ($country === null) {
throw new InvalidDataException(sprintf('Unknown country %s', $code));
}
return (new Address())
->setCountry($country)
->setCity($data->getRequiredString('city'))
->setStreetData(
$this->addressBuilder->parseFromText($data->getRequiredString('full_address'))
)
;
}
}
// inject $coreDenormalizer as paysera_normalization.core_denormalizer
// FQCN also works as service ID, so autowiring should work if you use it
// must be stdClass, not array
$data = json_decode('{
"email":"[email protected]",
"residence_address":{"country_code":"LT","city":"Vilnius","full_address":"Park street 182b-12"},
"shipping_addresses":[]
}');
$contactDetails = $coreDenormalizer->denormalize($data, ContactDetails::class);
// inject $coreNormalizer as paysera_normalization.core_normalizer
// FQCN also works as service ID, so autowiring should work if you use it
$normalized = $coreNormalizer->normalize($contactDetails);
var_dump($normalized);
// object(stdClass)#1 (3) { ...
// ...
class ContactDetailsNormalizer implements NormalizerInterface
{
/**
* @param ContactDetails $data
* @param NormalizationContext $normalizationContext
*
* @return array
*/
public function normalize($data, NormalizationContext $normalizationContext)
{
return [
'email' => $data->getEmail(),
// this is only a possible optimization, as field would be still filtered out afterwards:
'residence_address' => $normalizationContext->isFieldIncluded('residence_address')
? $data->getResidenceAddress()
: null,
// this is a field that will not be returned except if explicitly asked for:
'shipping_addesses' => $normalizationContext->isFieldExplicitlyIncluded('shipping_addesses')
? $data->getShippingAddresses()
: null,
];
}
}
yaml
paysera_normalization:
register_normalizers:
date_time: # registers de/normalizers for DateTime, DateTimeImmutable and DateTimeInterface
format: "U" # Unix timestamp. Use any from https://www.php.net/manual/en/function.date.php
public function normalize($data, string $type = null, NormalizationContext $context = null)
public function denormalize($data, string $type, DenormalizationContext $context = null)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.