1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Eclipxe\XlsxExporter; |
6
|
|
|
|
7
|
|
|
use BadMethodCallException; |
8
|
|
|
use Eclipxe\XlsxExporter\Exceptions\InvalidPropertyNameException; |
9
|
|
|
use Eclipxe\XlsxExporter\Exceptions\InvalidPropertyValueException; |
10
|
|
|
use Eclipxe\XlsxExporter\Styles\StyleInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class to access the style specification |
14
|
|
|
* |
15
|
|
|
* @property Styles\Alignment $alignment |
16
|
|
|
* @property Styles\Border $border |
17
|
|
|
* @property Styles\Fill $fill |
18
|
|
|
* @property Styles\Font $font |
19
|
|
|
* @property Styles\Format $format |
20
|
|
|
* @property Styles\Protection $protection |
21
|
|
|
* @method Styles\Alignment getAlignment() |
22
|
|
|
* @method Styles\Border getBorder() |
23
|
|
|
* @method Styles\Fill getFill() |
24
|
|
|
* @method Styles\Font getFont() |
25
|
|
|
* @method Styles\Format getFormat() |
26
|
|
|
* @method Styles\Protection getProtection() |
27
|
|
|
* @method $this setAlignment(Styles\Alignment $value) |
28
|
|
|
* @method $this setBorder(Styles\Border $value) |
29
|
|
|
* @method $this setFill(Styles\Fill $value) |
30
|
|
|
* @method $this setFont(Styles\Font $value) |
31
|
|
|
* @method $this setFormat(Styles\Format $value) |
32
|
|
|
* @method $this setProtection(Styles\Protection $value) |
33
|
|
|
*/ |
34
|
|
|
class Style |
35
|
|
|
{ |
36
|
|
|
protected ?int $styleindex = null; |
37
|
|
|
|
38
|
|
|
/** @var array<string, Styles\StyleInterface> */ |
39
|
|
|
protected $members = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array<string, array<string, scalar>> $arrayStyles |
43
|
|
|
*/ |
44
|
47 |
|
public function __construct(array $arrayStyles = []) |
45
|
|
|
{ |
46
|
47 |
|
$this->members = [ |
47
|
47 |
|
'format' => new Styles\Format(), |
48
|
47 |
|
'font' => new Styles\Font(), |
49
|
47 |
|
'fill' => new Styles\Fill(), |
50
|
47 |
|
'alignment' => new Styles\Alignment(), |
51
|
47 |
|
'border' => new Styles\Border(), |
52
|
47 |
|
'protection' => new Styles\Protection(), |
53
|
47 |
|
]; |
54
|
47 |
|
$this->setFromArray($arrayStyles); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @return StyleInterface */ |
58
|
10 |
|
public function __get(string $name) |
59
|
|
|
{ |
60
|
10 |
|
if (! isset($this->members[$name])) { |
61
|
1 |
|
throw new InvalidPropertyNameException($name); |
62
|
|
|
} |
63
|
9 |
|
return $this->members[$name]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @param mixed $value */ |
67
|
3 |
|
public function __set(string $name, $value): void |
68
|
|
|
{ |
69
|
3 |
|
if (! isset($this->members[$name])) { |
70
|
1 |
|
throw new InvalidPropertyNameException($name); |
71
|
|
|
} |
72
|
2 |
|
$styleclass = get_class($this->members[$name]); |
73
|
2 |
|
if (! $value instanceof $styleclass) { |
74
|
1 |
|
throw new InvalidPropertyValueException("The value must be an instance of \\$styleclass", $name, $value); |
75
|
|
|
} |
76
|
|
|
/** @var StyleInterface $value */ |
77
|
1 |
|
$this->members[$name] = $value; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param mixed[] $arguments |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
9 |
|
public function __call(string $name, array $arguments) |
85
|
|
|
{ |
86
|
9 |
|
$getter = ('get' === substr($name, 0, 3)); |
87
|
9 |
|
$setter = ('set' === substr($name, 0, 3)); |
88
|
9 |
|
if (! $getter && ! $setter) { |
89
|
1 |
|
throw new BadMethodCallException("Invalid method name $name"); |
90
|
|
|
} |
91
|
8 |
|
$name = lcfirst(substr($name, 3)); |
92
|
8 |
|
if (! array_key_exists($name, $this->members)) { |
93
|
1 |
|
throw new BadMethodCallException("Invalid setter/getter name $name"); |
94
|
|
|
} |
95
|
7 |
|
if ($getter) { |
96
|
5 |
|
return $this->{$name}; |
97
|
|
|
} |
98
|
3 |
|
if (1 !== count($arguments)) { |
99
|
2 |
|
throw new BadMethodCallException('Invalid setter argument'); |
100
|
|
|
} |
101
|
1 |
|
$this->{$name} = $arguments[0]; |
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return string[] |
107
|
|
|
*/ |
108
|
1 |
|
public function getMemberNames(): array |
109
|
|
|
{ |
110
|
1 |
|
return array_keys($this->members); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set styles from an array of key-values |
115
|
|
|
* Keys: format, font, fill, alignment, border, protection |
116
|
|
|
* @param array<string, array<string, scalar>> $array |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
47 |
|
public function setFromArray(array $array): self |
120
|
|
|
{ |
121
|
47 |
|
if ([] === $array) { |
122
|
26 |
|
return $this; |
123
|
|
|
} |
124
|
27 |
|
foreach ($this->members as $key => $style) { |
125
|
27 |
|
if (array_key_exists($key, $array) && is_array($array[$key])) { |
126
|
27 |
|
$style->setValues($array[$key]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
27 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check if any of the styles has valid values |
134
|
|
|
*/ |
135
|
2 |
|
public function hasValues(): bool |
136
|
|
|
{ |
137
|
2 |
|
foreach ($this->members as $style) { |
138
|
2 |
|
if ($style->hasValues()) { |
139
|
2 |
|
return true; |
140
|
|
|
} |
141
|
|
|
} |
142
|
1 |
|
return false; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
5 |
|
public function setStyleIndex(int $index): self |
149
|
|
|
{ |
150
|
5 |
|
$this->styleindex = $index; |
151
|
5 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
3 |
|
public function getStyleIndex(): ?int |
155
|
|
|
{ |
156
|
3 |
|
return $this->styleindex; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Method to return the XML xf node |
161
|
|
|
* This method could be outside this object |
162
|
|
|
* |
163
|
|
|
* @internal |
164
|
|
|
*/ |
165
|
5 |
|
public function asXML(int $xfId = 0): string |
166
|
|
|
{ |
167
|
|
|
// all the "apply" attributes are set to not inherit the value from cellStyleXfs |
168
|
5 |
|
return '<xf' |
169
|
5 |
|
. ' numFmtId="' . $this->format->id . '"' |
170
|
5 |
|
. ' fontId="' . $this->font->getIndex() . '"' |
171
|
5 |
|
. ' fillId="' . $this->fill->getIndex() . '"' |
172
|
5 |
|
. ' borderId="' . $this->border->getIndex() . '"' |
173
|
5 |
|
. ' xfId="' . $xfId . '"' // all is based on cellStyleXfs[0] by default |
174
|
5 |
|
. ' applyNumberFormat="false"' |
175
|
5 |
|
. ' applyFont="false"' |
176
|
5 |
|
. ' applyFill="false"' |
177
|
5 |
|
. ' applyBorder="false"' |
178
|
5 |
|
. ' applyAlignment="false"' |
179
|
5 |
|
. ' applyProtection="false"' |
180
|
5 |
|
. '>' |
181
|
5 |
|
. (($this->alignment->hasValues()) ? $this->alignment->asXML() : '') |
182
|
5 |
|
. (($this->protection->hasValues()) ? $this->protection->asXML() : '') |
183
|
5 |
|
. '</xf>' |
184
|
5 |
|
; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|