|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_templates\HtmlElement; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_templates\Interfaces\IAttributes; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Trait TStyles |
|
11
|
|
|
* @package kalanis\kw_templates\Template |
|
12
|
|
|
* Trait for work with cascade style sheets - direct access to styles |
|
13
|
|
|
* Extend child of AHtmlElement |
|
14
|
|
|
* @author Adam Dornak original |
|
15
|
|
|
* @author Petr Plsek refactored |
|
16
|
|
|
*/ |
|
17
|
|
|
trait TStyles |
|
18
|
|
|
{ |
|
19
|
1 |
|
public function addCss(string $name, string $value): void |
|
20
|
|
|
{ |
|
21
|
1 |
|
$attrStyle = $this->readCss(); |
|
22
|
1 |
|
$attrStyle[$name] = $value; |
|
23
|
1 |
|
$this->updateCss($attrStyle); |
|
24
|
1 |
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
public function getCss(string $name): string |
|
27
|
|
|
{ |
|
28
|
1 |
|
$attrStyle = $this->readCss(); |
|
29
|
1 |
|
return $attrStyle[$name]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
public function removeCss(string $name): void |
|
33
|
|
|
{ |
|
34
|
1 |
|
$attrStyle = $this->readCss(); |
|
35
|
1 |
|
if (isset($attrStyle[$name])) { |
|
36
|
1 |
|
unset($attrStyle[$name]); |
|
37
|
|
|
} |
|
38
|
1 |
|
$this->updateCss($attrStyle); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return array<string, string> |
|
43
|
|
|
*/ |
|
44
|
1 |
|
private function readCss(): array |
|
45
|
|
|
{ |
|
46
|
1 |
|
$attrStyle = $this->getAttribute(IAttributes::ATTR_NAME_STYLE); |
|
47
|
1 |
|
$parts = explode(IAttributes::ATTR_SEP_STYLE, strval($attrStyle)); |
|
48
|
|
|
|
|
49
|
1 |
|
$styles = []; |
|
50
|
1 |
|
foreach ($parts as $part) { |
|
51
|
1 |
|
if ($part && false !== strpos($part, IAttributes::ATTR_SET_STYLE)) { |
|
52
|
1 |
|
list($key, $val) = explode(IAttributes::ATTR_SET_STYLE, $part, 2); |
|
53
|
1 |
|
$styles[trim($key)] = trim($val); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
1 |
|
return $styles; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param array<string, string> $attrStyle |
|
61
|
|
|
*/ |
|
62
|
1 |
|
private function updateCss(array $attrStyle): void |
|
63
|
|
|
{ |
|
64
|
1 |
|
$style = ''; |
|
65
|
1 |
|
foreach ($attrStyle as $key => $val) { |
|
66
|
1 |
|
$style .= $key . IAttributes::ATTR_SET_STYLE . $val . IAttributes::ATTR_SEP_STYLE; |
|
67
|
|
|
} |
|
68
|
1 |
|
$this->setAttribute(IAttributes::ATTR_NAME_STYLE, $style); |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
abstract public function getAttribute(string $name): ?string; |
|
72
|
|
|
|
|
73
|
|
|
abstract public function setAttribute(string $name, string $value): void; |
|
74
|
|
|
} |
|
75
|
|
|
|