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
|
|
|
* @var Style[] |
16
|
|
|
*/ |
17
|
|
|
private $styles = array(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Font[] |
21
|
|
|
*/ |
22
|
|
|
private $fonts = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Fill[] |
26
|
|
|
*/ |
27
|
|
|
private $fills = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $hashes = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Styler constructor. |
36
|
|
|
*/ |
37
|
3 |
|
public function __construct() |
38
|
|
|
{ |
39
|
3 |
|
$this->addStyle(new Style()); |
40
|
3 |
|
$grey = new Style(); |
41
|
3 |
|
$this->addStyle($grey->setFillPattern('grey125')); |
42
|
3 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Add a new style, if it doesnt exists yet. |
46
|
|
|
* |
47
|
|
|
* @param Style $style |
48
|
|
|
*/ |
49
|
3 |
|
public function addStyle(Style $style) |
50
|
|
|
{ |
51
|
3 |
|
if (null === $style->getId()) { |
52
|
3 |
|
$this->register($style->getFont(), $this->fonts); |
53
|
3 |
|
$this->register($style->getFill(), $this->fills); |
54
|
3 |
|
$this->register($style, $this->styles); |
55
|
3 |
|
$style->lock(); |
56
|
3 |
|
} |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Register a new Style compoment its compoments to account |
61
|
|
|
* for reusable styles, fills, fonts, ... |
62
|
|
|
* |
63
|
|
|
* @param Component $component |
64
|
|
|
* @param array $collection |
65
|
|
|
*/ |
66
|
3 |
|
private function register(Component $component, &$collection) |
67
|
|
|
{ |
68
|
3 |
|
$hash = md5($component->asXml()); |
69
|
3 |
|
if (isset($this->hashes[$hash])) { |
70
|
3 |
|
$component->setId($this->hashes[$hash]); |
71
|
3 |
|
} else { |
72
|
3 |
|
$newId = count($collection); |
73
|
3 |
|
$component->setId($newId); |
74
|
3 |
|
$collection[$newId] = $component; |
75
|
3 |
|
$this->hashes[$hash] = $newId; |
76
|
|
|
} |
77
|
3 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return Style |
81
|
|
|
*/ |
82
|
1 |
|
public function getDefaultStyle() |
83
|
|
|
{ |
84
|
1 |
|
return $this->styles[0]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return entire xml string for the style sheet. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
2 |
|
public function getStyleSheetXml() |
93
|
|
|
{ |
94
|
2 |
|
$fontsXml = $fillsXml = $cellXfsXml = ''; |
95
|
2 |
|
foreach ($this->styles as $style) { |
96
|
2 |
|
$cellXfsXml .= $style->asXml(); |
97
|
2 |
|
} |
98
|
|
|
|
99
|
2 |
|
foreach ($this->fonts as $font) { |
100
|
2 |
|
$fontsXml .= $font->asXml(); |
101
|
2 |
|
} |
102
|
|
|
|
103
|
2 |
|
foreach ($this->fills as $fill) { |
104
|
2 |
|
$fillsXml .= $fill->asXml(); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
2 |
|
return sprintf(StyleXml::STYLE_SHEET_XML, |
108
|
2 |
|
sprintf(StyleXml::FONTS_XML, count($this->fonts), $fontsXml), |
109
|
2 |
|
sprintf(StyleXml::FILLS_XML, count($this->fills), $fillsXml), |
110
|
2 |
|
sprintf(StyleXml::CELL_XFS_XML, count($this->styles), $cellXfsXml) |
111
|
2 |
|
); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|