1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_table\core\Table; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_connect\core\AIterator; |
7
|
|
|
use kalanis\kw_connect\core\ConnectException; |
8
|
|
|
use kalanis\kw_connect\core\Interfaces\IRow; |
9
|
|
|
use kalanis\kw_table\core\Interfaces\Table\IRule; |
10
|
|
|
use kalanis\kw_table\core\TableException; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AStyle |
15
|
|
|
* @package kalanis\kw_table\core\Table |
16
|
|
|
* Columns styling |
17
|
|
|
*/ |
18
|
|
|
abstract class AStyle extends AIterator |
19
|
|
|
{ |
20
|
|
|
/** @var array<int, Internal\Attributes> */ |
21
|
|
|
protected array $styles = []; |
22
|
|
|
/** @var string|int */ |
23
|
|
|
protected $sourceName = ''; |
24
|
|
|
/** @var array<string, array<Internal\Attributes>> */ |
25
|
|
|
protected array $attributes = []; |
26
|
|
|
|
27
|
1 |
|
protected function getIterableName(): string |
28
|
|
|
{ |
29
|
1 |
|
return 'attributes'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add CSS classes: class => condition |
34
|
|
|
* @param IRule[] $classes |
35
|
|
|
*/ |
36
|
1 |
|
public function classArray(array $classes): void |
37
|
|
|
{ |
38
|
1 |
|
foreach ($classes as $class => $condition) { |
39
|
1 |
|
$this->__call('class', [$class, $condition]); |
40
|
|
|
} |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add attribute |
45
|
|
|
* @param string $function |
46
|
|
|
* @param array<string|int, string|IRule> $arguments |
47
|
|
|
* @return mixed|null|void |
48
|
|
|
*/ |
49
|
2 |
|
public function __call($function, $arguments) |
50
|
|
|
{ |
51
|
2 |
|
$this->attributes[$function][] = new Internal\Attributes( |
52
|
2 |
|
(isset($arguments[2]) && is_string($arguments[2]) ? $arguments[2] : ''), |
53
|
2 |
|
(isset($arguments[0]) && is_string($arguments[0]) ? $arguments[0] : ''), |
54
|
2 |
|
(isset($arguments[1]) && ($arguments[1] instanceof IRule) ? $arguments[1] : null) |
55
|
|
|
); |
56
|
2 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add colors from array -> color => conditions |
60
|
|
|
* @param IRule[] $data |
61
|
|
|
*/ |
62
|
1 |
|
public function colorizeArray(array $data): void |
63
|
|
|
{ |
64
|
1 |
|
foreach ($data as $colour => $condition) { |
65
|
1 |
|
$this->colorize($colour, $condition); |
66
|
|
|
} |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* When condition is met colour the cell |
71
|
|
|
* @param string $colour |
72
|
|
|
* @param IRule|null $condition |
73
|
|
|
*/ |
74
|
1 |
|
public function colorize(string $colour, ?IRule $condition): void |
75
|
|
|
{ |
76
|
1 |
|
$this->style('background-color: ' . $colour, $condition); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* When condition value equals current value then add cell style |
81
|
|
|
* @param string $style |
82
|
|
|
* @param IRule|null $condition |
83
|
|
|
* @param string|int $sourceName |
84
|
|
|
*/ |
85
|
2 |
|
public function style(string $style, ?IRule $condition, $sourceName = ''): void |
86
|
|
|
{ |
87
|
2 |
|
$this->styles[] = new Internal\Attributes($sourceName, $style, $condition); |
88
|
2 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return attribute content by obtained conditions |
92
|
|
|
* @param IRow $source |
93
|
|
|
* @throws ConnectException |
94
|
|
|
* @throws TableException |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
2 |
|
public function getCellStyle(IRow $source): string |
98
|
|
|
{ |
99
|
2 |
|
return $this->getAttributes($source) . $this->getStyleAttribute($source); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return all attributes to output |
104
|
|
|
* @param IRow $source |
105
|
|
|
* @throws ConnectException |
106
|
|
|
* @throws TableException |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
3 |
|
public function getAttributes(IRow $source): string |
110
|
|
|
{ |
111
|
3 |
|
$return = []; |
112
|
3 |
|
foreach ($this->attributes as $key => $attr) { |
113
|
1 |
|
$attribute = []; |
114
|
1 |
|
foreach ($attr as $style) { |
115
|
|
|
/** @var Internal\Attributes $style */ |
116
|
1 |
|
if (empty($style->getCondition()) || $this->isStyleApplied($source, $style)) { |
117
|
1 |
|
$attribute[] = $this->getAttributeRealValue($source, $style->getProperty()); |
118
|
|
|
} |
119
|
|
|
} |
120
|
1 |
|
$return[] = $key . '="' . $this->joinAttributeParts($attribute) . '"'; |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
return $this->joinAttributeParts($return); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns attribute value with checking if we do not want any value from row |
128
|
|
|
* @param IRow $source |
129
|
|
|
* @param string $value |
130
|
|
|
* @throws ConnectException |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
1 |
|
protected function getAttributeRealValue(IRow $source, string $value): string |
134
|
|
|
{ |
135
|
1 |
|
if (preg_match('/value\:(.*)/i', $value, $matches)) { |
136
|
1 |
|
return strval($this->getOverrideValue($source, $matches[1])); |
137
|
|
|
} else { |
138
|
1 |
|
return strval($value); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Merge attributes in array |
144
|
|
|
* @param string[] $values |
145
|
|
|
* @param string $glue |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
3 |
|
protected function joinAttributeParts(array $values, string $glue = ' '): string |
149
|
|
|
{ |
150
|
3 |
|
return implode($glue, $values); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Merge attribute Style - different for a bit different ordering |
155
|
|
|
* @param IRow $source |
156
|
|
|
* @throws ConnectException |
157
|
|
|
* @throws TableException |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
2 |
|
protected function getStyleAttribute(IRow $source) |
161
|
|
|
{ |
162
|
2 |
|
$return = []; |
163
|
2 |
|
foreach ($this->styles as $style) { |
164
|
|
|
/** @var Internal\Attributes $style */ |
165
|
2 |
|
if (empty($style->getCondition()) || $this->isStyleApplied($source, $style)) { |
166
|
2 |
|
$return[] = $style->getProperty(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
2 |
|
return (!empty($return)) ? ' style="' . implode('; ', $return) . '"' : ''; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Apply style? |
175
|
|
|
* @param IRow $source |
176
|
|
|
* @param Internal\Attributes $style |
177
|
|
|
* @throws ConnectException |
178
|
|
|
* @throws TableException |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
3 |
|
protected function isStyleApplied(IRow $source, Internal\Attributes $style): bool |
182
|
|
|
{ |
183
|
3 |
|
$property = (!empty($style->getColumnName())) ? $style->getColumnName() : $this->getSourceName(); |
184
|
3 |
|
return (bool) $style->getCondition()->validate($this->getOverrideValue($source, $property)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return string|int |
189
|
|
|
*/ |
190
|
|
|
abstract public function getSourceName(); |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param IRow $source |
194
|
|
|
* @param string|int $overrideProperty |
195
|
|
|
* @throws ConnectException |
196
|
|
|
* @return mixed |
197
|
|
|
*/ |
198
|
3 |
|
protected function getOverrideValue(IRow $source, $overrideProperty) |
199
|
|
|
{ |
200
|
3 |
|
return $source->getValue($overrideProperty); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|