PayloadNormalizer::denormalize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Payload\Serializer;
12
13
use GpsLab\Component\Payload\Payload;
14
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
15
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16
17
class PayloadNormalizer implements NormalizerInterface, DenormalizerInterface
18
{
19
    /**
20
     * @param mixed       $data
21
     * @param string|null $format
22
     *
23
     * @return bool
24
     */
25 1
    public function supportsNormalization($data, $format = null)
26
    {
27 1
        return $data instanceof Payload;
28
    }
29
30
    /**
31
     * @param Payload     $object
32
     * @param string|null $format
33
     * @param array       $context
34
     *
35
     * @return array
36
     */
37 1
    public function normalize($object, $format = null, array $context = [])
38
    {
39 1
        return $object->payload();
40
    }
41
42
    /**
43
     * @param array       $data
44
     * @param string      $class
45
     * @param string|null $format
46
     * @param array       $context
47
     *
48
     * @return Payload
49
     */
50 1
    public function denormalize($data, $class, $format = null, array $context = [])
51
    {
52 1
        return new $class($data);
53
    }
54
55
    /**
56
     * @param mixed       $data
57
     * @param string      $type
58
     * @param string|null $format
59
     *
60
     * @return bool
61
     */
62 2
    public function supportsDenormalization($data, $type, $format = null)
63
    {
64 2
        if (!is_array($data)) {
65 1
            return false;
66
        }
67
68
        // bigfix for is_subclass_of()
69
        // @see https://bugs.php.net/bug.php?id=53727
70
        try {
71 2
            $ref = new \ReflectionClass($type);
72 1
        } catch (\Exception $e) {
73 1
            return false; // class does not exist
74
        }
75
76 1
        return $ref->implementsInterface(Payload::class);
77
    }
78
}
79