FieldsAlgorithms::getEntityName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Gui;
3
4
use Mezon\Gui\Field\InputFile;
5
use Mezon\Gui\Field\InputDate;
6
use Mezon\Gui\Field\CustomField;
7
use Mezon\Gui\FormBuilder\FormHeader;
8
use Mezon\Gui\Field\LabelField;
9
use Mezon\Gui\Field\Select;
10
use Mezon\Gui\Field\InputText;
11
use Mezon\Gui\Field\Textarea;
12
use Mezon\Gui\FormBuilder\RowsField;
13
use Mezon\Security\Security;
14
use Mezon\FieldsSet;
15
16
/**
17
 * Class FieldsAlgorithms
18
 *
19
 * @package Gui
20
 * @subpackage FieldsAlgorithms
21
 * @author Dodonov A.A.
22
 * @version v.1.0 (2019/08/08)
23
 * @copyright Copyright (c) 2019, http://aeon.su
24
 */
25
26
/**
27
 * Class constructs forms
28
 */
29
class FieldsAlgorithms extends FieldsSet
30
{
31
32
    /**
33
     * List of control objects
34
     *
35
     * @var array
36
     */
37
    private $fieldObjects = [];
38
39
    /**
40
     * Entity name
41
     *
42
     * @var string
43
     */
44
    private $entityName = '';
45
46
    /**
47
     * Session Id
48
     *
49
     * @var string
50
     */
51
    private $sessionId = '';
52
53
    /**
54
     * Supported types
55
     *
56
     * @var array
57
     */
58
    public static $typeMap = [
59
        'file' => InputFile::class,
60
        'date' => InputDate::class,
61
        'custom' => CustomField::class,
62
        'header' => FormHeader::class,
63
        'label' => LabelField::class
64
    ];
65
66
    /**
67
     * Constructor
68
     *
69
     * @param array $fields
70
     *            list of all fields
71
     * @param string $entityName
72
     *            entity name
73
     */
74
    public function __construct(array $fields = [], string $entityName = '')
75
    {
76
        parent::__construct($fields);
77
78
        $this->entityName = $entityName;
79
80
        foreach ($this->getFields() as $name => $field) {
81
            $field['name'] = $name;
82
            $field['name-prefix'] = $this->entityName;
83
84
            $this->fieldObjects[$name] = $this->initObject($field);
85
        }
86
    }
87
88
    /**
89
     * Returning date value
90
     *
91
     * @param string $value
92
     *            value to be made secure
93
     * @return string secure value
94
     */
95
    protected function getDateValue(string $value): string
96
    {
97
        if ($value == '""') {
98
            return '';
99
        } else {
100
            return date('Y-m-d', strtotime($value));
101
        }
102
    }
103
104
    /**
105
     * Returning date value
106
     *
107
     * @param array $value
108
     *            value to be made secure
109
     * @return array secure value
110
     */
111
    protected function getExternalValue(array $value): array
112
    {
113
        foreach ($value as $i => $item) {
114
            $value[$i] = intval($item);
115
        }
116
117
        return $value;
118
    }
119
120
    /**
121
     * Method returns typed value
122
     *
123
     * @param string $type
124
     *            of the field
125
     * @param string|array $value
126
     *            of the field
127
     * @param bool $storeFiles
128
     *            need the uploaded file to be stored
129
     * @return mixed secured value
130
     */
131
    public function getTypedValue(string $type, $value, bool $storeFiles = true)
132
    {
133
        $result = '';
134
135
        switch ($type) {
136
            case ('integer'):
137
                $result = intval($value);
138
                break;
139
140
            case ('string'):
141
                if (is_string($value)) {
142
                    $result = Security::getStringValue($value);
143
                }
144
                break;
145
146
            case ('file'):
147
                $result = Security::getFileValue($value, $storeFiles);
148
                break;
149
150
            case ('date'):
151
                if (is_string($value)) {
152
                    $result = $this->getDateValue($value);
153
                }
154
                break;
155
156
            case ('external'):
157
                if (is_array($value)) {
158
                    $result = $this->getExternalValue($value);
159
                }
160
                break;
161
162
            default:
163
                throw (new \Exception('Undefined type "' . $type . '"'));
164
        }
165
166
        return $result;
167
    }
168
169
    /**
170
     * Getting secure value
171
     *
172
     * @param string $field
173
     *            field name
174
     * @param mixed $value
175
     *            field value
176
     * @param bool $storeFiles
177
     *            should we store files
178
     * @return mixed secure value of the field
179
     */
180
    public function getSecureValue(string $field, $value, bool $storeFiles = true)
181
    {
182
        $this->validateFieldExistance($field);
183
184
        return $this->getTypedValue($this->fieldObjects[$field]->getType(), $value, $storeFiles);
185
    }
186
187
    /**
188
     * Getting secure values
189
     *
190
     * @param string $field
191
     *            field name
192
     * @param mixed $values
193
     *            field values
194
     * @param bool $storeFiles
195
     *            should we store files
196
     * @return mixed secure values of the field or one value
197
     */
198
    public function getSecureValues(string $field, $values, bool $storeFiles = true)
199
    {
200
        $return = [];
201
202
        if (is_array($values)) {
203
            foreach ($values as $i => $value) {
204
                $return[$i] = $this->getSecureValue($field, $value, $storeFiles);
205
            }
206
        } else {
207
            $return = $this->getSecureValue($field, $values, $storeFiles);
208
        }
209
210
        return $return;
211
    }
212
213
    /**
214
     * Method returns field wich names are started from $prefix
215
     *
216
     * @param string $prefix
217
     *            of the fields to be fetched
218
     * @param bool $storeFiles
219
     *            should we store files
220
     * @return array fetched fields
221
     */
222
    public function getValuesForPrefix(string $prefix, bool $storeFiles = true): array
223
    {
224
        $records = [];
225
226
        foreach (array_keys($this->fieldObjects) as $name) {
227
            if (isset($_POST[$prefix . $name])) {
228
                $records[$name] = $this->getSecureValues($name, $_POST[$prefix . $name], $storeFiles);
229
            }
230
        }
231
232
        return $records;
233
    }
234
235
    /**
236
     * Method removes field
237
     *
238
     * @param string $fieldName
239
     *            field name
240
     */
241
    public function removeField($fieldName): void
242
    {
243
        parent::removeField($fieldName);
244
245
        if (isset($this->fieldObjects[$fieldName])) {
246
            unset($this->fieldObjects[$fieldName]);
247
        }
248
    }
249
250
    /**
251
     * Method fetches returns custom fields for saving
252
     *
253
     * @param array $record
254
     *            record to be extended
255
     * @param string $name
256
     *            name od the field
257
     * @return array extended record
258
     */
259
    public function fetchCustomField(array &$record, string $name): array
260
    {
261
        if (! isset($this->fieldObjects[$name])) {
262
            return $record;
263
        }
264
265
        $nestedFields = $this->fieldObjects[$name]->getFields();
266
267
        foreach ($nestedFields as $name => $field) {
268
            $fieldName = $this->entityName === '' ? $name : $this->entityName . '-' . $name;
269
270
            if (isset($_POST[$fieldName])) {
271
                $record[$name] = $this->getTypedValue($field['type'], $_POST[$fieldName], true);
272
            }
273
        }
274
275
        return $record;
276
    }
277
278
    /**
279
     * Method fetches submitted field
280
     *
281
     * @param array $record
282
     *            record to be extended
283
     * @param string $name
284
     *            name od the field
285
     */
286
    public function fetchField(array &$record, string $name): void
287
    {
288
        if (isset($_POST[$this->entityName . '-' . $name])) {
289
            $record[$name] = $this->getSecureValue($name, $_POST[$this->entityName . '-' . $name]);
290
        } elseif (isset($_FILES[$this->entityName . '-' . $name])) {
291
            $record[$name] = $this->getSecureValue($name, $_FILES[$this->entityName . '-' . $name]);
292
        } elseif ($this->hasCustomFields()) {
293
            $record = $this->fetchCustomField($record, $name);
294
        }
295
    }
296
297
    /**
298
     * Factory method for creating controls
299
     *
300
     * @param array $field
301
     *            field description
302
     * @return Field|Control constructed control
303
     */
304
    protected function constructControl(array $field)
305
    {
306
        if (isset($field['items'])) {
307
            return new Select($field);
308
        } elseif (isset($field['type'])) {
309
            if (in_array($field['type'], array_keys(self::$typeMap))) {
310
                $className = self::$typeMap[$field['type']];
311
312
                $field['session-id'] = $this->sessionId;
313
314
                /** @var Control|Field $control */
315
                $control = new $className($field);
316
317
                return $control;
318
            } else {
319
                return new InputText($field);
320
            }
321
        } else {
322
            throw (new \Exception('Can not define control\'s type', - 1));
323
        }
324
    }
325
326
    /**
327
     * Method inits control
328
     *
329
     * @param array $field
330
     *            field
331
     * @return mixed control
332
     */
333
    protected function initObject(array $field)
334
    {
335
        if (isset($field['control']) && $field['control'] == 'textarea') {
336
            $control = new Textarea($field);
337
        } elseif (isset($field['type']) && $field['type'] == 'rows') {
338
            $control = new RowsField($field['type']['rows'], $this->entityName);
339
        } else {
340
            $control = $this->constructControl($field);
341
        }
342
343
        return $control;
344
    }
345
346
    /**
347
     * Method returns field object
348
     *
349
     * @param string $name
350
     *            field name
351
     * @return Field field object
352
     */
353
    public function getObject(string $name): Field
354
    {
355
        return $this->fieldObjects[$name];
356
    }
357
358
    /**
359
     * Method compiles field DOM
360
     *
361
     * @param string $name
362
     *            field name
363
     * @return string compiled HTML
364
     */
365
    public function getCompiledField(string $name): string
366
    {
367
        $control = $this->getObject($name);
368
369
        return $control->html();
370
    }
371
372
    /**
373
     * Method returns entity name
374
     *
375
     * @return string entity name
376
     */
377
    public function getEntityName(): string
378
    {
379
        return $this->entityName;
380
    }
381
382
    /**
383
     * Method sets session id
384
     *
385
     * @param string $sessionId
386
     *            new session id
387
     */
388
    public function setSessionId(string $sessionId): void
389
    {
390
        $this->sessionId = $sessionId;
391
    }
392
}
393