Normalizer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
c 2
b 1
f 0
lcom 0
cbo 2
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 4 1
A denormalize() 0 16 3
A detectType() 0 13 2
1
<?php
2
3
namespace Tonic\Component\ApiLayer\JsonRpc\Method\ArgumentMapper\Normalizer;
4
5
use phpDocumentor\Reflection\DocBlock;
6
use phpDocumentor\Reflection\DocBlock\Tag\VarTag;
7
8
/**
9
 * Simplest implementation of normalizer.
10
 */
11
class Normalizer implements NormalizerInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function normalize($object)
17
    {
18
        return json_decode(json_encode($object), true);
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function denormalize($className, array $data = [])
25
    {
26
        $reflectionClass = new \ReflectionClass($className);
27
        $instance = $reflectionClass->newInstanceWithoutConstructor();
28
        foreach ($reflectionClass->getProperties() as $reflectionProperty) {
29
            $propertyName = $reflectionProperty->getName();
30
            $type = $this->detectType($reflectionProperty);
31
32
            $instance->{$propertyName} = class_exists($type)
33
                ? $this->denormalize($type, $data[$propertyName])
34
                : $data[$propertyName]
35
            ;
36
        }
37
38
        return $instance;
39
    }
40
41
    /**
42
     * @param \ReflectionProperty $reflectionProperty
43
     *
44
     * @return string|null
45
     */
46
    public function detectType(\ReflectionProperty $reflectionProperty)
47
    {
48
        $docBlock = new DocBlock($reflectionProperty->getDocComment());
49
        $tags = $docBlock->getTagsByName('var');
50
        if (count($tags) == 0) {
51
            return null;
52
        }
53
54
        /** @var VarTag $typeTag */
55
        $typeTag = reset($tags);
56
57
        return $typeTag->getType();
58
    }
59
}
60