Completed
Push — master ( 2a6a78...c2b2ef )
by DELEVOYE
05:24
created

SubscriberNormalizer::supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the MindbazBundle package.
5
 *
6
 * (c) David DELEVOYE <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Kozikaza\MindbazBundle\Serializer;
13
14
use Kozikaza\MindbazBundle\Model\Subscriber;
15
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
16
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
17
18
/**
19
 * @author Vincent Chalamon <[email protected]>
20
 */
21
class SubscriberNormalizer implements NormalizerInterface, DenormalizerInterface
22
{
23
    /**
24
     * @param Subscriber $subscriber
25
     * @param string     $format
26
     * @param array      $context
27
     *
28
     * @return array
29
     */
30 1
    public function normalize($subscriber, $format = null, array $context = [])
31
    {
32 1
        $data = [];
33 1
        $reflectionClass = new \ReflectionClass(Subscriber::class);
34 1
        foreach ($reflectionClass->getProperties() as $property) {
35 1
            $property->setAccessible(true);
36 1
            $value = $property->getValue($subscriber);
37 1
            if (null === $value || empty($value)) {
38 1
                continue;
39
            }
40 1
            $data[$property->getName()] = $value;
41 1
        }
42
43 1
        return $data;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 2
    public function supportsNormalization($data, $format = null)
50
    {
51 2
        return $data instanceof Subscriber;
52
    }
53
54
    /**
55
     * @param array  $data
56
     * @param string $class
57
     * @param string $format
58
     * @param array  $context
59
     *
60
     * @return Subscriber
61
     */
62 1
    public function denormalize($data, $class, $format = null, array $context = [])
63
    {
64 1
        $subscriber = new Subscriber();
65 1
        $reflectionClass = new \ReflectionClass(Subscriber::class);
66 1
        foreach ($data as $key => $value) {
67 1
            $property = $reflectionClass->getProperty($key);
68 1
            $property->setAccessible(true);
69 1
            $property->setValue($subscriber, $value);
70 1
        }
71
72 1
        return $subscriber;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2
    public function supportsDenormalization($data, $type, $format = null)
79
    {
80 2
        return Subscriber::class === $type;
81
    }
82
}
83