Completed
Push — develop ( 34668d...ecea6f )
by Tino
06:12
created

AbstractColumnRenderer::getTotalResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) by the ACP3 Developers.
5
 * See the LICENSE file at the top-level module directory for licensing details.
6
 */
7
8
namespace ACP3\Core\DataGrid\ColumnRenderer;
9
10
abstract class AbstractColumnRenderer implements ColumnRendererInterface
11
{
12
    const CELL_TYPE = 'td';
13
14
    /**
15
     * @var string
16
     */
17
    private $identifier = '';
18
    /**
19
     * @var string|null
20
     */
21
    private $primaryKey;
22
    /**
23
     * @var bool
24
     */
25
    private $useAjax = false;
26
    /**
27
     * @var int|null
28
     */
29
    private $totalResults;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getIdentifier(): string
35
    {
36
        return $this->identifier;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function setIdentifier(string $identifier): self
43
    {
44
        $this->identifier = $identifier;
45
46
        return $this;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getPrimaryKey(): string
53
    {
54
        return $this->primaryKey;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function setPrimaryKey(?string $primaryKey): self
61
    {
62
        $this->primaryKey = $primaryKey;
63
64
        return $this;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getUseAjax(): bool
71
    {
72
        return $this->useAjax;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function setUseAjax(bool $useAjax): self
79
    {
80
        $this->useAjax = $useAjax;
81
82
        return $this;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getTotalResults(): ?int
89
    {
90
        return $this->totalResults;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function setTotalResults(int $totalResults)
97
    {
98
        $this->totalResults = $totalResults;
99
100
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function fetchDataAndRenderColumn(array $column, array $dbResultRow)
107
    {
108
        return $this->render($column, $this->getValue($column, $dbResultRow));
109
    }
110
111
    /**
112
     * @param array  $column
113
     * @param string $value
114
     *
115
     * @return string
116
     */
117
    protected function render(array $column, $value = '')
118
    {
119
        if ($this->getUseAjax()) {
120
            return $this->renderAjax($column, $value);
121
        }
122
123
        $type = static::CELL_TYPE;
124
        $attribute = $this->addHtmlAttribute($column['attribute']);
125
        $class = $this->addHtmlAttribute('class', $column['class']);
126
        $style = $this->addHtmlAttribute('style', $column['style']);
127
128
        return "<{$type}{$attribute}{$class}{$style}>{$value}</{$type}>";
129
    }
130
131
    /**
132
     * @param array  $column
133
     * @param string $value
134
     *
135
     * @return string|array
136
     */
137
    private function renderAjax(array $column, string $value = '')
138
    {
139
        if (\is_array($column['attribute']) && \count($column['attribute'])) {
140
            $column['attribute']['_'] = $value;
141
142
            return $column['attribute'];
143
        }
144
145
        return $value;
146
    }
147
148
    /**
149
     * @param string|array $attributeName
150
     * @param string|null  $attributeData
151
     *
152
     * @return string
153
     */
154
    protected function addHtmlAttribute($attributeName, $attributeData = null)
155
    {
156
        if (\is_array($attributeName)) {
157
            $data = '';
158
            foreach ($attributeName as $key => $value) {
159
                $data .= $this->addHtmlAttribute($key, $value);
160
            }
161
162
            return $data;
163
        } elseif ($attributeData !== null && $attributeData !== '') {
164
            if (\in_array($attributeName, static::mandatoryAttributes())) {
165
                $attributeName = 'data-' . $attributeName;
166
            }
167
168
            return ' ' . $attributeName . '="' . $attributeData . '"';
169
        }
170
171
        return '';
172
    }
173
174
    /**
175
     * @param array $column
176
     *
177
     * @return string
178
     */
179
    protected function getFirstDbField(array $column)
180
    {
181
        $fields = $this->getDbFields($column);
182
183
        return \reset($fields);
184
    }
185
186
    /**
187
     * @param array $column
188
     * @param array $dbResultRow
189
     *
190
     * @return string
191
     */
192
    protected function getValue(array $column, array $dbResultRow)
193
    {
194
        $field = $this->getFirstDbField($column);
195
        $value = $this->getDbValueIfExists($dbResultRow, $field);
196
197
        if ($value === null) {
198
            $value = $this->getDefaultValue($column);
199
        }
200
201
        return $value;
202
    }
203
204
    /**
205
     * @param array  $dbResultRow
206
     * @param string $field
207
     *
208
     * @return mixed
209
     */
210
    protected function getDbValueIfExists(array $dbResultRow, $field)
211
    {
212
        return $dbResultRow[$field] ?? null;
213
    }
214
215
    /**
216
     * @param array $column
217
     *
218
     * @return string
219
     */
220
    protected function getDefaultValue(array $column)
221
    {
222
        if (isset($column['custom']['default_value'])) {
223
            return $column['custom']['default_value'];
224
        }
225
226
        return '';
227
    }
228
229
    /**
230
     * @param array $column
231
     *
232
     * @return array
233
     */
234
    protected function getDbFields(array $column)
235
    {
236
        return $column['fields'];
237
    }
238
239
    public static function mandatoryAttributes(): array
240
    {
241
        return [];
242
    }
243
}
244