DataRowReader::getInputValues()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 12
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Db\Page\Dml;
4
5
use Lagdo\DbAdmin\Db\Page\AppPage;
6
use Lagdo\DbAdmin\Driver\DriverInterface;
7
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
8
use Lagdo\DbAdmin\Driver\Entity\UserTypeEntity;
9
use Lagdo\DbAdmin\Driver\Utils\Utils;
10
11
use function count;
12
use function implode;
13
use function is_array;
14
use function is_string;
15
use function json_decode;
16
use function preg_match;
17
use function substr;
18
19
/**
20
 * Reads data from the user inputs for data row insert and update.
21
 */
22
class DataRowReader
23
{
24
    /**
25
     * @var array<UserTypeEntity>
26
     */
27
    private array $userTypes;
28
29
    /**
30
     * The constructor
31
     *
32
     * @param AppPage $page
33
     * @param DriverInterface $driver
34
     * @param Utils $utils
35
     */
36
    public function __construct(private AppPage $page,
37
        private DriverInterface $driver, private Utils $utils)
38
    {
39
        $this->userTypes = $this->driver->userTypes(true);
40
    }
41
42
    /**
43
     * Get the user input values for data save on insert and update
44
     * Function process_input() in html.inc.php.
45
     *
46
     * @param TableFieldEntity $field
47
     * @param array $values
48
     *
49
     * @return mixed
50
     */
51
    private function getInputValue(TableFieldEntity $field, array $values): mixed
52
    {
53
        if ($field->isDisabled()) {
54
            return false;
55
        }
56
57
        $fieldId = $this->driver->bracketEscape($field->name);
58
        $value = $values['fields'][$fieldId];
59
60
        $userType = $this->userTypes[$field->type] ?? null;
61
        $enumValues = $userType?->enums ?? [];
62
        if ($field->type === "enum" || count($enumValues) > 0) {
63
            $value = $value[0];
64
            if ($value === "orig") {
65
                return false;
66
            }
67
            if ($value === "null") {
68
                return "NULL";
69
            }
70
71
            $value = substr($value, 4); // 4 - strlen("val-")
72
        }
73
74
        if ($field->autoIncrement && $value === '') {
75
            return null;
76
        }
77
78
        // The function is not provided for auto-incremented fields or enums.
79
        $function = $values['function'][$fieldId] ?? '';
80
        if ($function === 'orig') {
81
            return preg_match('~^CURRENT_TIMESTAMP~i', $field->onUpdate) ?
82
                $this->driver->escapeId($field->name) : false;
83
        }
84
85
        if ($function === 'NULL') {
86
            return 'NULL';
87
        }
88
89
        if ($field->type === 'set') {
90
            $value = implode(',', (array)$value);
91
        }
92
93
        if ($function === 'json') {
94
            $function = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $function is dead and can be removed.
Loading history...
95
            $value = json_decode($value, true);
96
            //! report errors
97
            return is_array($value) ? $value : false;
98
        }
99
100
        if ($this->utils->isBlob($field) && $this->utils->iniBool('file_uploads')) {
101
            $file = $this->page->getFileContents("fields-$fieldId");
102
            //! report errors
103
            return is_string($file) ? $this->driver->quoteBinary($file) : false;
104
        }
105
106
        return $this->page->getUnconvertedFieldValue($field, $value, $function);
107
    }
108
109
    /**
110
     * @param array<TableFieldEntity> $fields The table fields
111
     * @param array $inputs The user form inputs
112
     *
113
     * @return array
114
     */
115
    public function getInputValues(array $fields, array $inputs): array
116
    {
117
        // From edit.inc.php
118
        $values = [];
119
        foreach ($fields as $name => $field) {
120
            $value = $this->getInputValue($field, $inputs);
121
            if ($value !== false && $value !== null) {
122
                $values[$this->driver->escapeId($name)] = $value;
123
            }
124
        }
125
126
        return $values;
127
    }
128
}
129