Admin::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\DbAdmin\Admin;
4
5
use Lagdo\DbAdmin\Driver\Utils\Utils;
6
use Lagdo\DbAdmin\Driver\DriverInterface;
7
use Lagdo\DbAdmin\Driver\Entity\TableEntity;
8
use Lagdo\DbAdmin\Driver\Entity\TableFieldEntity;
9
10
use function max;
11
use function preg_match;
12
use function strlen;
13
14
class Admin
15
{
16
    use Traits\AdminTrait;
0 ignored issues
show
Bug introduced by
The trait Lagdo\DbAdmin\Admin\Traits\AdminTrait requires the property $type which is not provided by Lagdo\DbAdmin\Admin\Admin.
Loading history...
17
    use Traits\QueryInputTrait;
0 ignored issues
show
introduced by
The trait Lagdo\DbAdmin\Admin\Traits\QueryInputTrait requires some properties which are not provided by Lagdo\DbAdmin\Admin\Admin: $name, $onUpdate, $type, $autoIncrement
Loading history...
18
    use Traits\QueryTrait;
0 ignored issues
show
introduced by
The trait Lagdo\DbAdmin\Admin\Traits\QueryTrait requires some properties which are not provided by Lagdo\DbAdmin\Admin\Admin: $trans, $null, $values, $type, $autoIncrement, $input
Loading history...
19
    use Traits\DumpTrait;
0 ignored issues
show
Bug introduced by
The trait Lagdo\DbAdmin\Admin\Traits\DumpTrait requires the property $trans which is not provided by Lagdo\DbAdmin\Admin\Admin.
Loading history...
20
21
    /**
22
     * The constructor
23
     *
24
     * @param DriverInterface $driver
25
     * @param Utils $utils
26
     */
27
    public function __construct(public DriverInterface $driver, protected Utils $utils)
28
    {}
29
30
    /**
31
     * Name in title and navigation
32
     *
33
     * @return string
34
     */
35
    public function name(): string
36
    {
37
        return '<span class="jaxon_dbadmin_name">Jaxon DbAdmin</span>';
38
    }
39
40
    /**
41
     * Get a target="_blank" attribute
42
     *
43
     * @return string
44
     */
45
    public function blankTarget(): string
46
    {
47
        return ' target="_blank" rel="noreferrer noopener"';
48
    }
49
50
    /**
51
     * Get escaped error message
52
     *
53
     * @return string
54
     */
55
    public function error(): string
56
    {
57
        return $this->utils->html($this->driver->error());
58
    }
59
60
    /**
61
     * Find unique identifier of a row
62
     *
63
     * @param array $row
64
     * @param array $indexes Result of indexes()
65
     *
66
     * @return array
67
     */
68
    public function uniqueIds(array $row, array $indexes): array
69
    {
70
        foreach ($indexes as $index) {
71
            if (preg_match('~PRIMARY|UNIQUE~', $index->type)) {
72
                $ids = [];
73
                foreach ($index->columns as $key) {
74
                    if (!isset($row[$key])) { // NULL is ambiguous
75
                        continue 2;
76
                    }
77
                    $ids[$key] = $row[$key];
78
                }
79
                return $ids;
80
            }
81
        }
82
        return [];
83
    }
84
85
    /**
86
     * Table caption used in navigation and headings
87
     *
88
     * @param TableEntity $table
89
     *
90
     * @return string
91
     */
92
    public function tableName(TableEntity $table): string
93
    {
94
        return $this->utils->html($table->name);
95
    }
96
97
    /**
98
     * Field caption used in select and edit
99
     *
100
     * @param TableFieldEntity $field Single field returned from fields()
101
     * @param int $order Order of column in select
102
     *
103
     * @return string
104
     */
105
    public function fieldName(TableFieldEntity $field, /** @scrutinizer ignore-unused */ int $order = 0): string
106
    {
107
        return '<span title="' . $this->utils->html($field->fullType) . '">' . $this->utils->html($field->name) . '</span>';
108
    }
109
110
    /**
111
     * Value printed in select table
112
     *
113
     * @param mixed $value HTML-escaped value to print
114
     * @param string $type Field type
115
     * @param mixed $original Original value before escaping
116
     *
117
     * @return string
118
     */
119
    private function getSelectFieldValue($value, string $type, $original): string
120
    {
121
        if ($value === null) {
122
            return '<i>NULL</i>';
123
        }
124
        if (preg_match('~char|binary|boolean~', $type) && !preg_match('~var~', $type)) {
125
            return "<code>$value</code>";
126
        }
127
        if (preg_match('~blob|bytea|raw|file~', $type) && !$this->utils->str->isUtf8($value)) {
128
            return '<i>' . $this->utils->trans->lang('%d byte(s)', strlen($original)) . '</i>';
129
        }
130
        if (preg_match('~json~', $type)) {
131
            return "<code>$value</code>";
132
        }
133
        if ($this->isMail($value)) {
134
            return '<a href="' . $this->utils->html("mailto:$value") . '">' . $value . '</a>';
135
        }
136
        elseif ($this->isUrl($value)) {
137
            // IE 11 and all modern browsers hide referrer
138
            return '<a href="' . $this->utils->html($value) . '"' . $this->blankTarget() . '>' . $value . '</a>';
139
        }
140
        return $value;
141
    }
142
143
    /**
144
     * Format value to use in select
145
     *
146
     * @param TableFieldEntity $field
147
     * @param mixed $value
148
     * @param int|string|null $textLength
149
     *
150
     * @return string
151
     */
152
    public function selectValue(TableFieldEntity $field, $value, $textLength): string
153
    {
154
        // if (\is_array($value)) {
155
        //     $expression = '';
156
        //     foreach ($value as $k => $v) {
157
        //         $expression .= '<tr>' . ($value != \array_values($value) ?
158
        //             '<th>' . $this->utils->html($k) :
159
        //             '') . '<td>' . $this->selectValue($field, $v, $textLength);
160
        //     }
161
        //     return "<table cellspacing='0'>$expression</table>";
162
        // }
163
        // if (!$link) {
164
        //     $link = $this->selectLink($value, $field);
165
        // }
166
        $expression = $value;
167
        if (!empty($expression)) {
168
            if (!$this->utils->str->isUtf8($expression)) {
169
                $expression = "\0"; // htmlspecialchars of binary data returns an empty string
170
            } elseif ($textLength != '' && $this->isShortable($field)) {
171
                // usage of LEFT() would reduce traffic but complicate query -
172
                // expected average speedup: .001 s VS .01 s on local network
173
                $expression = $this->utils->str->shortenUtf8($expression, max(0, +$textLength));
174
            } else {
175
                $expression = $this->utils->html($expression);
176
            }
177
        }
178
        return $this->getSelectFieldValue($expression, $field->type, $value);
179
    }
180
}
181