Passed
Pull Request — main (#56)
by Niels
10:35
created

ProblemExceptionNormalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 45
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 7 2
A normalize() 0 15 3
A unsetKeysWithNullValue() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the OpenapiBundle package.
7
 *
8
 * (c) Niels Nijens <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nijens\OpenapiBundle\ExceptionHandling\Normalizer;
15
16
use Nijens\OpenapiBundle\ExceptionHandling\Exception\ProblemExceptionInterface;
17
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
18
use Symfony\Component\Serializer\Exception\LogicException;
19
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
22
use Throwable;
23
24
/**
25
 * Normalizes a {@see Throwable} implementing the {@see ProblemExceptionInterface}.
26
 *
27
 * @author Niels Nijens <[email protected]>
28
 */
29
final class ProblemExceptionNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
30
{
31
    use NormalizerAwareTrait;
32
33
    private const ALREADY_CALLED = 'nijens_openapi.problem_exception_normalizer.already_called';
34
35
    public function normalize($object, $format = null, array $context = [])
36
    {
37
        if ($object instanceof ProblemExceptionInterface === false) {
38
            throw new InvalidArgumentException(sprintf('The object must implement "%s".', ProblemExceptionInterface::class));
39
        }
40
41
        if (isset($context[self::ALREADY_CALLED])) {
42
            throw new LogicException(sprintf('The normalizer "%s" can only be called once.', self::class));
43
        }
44
45
        $context[self::ALREADY_CALLED] = true;
46
47
        $data = $this->normalizer->normalize($object, $format, $context);
48
49
        return $this->unsetKeysWithNullValue($data);
50
    }
51
52
    public function supportsNormalization($data, $format = null, array $context = []): bool
53
    {
54
        if (isset($context[self::ALREADY_CALLED])) {
55
            return false;
56
        }
57
58
        return $data instanceof ProblemExceptionInterface;
59
    }
60
61
    private function unsetKeysWithNullValue(array $data)
62
    {
63
        foreach ($data as $key => $value) {
64
            if (is_array($value)) {
65
                $data[$key] = $this->unsetKeysWithNullValue($value);
66
            }
67
68
            if ($value === null) {
69
                unset($data[$key]);
70
            }
71
        }
72
73
        return $data;
74
    }
75
}
76