ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Styles.php
Go to the documentation of this file.
1<?php
2
4
14use SimpleXMLElement;
15
17{
23 private static $theme = null;
24
25 private $styles = [];
26
27 private $cellStyles = [];
28
29 private $styleXml;
30
31 public function __construct(SimpleXMLElement $styleXml)
32 {
33 $this->styleXml = $styleXml;
34 }
35
36 public function setStyleBaseData(?Theme $theme = null, $styles = [], $cellStyles = []): void
37 {
38 self::$theme = $theme;
39 $this->styles = $styles;
40 $this->cellStyles = $cellStyles;
41 }
42
43 public static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
44 {
45 if (isset($fontStyleXml->name, $fontStyleXml->name['val'])) {
46 $fontStyle->setName((string) $fontStyleXml->name['val']);
47 }
48 if (isset($fontStyleXml->sz, $fontStyleXml->sz['val'])) {
49 $fontStyle->setSize((float) $fontStyleXml->sz['val']);
50 }
51 if (isset($fontStyleXml->b)) {
52 $fontStyle->setBold(!isset($fontStyleXml->b['val']) || self::boolean((string) $fontStyleXml->b['val']));
53 }
54 if (isset($fontStyleXml->i)) {
55 $fontStyle->setItalic(!isset($fontStyleXml->i['val']) || self::boolean((string) $fontStyleXml->i['val']));
56 }
57 if (isset($fontStyleXml->strike)) {
58 $fontStyle->setStrikethrough(
59 !isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val'])
60 );
61 }
62 $fontStyle->getColor()->setARGB(self::readColor($fontStyleXml->color));
63
64 if (isset($fontStyleXml->u) && !isset($fontStyleXml->u['val'])) {
66 } elseif (isset($fontStyleXml->u, $fontStyleXml->u['val'])) {
67 $fontStyle->setUnderline((string) $fontStyleXml->u['val']);
68 }
69
70 if (isset($fontStyleXml->vertAlign, $fontStyleXml->vertAlign['val'])) {
71 $verticalAlign = strtolower((string) $fontStyleXml->vertAlign['val']);
72 if ($verticalAlign === 'superscript') {
73 $fontStyle->setSuperscript(true);
74 } elseif ($verticalAlign === 'subscript') {
75 $fontStyle->setSubscript(true);
76 }
77 }
78 }
79
80 private static function readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLElement $numfmtStyleXml): void
81 {
82 if ($numfmtStyleXml->count() === 0) {
83 return;
84 }
85 $numfmt = $numfmtStyleXml->attributes();
86 if ($numfmt->count() > 0 && isset($numfmt['formatCode'])) {
87 $numfmtStyle->setFormatCode((string) $numfmt['formatCode']);
88 }
89 }
90
91 public static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml): void
92 {
93 if ($fillStyleXml->gradientFill) {
95 $gradientFill = $fillStyleXml->gradientFill[0];
96 if (!empty($gradientFill['type'])) {
97 $fillStyle->setFillType((string) $gradientFill['type']);
98 }
99 $fillStyle->setRotation((float) ($gradientFill['degree']));
100 $gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
101 $fillStyle->getStartColor()->setARGB(
102 self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color)
103 );
104 $fillStyle->getEndColor()->setARGB(
105 self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color)
106 );
107 } elseif ($fillStyleXml->patternFill) {
108 $defaultFillStyle = Fill::FILL_NONE;
109 if ($fillStyleXml->patternFill->fgColor) {
110 $fillStyle->getStartColor()->setARGB(self::readColor($fillStyleXml->patternFill->fgColor, true));
111 $defaultFillStyle = Fill::FILL_SOLID;
112 }
113 if ($fillStyleXml->patternFill->bgColor) {
114 $fillStyle->getEndColor()->setARGB(self::readColor($fillStyleXml->patternFill->bgColor, true));
115 $defaultFillStyle = Fill::FILL_SOLID;
116 }
117
118 $patternType = (string) $fillStyleXml->patternFill['patternType'] != ''
119 ? (string) $fillStyleXml->patternFill['patternType']
120 : $defaultFillStyle;
121
122 $fillStyle->setFillType($patternType);
123 }
124 }
125
126 public static function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
127 {
128 $diagonalUp = self::boolean((string) $borderStyleXml['diagonalUp']);
129 $diagonalDown = self::boolean((string) $borderStyleXml['diagonalDown']);
130 if (!$diagonalUp && !$diagonalDown) {
132 } elseif ($diagonalUp && !$diagonalDown) {
134 } elseif (!$diagonalUp && $diagonalDown) {
136 } else {
138 }
139
140 self::readBorder($borderStyle->getLeft(), $borderStyleXml->left);
141 self::readBorder($borderStyle->getRight(), $borderStyleXml->right);
142 self::readBorder($borderStyle->getTop(), $borderStyleXml->top);
143 self::readBorder($borderStyle->getBottom(), $borderStyleXml->bottom);
144 self::readBorder($borderStyle->getDiagonal(), $borderStyleXml->diagonal);
145 }
146
147 private static function readBorder(Border $border, SimpleXMLElement $borderXml): void
148 {
149 if (isset($borderXml['style'])) {
150 $border->setBorderStyle((string) $borderXml['style']);
151 }
152 if (isset($borderXml->color)) {
153 $border->getColor()->setARGB(self::readColor($borderXml->color));
154 }
155 }
156
157 public static function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
158 {
159 $alignment->setHorizontal((string) $alignmentXml['horizontal']);
160 $alignment->setVertical((string) $alignmentXml['vertical']);
161
162 $textRotation = 0;
163 if ((int) $alignmentXml['textRotation'] <= 90) {
164 $textRotation = (int) $alignmentXml['textRotation'];
165 } elseif ((int) $alignmentXml['textRotation'] > 90) {
166 $textRotation = 90 - (int) $alignmentXml['textRotation'];
167 }
168
169 $alignment->setTextRotation((int) $textRotation);
170 $alignment->setWrapText(self::boolean((string) $alignmentXml['wrapText']));
171 $alignment->setShrinkToFit(self::boolean((string) $alignmentXml['shrinkToFit']));
172 $alignment->setIndent(
173 (int) ((string) $alignmentXml['indent']) > 0 ? (int) ((string) $alignmentXml['indent']) : 0
174 );
175 $alignment->setReadOrder(
176 (int) ((string) $alignmentXml['readingOrder']) > 0 ? (int) ((string) $alignmentXml['readingOrder']) : 0
177 );
178 }
179
180 private function readStyle(Style $docStyle, $style): void
181 {
182 if ($style->numFmt instanceof SimpleXMLElement) {
183 self::readNumberFormat($docStyle->getNumberFormat(), $style->numFmt);
184 } else {
185 $docStyle->getNumberFormat()->setFormatCode($style->numFmt);
186 }
187
188 if (isset($style->font)) {
189 self::readFontStyle($docStyle->getFont(), $style->font);
190 }
191
192 if (isset($style->fill)) {
193 self::readFillStyle($docStyle->getFill(), $style->fill);
194 }
195
196 if (isset($style->border)) {
197 self::readBorderStyle($docStyle->getBorders(), $style->border);
198 }
199
200 if (isset($style->alignment->alignment)) {
201 self::readAlignmentStyle($docStyle->getAlignment(), $style->alignment);
202 }
203
204 // protection
205 if (isset($style->protection)) {
208 }
209
210 // top-level style settings
211 if (isset($style->quotePrefix)) {
212 $docStyle->setQuotePrefix(true);
213 }
214 }
215
216 public static function readProtectionLocked(Style $docStyle, $style): void
217 {
218 if (isset($style->protection['locked'])) {
219 if (self::boolean((string) $style->protection['locked'])) {
220 $docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
221 } else {
222 $docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
223 }
224 }
225 }
226
227 public static function readProtectionHidden(Style $docStyle, $style): void
228 {
229 if (isset($style->protection['hidden'])) {
230 if (self::boolean((string) $style->protection['hidden'])) {
231 $docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED);
232 } else {
233 $docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
234 }
235 }
236 }
237
238 public static function readColor($color, $background = false)
239 {
240 if (isset($color['rgb'])) {
241 return (string) $color['rgb'];
242 } elseif (isset($color['indexed'])) {
243 return Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
244 } elseif (isset($color['theme'])) {
245 if (self::$theme !== null) {
246 $returnColour = self::$theme->getColourByIndex((int) $color['theme']);
247 if (isset($color['tint'])) {
248 $tintAdjust = (float) $color['tint'];
249 $returnColour = Color::changeBrightness($returnColour, $tintAdjust);
250 }
251
252 return 'FF' . $returnColour;
253 }
254 }
255
256 return ($background) ? 'FFFFFFFF' : 'FF000000';
257 }
258
259 public function dxfs($readDataOnly = false)
260 {
261 $dxfs = [];
262 if (!$readDataOnly && $this->styleXml) {
263 // Conditional Styles
264 if ($this->styleXml->dxfs) {
265 foreach ($this->styleXml->dxfs->dxf as $dxf) {
266 $style = new Style(false, true);
267 $this->readStyle($style, $dxf);
268 $dxfs[] = $style;
269 }
270 }
271 // Cell Styles
272 if ($this->styleXml->cellStyles) {
273 foreach ($this->styleXml->cellStyles->cellStyle as $cellStyle) {
274 if ((int) ($cellStyle['builtinId']) == 0) {
275 if (isset($this->cellStyles[(int) ($cellStyle['xfId'])])) {
276 // Set default style
277 $style = new Style();
278 $this->readStyle($style, $this->cellStyles[(int) ($cellStyle['xfId'])]);
279
280 // normal style, currently not using it for anything
281 }
282 }
283 }
284 }
285 }
286
287 return $dxfs;
288 }
289
290 public function styles()
291 {
292 return $this->styles;
293 }
294
295 private static function getArrayItem($array, $key = 0)
296 {
297 return $array[$key] ?? null;
298 }
299}
An exception for terminatinating execution or to throw for unit testing.
static readBorder(Border $border, SimpleXMLElement $borderXml)
Definition: Styles.php:147
static readProtectionHidden(Style $docStyle, $style)
Definition: Styles.php:227
static readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml)
Definition: Styles.php:126
static readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml)
Definition: Styles.php:43
static readProtectionLocked(Style $docStyle, $style)
Definition: Styles.php:216
setStyleBaseData(?Theme $theme=null, $styles=[], $cellStyles=[])
Definition: Styles.php:36
static readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLElement $numfmtStyleXml)
Definition: Styles.php:80
static readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml)
Definition: Styles.php:157
readStyle(Style $docStyle, $style)
Definition: Styles.php:180
static readColor($color, $background=false)
Definition: Styles.php:238
__construct(SimpleXMLElement $styleXml)
Definition: Styles.php:31
static getArrayItem($array, $key=0)
Definition: Styles.php:295
setHorizontal($pValue)
Set Horizontal.
Definition: Alignment.php:200
setTextRotation($pValue)
Set TextRotation.
Definition: Alignment.php:274
setWrapText($pValue)
Set Wrap Text.
Definition: Alignment.php:317
setReadOrder($pValue)
Set read order.
Definition: Alignment.php:432
setShrinkToFit($pValue)
Set Shrink to fit.
Definition: Alignment.php:353
setDiagonalDirection($pValue)
Set DiagonalDirection.
Definition: Borders.php:372
static indexedColor($pIndex, $background=false)
Get indexed color.
Definition: Color.php:303
setFillType($pValue)
Set Fill Type.
Definition: Fill.php:189
getStartColor()
Get Start Color.
Definition: Fill.php:239
setRotation($pValue)
Set Rotation.
Definition: Fill.php:222
setBold($pValue)
Set Bold.
Definition: Font.php:299
setSize($sizeInPoints)
Set Size.
Definition: Font.php:256
setItalic($pValue)
Set Italic.
Definition: Font.php:335
setName($fontname)
Set Name.
Definition: Font.php:220
setStrikethrough($pValue)
Set Strikethrough.
Definition: Font.php:479
setSubscript(bool $pValue)
Set Subscript.
Definition: Font.php:403
setUnderline($pValue)
Set Underline.
Definition: Font.php:441
setSuperscript(bool $pValue)
Set Superscript.
Definition: Font.php:369
setQuotePrefix($pValue)
Set quote prefix.
Definition: Style.php:597
getNumberFormat()
Get Number Format.
Definition: Style.php:537
getProtection()
Get Protection.
Definition: Style.php:571
$key
Definition: croninfo.php:18
$style
Definition: example_012.php:70
$border