<?php
|
|
namespace App\Serializer;
|
|
use App\Dto\CreateSecretDTO;
|
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
|
|
/**
|
* This denormalizer is required when we got the form in an x-www-form-urlencoded format, because we have to
|
* make a CreateSecretDTO object from the input POST data.
|
*
|
* After this, the regular (non-x-www-form-urlencoded) pipeline is active, validation & creation is handled by that.
|
*/
|
class SecretDenormalizer implements ContextAwareDenormalizerInterface
|
{
|
/**
|
* @inheritDoc
|
*/
|
public function supportsDenormalization($data, string $type, string $format = null, array $context = [])
|
{
|
if ($type !== CreateSecretDTO::class)
|
{
|
return false;
|
}
|
|
return (isset($data['secret']) && isset($data['expireAfterViews']) && isset($data['expireAfter']));
|
}
|
|
/**
|
* @inheritDoc
|
*/
|
public function denormalize($data, string $type, string $format = null, array $context = [])
|
{
|
if ($type !== CreateSecretDTO::class)
|
{
|
return false;
|
}
|
|
return CreateSecretDTO::fromArray($data['secret'], $data['expireAfter'], $data['expireAfterViews']);
|
}
|
}
|