Completed
Push — master ( d950da...d2fb93 )
by Narcotic
09:23 queued 10s
created

JsonDefinitionArray   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90.63%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 112
ccs 29
cts 32
cp 0.9063
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getElement() 0 4 1
A getName() 0 4 1
A getTypeDoctrine() 0 4 1
A getType() 0 4 1
A getDefAsArray() 0 13 1
A getHashAnonymousHashRequired() 0 8 3
A getTypeSerializer() 0 11 3
1
<?php
2
namespace Graviton\GeneratorBundle\Definition;
3
4
/**
5
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
6
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
7
 * @link     http://swisscom.ch
8
 */
9
class JsonDefinitionArray implements DefinitionElementInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $name;
15
    /**
16
     * @var DefinitionElementInterface
17
     */
18
    private $element;
19
20
    /**
21
     * Constructor
22
     *
23
     * @param string                     $name    Field name
24
     * @param DefinitionElementInterface $element Array item definition
25
     */
26 42
    public function __construct($name, DefinitionElementInterface $element)
27
    {
28 42
        $this->name = $name;
29 42
        $this->element = $element;
30 42
    }
31
32
    /**
33
     * @return DefinitionElementInterface
34
     */
35 14
    public function getElement()
36
    {
37 14
        return $this->element;
38
    }
39
40
    /**
41
     * Returns the name of this field
42
     *
43
     * @return string
44
     */
45 6
    public function getName()
46
    {
47 6
        return $this->name;
48
    }
49
50
    /**
51
     * Returns the whole definition in array form
52
     *
53
     * @return array Definition
54
     */
55 2
    public function getDefAsArray()
56
    {
57 2
        return array_replace(
58 2
            $this->element->getDefAsArray(),
59
            [
60 2
                'name'              => $this->getName(),
61 2
                'type'              => $this->getType(),
62 2
                'doctrineType'      => $this->getTypeDoctrine(),
63 2
                'serializerType'    => $this->getTypeSerializer()
64 1
            ],
65 2
            $this->getHashAnonymousHashRequired()
66 1
        );
67
    }
68
69
    /**
70
     * Returns the field type in a doctrine-understandable way..
71
     *
72
     * @return string Type
73
     */
74 6
    public function getTypeDoctrine()
75
    {
76 6
        return $this->element->getTypeDoctrine().'[]';
77
    }
78
79
    /**
80
     * possible overrides. if the element is an anonymous hash, we will always
81
     * default to required => false.
82
     *
83
     * @return array additional overrides
84
     */
85 2
    private function getHashAnonymousHashRequired()
86
    {
87 2
       if ($this->element instanceof JsonDefinitionHash && $this->element->isAnonymous()) {
88
           return ['required' => false];
89
       }
90
91 2
       return [];
92
    }
93
94
    /**
95
     * Returns the field type
96
     *
97
     * @return string Type
98
     */
99 8
    public function getType()
100
    {
101 8
        return $this->element->getType().'[]';
102
    }
103
104
    /**
105
     * Returns the field type in a serializer-understandable way..
106
     *
107
     * @return string Type
108
     */
109 6
    public function getTypeSerializer()
110
    {
111 6
        $type = 'array<'.$this->element->getTypeSerializer().'>';
112
113
        // if we have a x-dynamic-key field, serializer type must be changed
114 6
        if ($this->element instanceof JsonDefinitionField && !empty($this->element->getXDynamicKey())) {
115
            $type = 'array<string, '.$this->element->getTypeSerializer().'>';
116
        }
117
118 6
        return $type;
119
    }
120
}
121