|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OneSheet\Style; |
|
4
|
|
|
|
|
5
|
|
|
use OneSheet\Xml\StyleXml; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Styler to keep track of registered styles. |
|
9
|
|
|
* |
|
10
|
|
|
* @package OneSheet |
|
11
|
|
|
*/ |
|
12
|
|
|
class Styler |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Holds unique styles. |
|
16
|
|
|
* |
|
17
|
|
|
* @var Style[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $styles = array(); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Holds unique fonts. |
|
23
|
|
|
* |
|
24
|
|
|
* @var Font[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $fonts = array(); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Holds uniqiue fills. |
|
30
|
|
|
* |
|
31
|
|
|
* @var Fill[] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $fills = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Holds uniqiue borders. |
|
37
|
4 |
|
* |
|
38
|
|
|
* @var Border[] |
|
39
|
4 |
|
*/ |
|
40
|
4 |
|
private $borders = array(); |
|
41
|
4 |
|
|
|
42
|
4 |
|
/** |
|
43
|
|
|
* Holds (hash => component id) mappings. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
private $hashes = array(); |
|
48
|
|
|
|
|
49
|
4 |
|
/** |
|
50
|
|
|
* Styler constructor to initialize reserved default styles. |
|
51
|
4 |
|
*/ |
|
52
|
4 |
|
public function __construct() |
|
53
|
4 |
|
{ |
|
54
|
4 |
|
$this->addStyle(new Style()); |
|
55
|
4 |
|
$grey = new Style(); |
|
56
|
4 |
|
$this->addStyle($grey->setFillPattern('grey125')); |
|
57
|
4 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Add a new style, if it doesnt exists yet. |
|
61
|
|
|
* |
|
62
|
|
|
* @param Style $style |
|
63
|
|
|
*/ |
|
64
|
|
|
public function addStyle(Style $style) |
|
65
|
|
|
{ |
|
66
|
4 |
|
if (null === $style->getId()) { |
|
67
|
|
|
$this->register($style->getFont(), $this->fonts); |
|
68
|
4 |
|
$this->register($style->getFill(), $this->fills); |
|
69
|
4 |
|
$this->register($style->getBorder(), $this->borders); |
|
70
|
4 |
|
$this->register($style, $this->styles); |
|
71
|
4 |
|
$style->lock(); |
|
72
|
4 |
|
} |
|
73
|
4 |
|
} |
|
74
|
4 |
|
|
|
75
|
4 |
|
/** |
|
76
|
|
|
* Register a new style/font/fill component to account |
|
77
|
4 |
|
* for reusable styles, fills, fonts, ... |
|
78
|
|
|
* |
|
79
|
|
|
* @param Component $component |
|
80
|
|
|
* @param array $collection |
|
81
|
|
|
*/ |
|
82
|
1 |
|
private function register(Component $component, &$collection) |
|
83
|
|
|
{ |
|
84
|
1 |
|
$hash = md5($component->asXml()); |
|
85
|
|
|
if (isset($this->hashes[$hash])) { |
|
86
|
|
|
$component->setId($this->hashes[$hash]); |
|
87
|
|
|
} else { |
|
88
|
|
|
$newId = count($collection); |
|
89
|
|
|
$component->setId($newId); |
|
90
|
|
|
$collection[$newId] = $component; |
|
91
|
|
|
$this->hashes[$hash] = $newId; |
|
92
|
2 |
|
} |
|
93
|
|
|
} |
|
94
|
2 |
|
|
|
95
|
2 |
|
/** |
|
96
|
2 |
|
* Return default style (Calibri,11). |
|
97
|
2 |
|
* |
|
98
|
|
|
* @return Style |
|
99
|
2 |
|
*/ |
|
100
|
2 |
|
public function getDefaultStyle() |
|
101
|
2 |
|
{ |
|
102
|
|
|
return $this->styles[0]; |
|
103
|
2 |
|
} |
|
104
|
2 |
|
|
|
105
|
2 |
|
/** |
|
106
|
|
|
* Return entire xml string for the style sheet. |
|
107
|
2 |
|
* |
|
108
|
2 |
|
* @return string |
|
109
|
2 |
|
*/ |
|
110
|
2 |
|
public function getStyleSheetXml() |
|
111
|
2 |
|
{ |
|
112
|
|
|
$fontsXml = $fillsXml = $bordersXml = $cellXfsXml = ''; |
|
113
|
|
|
foreach ($this->styles as $style) { |
|
114
|
|
|
$cellXfsXml .= $style->asXml(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
foreach ($this->fonts as $font) { |
|
118
|
|
|
$fontsXml .= $font->asXml(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
foreach ($this->fills as $fill) { |
|
122
|
|
|
$fillsXml .= $fill->asXml(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
foreach ($this->borders as $border) { |
|
126
|
|
|
$bordersXml .= $border->asXml(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return sprintf(StyleXml::STYLE_SHEET_XML, |
|
130
|
|
|
sprintf(StyleXml::FONTS_XML, count($this->fonts), $fontsXml), |
|
131
|
|
|
sprintf(StyleXml::FILLS_XML, count($this->fills), $fillsXml), |
|
132
|
|
|
sprintf(StyleXml::BORDERS_XML, count($this->borders), $bordersXml), |
|
133
|
|
|
sprintf(StyleXml::CELL_XFS_XML, count($this->styles), $cellXfsXml) |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|