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 unique fills. |
30
|
|
|
* |
31
|
|
|
* @var Fill[] |
32
|
|
|
*/ |
33
|
|
|
private $fills = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Holds unique borders. |
37
|
|
|
* |
38
|
|
|
* @var Border[] |
39
|
|
|
*/ |
40
|
|
|
private $borders = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Holds (hash => component id) mappings. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $hashes = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Styler constructor to initialize reserved default styles. |
51
|
|
|
*/ |
52
|
4 |
|
public function __construct() |
53
|
|
|
{ |
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
|
4 |
|
public function addStyle(Style $style) |
65
|
|
|
{ |
66
|
4 |
|
if (null === $style->getId()) { |
67
|
4 |
|
$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
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Register a new style/font/fill component to account |
77
|
|
|
* for reusable styles, fills, fonts, ... |
78
|
|
|
* |
79
|
|
|
* @param Component $component |
80
|
|
|
* @param array $collection |
81
|
|
|
*/ |
82
|
4 |
|
private function register(Component $component, &$collection) |
83
|
|
|
{ |
84
|
4 |
|
$hash = md5($component->asXml()); |
85
|
4 |
|
if (isset($this->hashes[$hash])) { |
86
|
4 |
|
$component->setId($this->hashes[$hash]); |
87
|
4 |
|
} else { |
88
|
4 |
|
$newId = count($collection); |
89
|
4 |
|
$component->setId($newId); |
90
|
4 |
|
$collection[$newId] = $component; |
91
|
4 |
|
$this->hashes[$hash] = $newId; |
92
|
|
|
} |
93
|
4 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return default style (Calibri,11). |
97
|
|
|
* |
98
|
|
|
* @return Style |
99
|
|
|
*/ |
100
|
1 |
|
public function getDefaultStyle() |
101
|
|
|
{ |
102
|
1 |
|
return $this->styles[0]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return entire xml string for the style sheet. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
2 |
|
public function getStyleSheetXml() |
111
|
|
|
{ |
112
|
2 |
|
$fontsXml = $this->getComponentXml($this->fonts); |
113
|
2 |
|
$fillsXml = $this->getComponentXml($this->fills); |
114
|
2 |
|
$bordersXml = $this->getComponentXml($this->borders); |
115
|
2 |
|
$cellXfsXml = $this->getComponentXml($this->styles); |
116
|
|
|
|
117
|
2 |
|
return sprintf(StyleXml::STYLE_SHEET_XML, |
118
|
2 |
|
sprintf(StyleXml::FONTS_XML, count($this->fonts), $fontsXml), |
119
|
2 |
|
sprintf(StyleXml::FILLS_XML, count($this->fills), $fillsXml), |
120
|
2 |
|
sprintf(StyleXml::BORDERS_XML, count($this->borders), $bordersXml), |
121
|
2 |
|
sprintf(StyleXml::CELL_XFS_XML, count($this->styles), $cellXfsXml) |
122
|
2 |
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return fonts, fills, borders, xfs xml strings. |
127
|
|
|
* |
128
|
|
|
* @param Component[] $components |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
2 |
|
private function getComponentXml(array &$components) |
132
|
|
|
{ |
133
|
2 |
|
$componentXml = ''; |
134
|
2 |
|
foreach ($components as $component) { |
135
|
2 |
|
$componentXml .= $component->asXml(); |
136
|
2 |
|
} |
137
|
2 |
|
return $componentXml; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|