Passed
Pull Request — master (#37)
by Bruno
07:17
created

VueCode::getTemplateData()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 70
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
eloc 44
c 6
b 2
f 0
dl 0
loc 70
rs 8.9048
cc 5
nc 12
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Vue;
4
5
use Formularium\Datatype;
6
use Formularium\Datatype\Datatype_bool;
7
use Formularium\Datatype\Datatype_number;
8
use Formularium\Datatype\Datatype_string;
9
use Formularium\Field;
10
use Formularium\Frontend\Vue\VueCode\Computed;
11
use Formularium\Frontend\Vue\VueCode\Prop;
12
use Formularium\HTMLNode;
13
use Formularium\Model;
14
use Formularium\RenderableParameter;
15
16
use function Safe\json_encode;
17
18
/**
19
 * Converts an array to a JS object. Unlike JSON this does serialize
20
 * data to strings, and allow functions, etc. If you need to add
21
 * strings they are expected to be previously quoted with " or '
22
 *
23
 * @param array $data
24
 * @return array
25
 */
26
function expandJS(array $data): array
27
{
28
    return array_map(function ($key, $value) {
29
        return "$key" .
30
            (
31
                is_array($value) ?
32
                ': {' . implode(",\n", expandJS($value)) . '}' :
33
                ($value ? ':' . $value : '')
34
            );
35
    }, array_keys($data), $data);
36
}
37
38
class VueCode
39
{
40
41
    /**
42
     * Appended to the field variable names to handle models stored in an object field.
43
     *
44
     * Allows you to declare the model like this:
45
     *
46
     * data() {
47
     *   return {
48
     *       model: model,
49
     *   };
50
     * },
51
     *
52
     * @var string
53
     */
54
    public $fieldModelVariable = '';
55
56
    /**
57
     * Extra props.
58
     *
59
     * @var Prop[]
60
     */
61
    public $extraProps = [];
62
63
    /**
64
     * extra data fields
65
     *
66
     * @var string[]
67
     */
68
    public $extraData = [];
69
70
    /**
71
     * The list of imports to add: import 'key' from 'value'
72
     *
73
     * @var string[]
74
     */
75
    public $imports = [];
76
77
    /**
78
     * @var Computed[]
79
     */
80
    public $computed = [];
81
82
    /**
83
     * @var string[]
84
     */
85
    public $methods = [];
86
87
    /**
88
     * @var string[]
89
     */
90
    public $other = [];
91
92
    /**
93
     * @var VueCodeAbstractRenderer
94
     */
95
    public $renderer;
96
97
    public function __construct(string $rendererClass = Vue2CodeDictRenderer::class)
98
    {
99
        $this->renderer = new $rendererClass($this);
100
    }
101
102
    /**
103
     * @param string $name
104
     * @param string $code
105
     * @return self
106
     */
107
    public function appendMethod($name, $code): self
108
    {
109
        $this->methods[$name] = $code;
110
        return $this;
111
    }
112
113
    /**
114
     * @param string $name
115
     * @param string $code
116
     * @return self
117
     */
118
    public function appendOther($name, $code): self
119
    {
120
        $this->other[$name] = $code;
121
        return $this;
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    public function getExtraProps(): array
128
    {
129
        return $this->extraProps;
130
    }
131
132
    /**
133
     *
134
     * @param Prop[] $extraProps
135
     *
136
     * @return  self
137
     */
138
    public function setExtraProps(array $extraProps): self
139
    {
140
        $this->extraProps = $extraProps;
141
142
        return $this;
143
    }
144
145
    /**
146
     *
147
     * @param Prop $prop
148
     *
149
     * @return  self
150
     */
151
    public function appendExtraProp(Prop $prop): self
152
    {
153
        $this->extraProps[] = $prop;
154
155
        return $this;
156
    }
157
158
    /**
159
     * Appends to the `data` field.
160
     *
161
     * @param string $name
162
     * @param mixed $value
163
     * @return self
164
     */
165
    public function appendExtraData(string $name, $value): self
166
    {
167
        $this->extraData[$name] = $value;
168
        return $this;
169
    }
170
171
    /**
172
     * The list of imports to add: import 'key' from 'value'
173
     *
174
     * @param string $key
175
     * @param string $value
176
     * @return self
177
     */
178
    public function appendImport(string $key, string $value): self
179
    {
180
        $this->imports[$key] = $value;
181
182
        return $this;
183
    }
184
185
    public function appendComputed(Computed $computed): self
186
    {
187
        $this->computed[] = $computed;
188
189
        return $this;
190
    }
191
192
    /**
193
     * Get appended to the field variable names to handle models stored in an object field.
194
     *
195
     * @return  string
196
     */
197
    public function getFieldModelVariable(): string
198
    {
199
        return $this->fieldModelVariable;
200
    }
201
202
    /**
203
     * Set appended to the field variable names to handle models stored in an object field.
204
     *
205
     * @param  string  $fieldModelVariable  Appended to the field variable names to handle models stored in an object field.
206
     *
207
     * @return  self
208
     */
209
    public function setFieldModelVariable(string $fieldModelVariable): self
210
    {
211
        $this->fieldModelVariable = $fieldModelVariable;
212
213
        return $this;
214
    }
215
216
    /**
217
     * Converts a Datatype to a JS type
218
     *
219
     * @param Datatype|string $type
220
     * @return string
221
     */
222
    public static function mapTypeToJS($type): string
223
    {
224
        if ($type instanceof Datatype_number || $type === 'number') {
225
            return 'Number';
226
        } elseif ($type instanceof Datatype_bool || $type === 'boolean') {
227
            return 'Boolean';
228
        } elseif ($type instanceof Datatype_string || $type === 'string') {
229
            return 'String';
230
        } elseif (is_string($type)) {
231
            return 'String';
232
        } elseif ($type->getBasetype() == 'relationship') { // TODO this is crappy, comes from modelarium
233
            return 'Object';
234
        }
235
        return 'String';
236
    }
237
238
    /**
239
     * Generates the javascript code.
240
     *
241
     * @param Model $m
242
     * @param HTMLNode[] $elements
243
     * @return string
244
     */
245
    public function toScript(Model $m, array $elements)
246
    {
247
        return $this->renderer->toScript($m, $elements);
248
    }
249
250
    /**
251
     * Generates the javascript code.
252
     *
253
     * @param Model $m
254
     * @param HTMLNode[] $elements
255
     * @return string
256
     */
257
    public function toVariable(Model $m, array $elements)
258
    {
259
        return $this->renderer->toVariable($m, $elements);
260
    }
261
262
    /**
263
     * Get the value of other
264
     * @return array
265
     */
266
    public function &getOther(): array
267
    {
268
        return $this->other;
269
    }
270
271
    /**
272
     * @param Model $m
273
     * @return Prop[]
274
     */
275
    public function props(Model $m): array
276
    {
277
        $props = [];
278
        foreach ($m->getFields() as $field) {
279
            /**
280
             * @var Field $field
281
             */
282
            if ($field->getRenderable(Framework::VUE_PROP, false)) {
283
                $p = new Prop(
284
                    $field->getName(),
285
                    $field->getDatatype(),
286
                );
287
                if ($field->getRenderable(Datatype::REQUIRED, false)) {
288
                    $p->required = true;
289
                }
290
                $p->default = $field->getRenderable(RenderableParameter::DEFAULTVALUE, null);
291
                $props[] = $p;
292
            }
293
        }
294
295
        return array_merge($props, $this->extraProps);
296
    }
297
}
298