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