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
| 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 |