Completed
Push — master ( df1f12...4872e5 )
by Lucas
05:42
created

JsonDefinitionField::getDefAsArray()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 29

Duplication

Lines 14
Ratio 35.9 %

Code Coverage

Tests 32
CRAP Score 1.0024

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 14
loc 39
ccs 32
cts 37
cp 0.8649
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 29
nc 1
nop 0
crap 1.0024
1
<?php
2
namespace Graviton\GeneratorBundle\Definition;
3
4
use Graviton\DocumentBundle\Entity\ExtReference;
5
use Graviton\DocumentBundle\Entity\Hash;
6
use Graviton\GeneratorBundle\Definition\Schema\XDynamicKey;
7
8
/**
9
 * A single field as specified in the json definition
10
 *
11
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
12
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
13
 * @link     http://swisscom.ch
14
 */
15
class JsonDefinitionField implements DefinitionElementInterface
16
{
17
    /**
18
     * Typemap from our source types to doctrine types
19
     */
20
    private static $doctrineTypeMap = [
21
        self::TYPE_STRING => 'string',
22
        self::TYPE_VARCHAR => 'string',
23
        self::TYPE_TEXT => 'string',
24
        self::TYPE_INTEGER => 'int',
25
        self::TYPE_LONG => 'int',
26
        self::TYPE_FLOAT => 'float',
27
        self::TYPE_DOUBLE => 'float',
28
        self::TYPE_DECIMAL => 'float',
29
        self::TYPE_DATETIME => 'date',
30
        self::TYPE_BOOLEAN => 'boolean',
31
        self::TYPE_OBJECT => 'hash',
32
        self::TYPE_EXTREF => 'extref',
33
    ];
34
35
    private static $serializerTypeMap = [
36
        self::TYPE_STRING => 'string',
37
        self::TYPE_VARCHAR => 'string',
38
        self::TYPE_TEXT => 'string',
39
        self::TYPE_INTEGER => 'integer',
40
        self::TYPE_LONG => 'integer',
41
        self::TYPE_FLOAT => 'double',
42
        self::TYPE_DOUBLE => 'double',
43
        self::TYPE_DECIMAL => 'double',
44
        self::TYPE_DATETIME => 'DateTime',
45
        self::TYPE_BOOLEAN => 'boolean',
46
        self::TYPE_OBJECT => Hash::class,
47
        self::TYPE_EXTREF => ExtReference::class,
48
    ];
49
50
    /**
51
     * @var string
52
     */
53
    private $name;
54
    /**
55
     * Our definition
56
     *
57
     * @var Schema\Field
58
     */
59
    private $definition;
60
61
    /**
62
     * Constructor
63
     *
64
     * @param string       $name       Field name
65
     * @param Schema\Field $definition Definition
66
     */
67 43
    public function __construct($name, Schema\Field $definition)
68
    {
69 43
        $this->name = $name;
70 43
        $this->definition = $definition;
71 43
    }
72
73
    /**
74
     * Returns the field definition
75
     *
76
     * @return Schema\Field definition
77
     */
78 12
    public function getDef()
79
    {
80 12
        return $this->definition;
81
    }
82
83
    /**
84
     * Returns the field name
85
     *
86
     * @return string Name
87
     */
88 11
    public function getName()
89
    {
90 11
        return $this->name;
91
    }
92
93
    /**
94
     * Returns the whole definition in array form
95
     *
96
     * @return array Definition
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,integer|str...array|XDynamicKey|null>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
97
     */
98 8
    public function getDefAsArray()
99
    {
100
        return [
101 8
            'length'            => $this->definition->getLength(),
102 8
            'title'             => $this->definition->getTitle(),
103 8
            'description'       => $this->definition->getDescription(),
104 8
            'readOnly'          => $this->definition->getReadOnly(),
105 8
            'required'          => $this->definition->getRequired(),
106 8
            'searchable'        => $this->definition->getSearchable(),
107 8
            'translatable'      => $this->definition->getTranslatable(),
108 8
            'collection'        => $this->definition->getCollection(),
109
110 8
            'name'              => $this->getName(),
111 8
            'type'              => $this->getType(),
112 8
            'exposedName'       => $this->getExposedName(),
113 8
            'doctrineType'      => $this->getTypeDoctrine(),
114 8
            'serializerType'    => $this->getTypeSerializer(),
115 8
            'xDynamicKey'       => $this->getXDynamicKey(),
116 8
            'relType'           => null,
117 8
            'isClassType'       => false,
118 8
            'constraints'       => array_map(
119 View Code Duplication
                function (Schema\Constraint $constraint) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
                    return [
121 3
                        'name'  => $constraint->getName(),
122 3
                        'options'   => array_map(
123 3
                            function (Schema\ConstraintOption $option) {
124
                                return [
125 3
                                    'name'  => $option->getName(),
126 3
                                    'value' => $option->getValue(),
127 3
                                ];
128 3
                            },
129 3
                            $constraint->getOptions()
130 3
                        )
131 3
                    ];
132 8
                },
133 8
                $this->definition->getConstraints()
134 8
            )
135 8
        ];
136
    }
137
138
    /**
139
     * Returns the field type in a doctrine-understandable way..
140
     *
141
     * @return string Type
142
     */
143 16
    public function getTypeDoctrine()
144
    {
145 16
        if (isset(self::$doctrineTypeMap[$this->getType()])) {
146 12
            return self::$doctrineTypeMap[$this->getType()];
147
        }
148
149
        // our fallback default
150 4
        return self::$doctrineTypeMap[self::TYPE_STRING];
151
    }
152
153
    /**
154
     * Returns the field type
155
     *
156
     * @return string Type
157
     */
158 28
    public function getType()
159
    {
160 28
        return strtolower($this->definition->getType());
161
    }
162
163
    /**
164
     * Returns the field type in a serializer-understandable way..
165
     *
166
     * @return string Type
167
     */
168 16
    public function getTypeSerializer()
169
    {
170 16
        if (isset(self::$serializerTypeMap[$this->getType()])) {
171 12
            return self::$serializerTypeMap[$this->getType()];
172
        }
173
174
        // our fallback default
175 4
        return self::$serializerTypeMap[self::TYPE_STRING];
176
    }
177
178
    /**
179
     * @return XDynamicKey|void
180
     */
181 8
    public function getXDynamicKey()
182
    {
183 8
        $key = $this->definition->getXDynamicKey();
184 8
        if ($key instanceof XDynamicKey) {
185 1
            return $key;
186
        }
187 7
    }
188
189
    /**
190
     * Gets the name this field should be exposed as (serializer concern).
191
     * Normally this is the name, but can be overriden by "exposeAs" property on the field.
192
     *
193
     * @return string exposed field name
194
     */
195 8
    private function getExposedName()
196
    {
197 8
        return $this->definition->getExposeAs() === null ?
198 8
            $this->getName() :
199 8
            $this->definition->getExposeAs();
200
    }
201
}
202