A gateway/firewall task to be able to talk someone about the real job
Fibinger Ádám
2021-10-22 fd7692eae00cbb0db3e6b732f68357e3c64a8a8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?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']);
    }
}