Completed
Push — feature/EVO-4702 ( fbb50e )
by Leonard
14:02
created

JsonDefinitionField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 86
    public function __construct($name, Schema\Field $definition)
68
    {
69 86
        $this->name = $name;
70 86
        $this->definition = $definition;
71 86
    }
72
73
    /**
74
     * Returns the field definition
75
     *
76
     * @return Schema\Field definition
77
     */
78 24
    public function getDef()
79
    {
80 24
        return $this->definition;
81
    }
82
83
    /**
84
     * Returns the field name
85
     *
86
     * @return string Name
87
     */
88 22
    public function getName()
89
    {
90 22
        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 16
    public function getDefAsArray()
99
    {
100
        return [
101 16
            'length'            => $this->definition->getLength(),
102 16
            'title'             => $this->definition->getTitle(),
103 16
            'description'       => $this->definition->getDescription(),
104 16
            'readOnly'          => $this->definition->getReadOnly(),
105 16
            'required'          => $this->definition->getRequired(),
106 16
            'searchable'        => $this->definition->getSearchable(),
107 16
            'translatable'      => $this->definition->getTranslatable(),
108 16
            'collection'        => $this->definition->getCollection(),
109
110 16
            'name'              => $this->getName(),
111 16
            'type'              => $this->getType(),
112 16
            'exposedName'       => $this->getExposedName(),
113 16
            'doctrineType'      => $this->getTypeDoctrine(),
114 16
            'serializerType'    => $this->getTypeSerializer(),
115 16
            'xDynamicKey'       => $this->getXDynamicKey(),
116 8
            'relType'           => null,
117 8
            'isClassType'       => false,
118 16
            '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 6
                        'name'  => $constraint->getName(),
122 6
                        'options'   => array_map(
123 6
                            function (Schema\ConstraintOption $option) {
124
                                return [
125 6
                                    'name'  => $option->getName(),
126 6
                                    'value' => $option->getValue(),
127 3
                                ];
128 6
                            },
129 6
                            $constraint->getOptions()
130 3
                        )
131 3
                    ];
132 16
                },
133 16
                $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 32
    public function getTypeDoctrine()
144
    {
145 32
        if (isset(self::$doctrineTypeMap[$this->getType()])) {
146 24
            return self::$doctrineTypeMap[$this->getType()];
147
        }
148
149
        // our fallback default
150 8
        return self::$doctrineTypeMap[self::TYPE_STRING];
151
    }
152
153
    /**
154
     * Returns the field type
155
     *
156
     * @return string Type
157
     */
158 56
    public function getType()
159
    {
160 56
        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 32
    public function getTypeSerializer()
169
    {
170 32
        if (isset(self::$serializerTypeMap[$this->getType()])) {
171 24
            return self::$serializerTypeMap[$this->getType()];
172
        }
173
174
        // our fallback default
175 8
        return self::$serializerTypeMap[self::TYPE_STRING];
176
    }
177
178
    /**
179
     * @return XDynamicKey|void
180
     */
181 16
    public function getXDynamicKey()
182
    {
183 16
        $key = $this->definition->getXDynamicKey();
184 16
        if ($key instanceof XDynamicKey) {
185 2
            return $key;
186
        }
187 14
    }
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 16
    private function getExposedName()
196
    {
197 16
        return $this->definition->getExposeAs() === null ?
198 13
            $this->getName() :
199 16
            $this->definition->getExposeAs();
200
    }
201
}
202