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