1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Eclipxe\XlsxExporter\Styles; |
6
|
|
|
|
7
|
|
|
use Eclipxe\XlsxExporter\Exceptions\InvalidPropertyNameException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Abstract implementation of the StyleInterface for internal use |
11
|
|
|
*/ |
12
|
|
|
abstract class AbstractStyle implements StyleInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Storage of the properties contents |
16
|
|
|
* @var array<string, scalar|null> |
17
|
|
|
*/ |
18
|
|
|
protected array $data = []; |
19
|
|
|
|
20
|
|
|
/** @var int|null Index property */ |
21
|
|
|
protected ?int $index = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get an array of property names |
25
|
|
|
* |
26
|
|
|
* @return string[] |
27
|
|
|
*/ |
28
|
|
|
abstract protected function properties(): array; |
29
|
|
|
|
30
|
|
|
abstract public function asXML(): string; |
31
|
|
|
|
32
|
29 |
|
public function setValues(array $values): void |
33
|
|
|
{ |
34
|
29 |
|
foreach ($values as $key => $value) { |
35
|
29 |
|
$this->{$key} = $value; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getValues(): array |
40
|
|
|
{ |
41
|
|
|
$array = []; |
42
|
|
|
foreach ($this->properties() as $key) { |
43
|
|
|
$array[$key] = $this->{$key}; |
44
|
|
|
} |
45
|
|
|
return $array; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** @return scalar|null */ |
49
|
20 |
|
public function __get(string $name) |
50
|
|
|
{ |
51
|
20 |
|
if (! in_array($name, $this->properties())) { |
52
|
|
|
throw new InvalidPropertyNameException($name); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// use getter if method exists |
56
|
20 |
|
$method = 'get' . ucfirst($name); |
57
|
20 |
|
if (method_exists($this, $method)) { |
58
|
6 |
|
return $this->$method(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// return data value if method exists |
62
|
18 |
|
return $this->data[$name] ?? null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @param scalar|null $value */ |
66
|
36 |
|
public function __set(string $name, $value): void |
67
|
|
|
{ |
68
|
36 |
|
if (! in_array($name, $this->properties())) { |
69
|
|
|
throw new InvalidPropertyNameException($name); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// cast value if method exists |
73
|
36 |
|
$method = 'cast' . ucfirst($name); |
74
|
36 |
|
if (method_exists($this, $method)) { |
75
|
36 |
|
$value = $this->$method($value); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// use setter if method exists, otherwise just set the value on $data |
79
|
36 |
|
$method = 'set' . ucfirst($name); |
80
|
36 |
|
if (method_exists($this, $method)) { |
81
|
|
|
$this->$method($value); |
82
|
|
|
} else { |
83
|
36 |
|
$this->data[$name] = $value; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function __isset(string $name) |
88
|
|
|
{ |
89
|
|
|
return isset($this->data[$name]); |
90
|
|
|
} |
91
|
|
|
|
92
|
7 |
|
public function hasValues(): bool |
93
|
|
|
{ |
94
|
7 |
|
return ([] !== $this->data); |
95
|
|
|
} |
96
|
|
|
|
97
|
5 |
|
public function getIndex(): ?int |
98
|
|
|
{ |
99
|
5 |
|
return $this->index; |
100
|
|
|
} |
101
|
|
|
|
102
|
4 |
|
public function setIndex(int $index): void |
103
|
|
|
{ |
104
|
4 |
|
$this->index = $index; |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
public function getHash(): string |
108
|
|
|
{ |
109
|
4 |
|
return sha1(get_class($this) . '::' . print_r($this->data, true)); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|