PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.11.10
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.11.10
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
visualizer / vendor / openspout / openspout / src / Writer / Common / Manager / Style / StyleManager.php

StyleManager.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.11.10, at vendor/openspout/openspout/src/Writer/Common/Manager/Style/StyleManager.php

83 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace OpenSpout\Writer\Common\Manager\Style;
4
5 use OpenSpout\Common\Entity\Cell;
6 use OpenSpout\Common\Entity\Style\Style;
7
8 /**
9 * Manages styles to be applied to a cell.
10 */
11 class StyleManager implements StyleManagerInterface
12 {
13 /** @var StyleRegistry Registry for all used styles */
14 protected $styleRegistry;
15
16 public function __construct(StyleRegistry $styleRegistry)
17 {
18 $this->styleRegistry = $styleRegistry;
19 }
20
21 /**
22 * Registers the given style as a used style.
23 * Duplicate styles won't be registered more than once.
24 *
25 * @param Style $style The style to be registered
26 *
27 * @return Style the registered style, updated with an internal ID
28 */
29 public function registerStyle($style)
30 {
31 return $this->styleRegistry->registerStyle($style);
32 }
33
34 /**
35 * Apply additional styles if the given row needs it.
36 * Typically, set "wrap text" if a cell contains a new line.
37 *
38 * @return PossiblyUpdatedStyle The eventually updated style
39 */
40 public function applyExtraStylesIfNeeded(Cell $cell): PossiblyUpdatedStyle
41 {
42 return $this->applyWrapTextIfCellContainsNewLine($cell);
43 }
44
45 /**
46 * Returns the default style.
47 *
48 * @return Style Default style
49 */
50 protected function getDefaultStyle()
51 {
52 // By construction, the default style has ID 0
53 return $this->styleRegistry->getRegisteredStyles()[0];
54 }
55
56 /**
57 * Set the "wrap text" option if a cell of the given row contains a new line.
58 *
59 * @NOTE: There is a bug on the Mac version of Excel (2011 and below) where new lines
60 * are ignored even when the "wrap text" option is set. This only occurs with
61 * inline strings (shared strings do work fine).
62 * A workaround would be to encode "\n" as "_x000D_" but it does not work
63 * on the Windows version of Excel...
64 *
65 * @param Cell $cell The cell the style should be applied to
66 *
67 * @return PossiblyUpdatedStyle The eventually updated style
68 */
69 protected function applyWrapTextIfCellContainsNewLine(Cell $cell): PossiblyUpdatedStyle
70 {
71 $cellStyle = $cell->getStyle();
72
73 // if the "wrap text" option is already set, no-op
74 if (!$cellStyle->hasSetWrapText() && $cell->isString() && false !== strpos($cell->getValue(), "\n")) {
75 $cellStyle->setShouldWrapText();
76
77 return new PossiblyUpdatedStyle($cellStyle, true);
78 }
79
80 return new PossiblyUpdatedStyle($cellStyle, false);
81 }
82 }
83