DataRowWriter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 75
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUpdatedRow() 0 10 2
A __construct() 0 4 1
A getInputValues() 0 29 4
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\Utils\Utils;
9
10
/**
11
 * Reads data from the database for the row insert and update user forms.
12
 */
13
class DataRowWriter
14
{
15
    /**
16
     * @var bool|null
17
     */
18
    private bool|null $autofocus;
19
20
    /**
21
     * The constructor
22
     *
23
     * @param AppPage $page
24
     * @param DriverInterface $driver
25
     * @param Utils $utils
26
     * @param string $action
27
     * @param string $operation
28
     */
29
    public function __construct(private AppPage $page, private DriverInterface $driver,
30
        private Utils $utils, private string $action, private string $operation,
31
        private DataFieldValue $fieldValue, private DataFieldInput $fieldInput)
32
    {}
33
34
    /**
35
     * @param array $result
36
     * @param array<string, TableFieldEntity> $fields
37
     * @param array $options
38
     *
39
     * @return array
40
     */
41
    public function getUpdatedRow(array $result, array $fields, array $options): array
42
    {
43
        $textLength = $options['select']['length'];
44
        $formatted = [];
45
        foreach ($result as $fieldName => $value) {
46
            $field = $fields[$fieldName];
47
            $value = $this->driver->value($value, $field);
48
            $formatted[] = $this->page->getFieldValue($field, $textLength, $value);
49
        }
50
        return $formatted;
51
    }
52
53
    /**
54
     * @param array<TableFieldEntity> $fields
55
     * @param array|null $rowData
56
     *
57
     * @return array<FieldEditEntity>
58
     */
59
    public function getInputValues(array $fields, array|null $rowData = null): array
60
    {
61
        // From html.inc.php (function edit_form($table, $fields, $rowData, $update))
62
        $this->autofocus = $this->action !== 'save';
63
64
        $entries = [];
65
        foreach ($fields as $name => $field) {
66
            $editField = $this->fieldValue->getFieldInputValues($field, $rowData);
67
68
            if ($this->autofocus !== false) {
69
                $this->autofocus = match(true) {
70
                    $field->autoIncrement => null,
71
                    $editField->function === 'now' => null,
72
                    $editField->function === 'uuid' => null,
73
                    default => true,
74
                };
75
            }
76
77
            // Format the data fields for the user input form.
78
            $this->fieldInput->setFieldInputValues($editField, $this->autofocus);
79
80
            $entries[$name] = $editField;
81
82
            if ($this->autofocus) {
83
                $this->autofocus = false;
84
            }
85
        }
86
87
        return $entries;
88
    }
89
}
90