ExceptionNormalizer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace W2w\Lib\Apie\Plugins\Core\Normalizers;
3
4
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
5
use ReflectionClass;
6
use Throwable;
7
use W2w\Lib\ApieObjectAccessNormalizer\Exceptions\ValidationException;
8
9
/**
10
 * Class that normalizes a throwable class.
11
 *
12
 * @deprecated use ExceptionObjectAccess instead
13
 */
14
class ExceptionNormalizer implements NormalizerInterface
15
{
16
    private $showStack;
17
18
    /**
19
     * @param bool $showStack If true, outputs a stack trace.
20
     */
21
    public function __construct(bool $showStack)
22
    {
23
        $this->showStack = $showStack;
24
    }
25
26
    /**
27
     * @param Throwable $object
28
     * @param string|null $format
29
     * @param array $context
30
     * @return string[]
31
     */
32
    public function normalize($object, $format = null, array $context = [])
33
    {
34
        $res = [
35
            'type'    => (new ReflectionClass($object))->getShortName(),
36
            'message' => $object->getMessage(),
37
            'code'    => $object->getCode(),
38
        ];
39
        if ($this->showStack) {
40
            $res['trace'] = $object->getTraceAsString();
41
        }
42
        if ($object instanceof ValidationException) {
43
            $res['errors'] = $object->getErrors();
44
        }
45
46
        return $res;
47
    }
48
49
    /**
50
     * @param mixed $data
51
     * @param string|null $format
52
     * @return bool
53
     */
54
    public function supportsNormalization($data, $format = null)
55
    {
56
        return $data instanceof Throwable;
57
    }
58
}
59