QueryTrait::editFunctions()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Admin\Traits;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
6
7
use function preg_match;
8
9
trait QueryTrait
10
{
11
    /**
12
     * @param TableFieldEntity $field
13
     * @param array $values First entries
14
     * @param bool $update
15
     *
16
     * @return string[]
17
     */
18
    private function getEditFunctionNames(TableFieldEntity $field, array $values, bool $update): array
19
    {
20
        $names = $values;
21
        $dbFunctions = [$this->driver->insertFunctions(), $this->driver->editFunctions()];
22
        foreach ($dbFunctions as $key => $functions) {
23
            if (!$key || (!isset($this->utils->input->values['call']) && $update)) { // relative functions
24
                foreach ($functions as $pattern => $value) {
25
                    if (!$pattern || preg_match("~$pattern~", $field->type)) {
26
                        $names[] = $value;
27
                    }
28
                }
29
            }
30
            if ($key && !preg_match('~set|blob|bytea|raw|file|bool~', $field->type)) {
31
                $names[] = 'SQL';
32
            }
33
        }
34
        return $names;
35
    }
36
37
    /**
38
     * Functions displayed in edit form
39
     *
40
     * @param TableFieldEntity $field Single field from fields()
41
     *
42
     * @return array
43
     */
44
    public function editFunctions(TableFieldEntity $field): array
45
    {
46
        $update = isset($this->utils->input->values['select']); // || $this->where([]);
47
        if ($field->autoIncrement && !$update) {
48
            return [$this->utils->trans->lang('Auto Increment')];
49
        }
50
51
        $names = $field->null ? ['NULL', ''] : [''];
52
        return $this->getEditFunctionNames($field, $names, $update);
53
    }
54
}
55