Admin::getSelectFieldValue()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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