|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OneSheet\Style; |
|
4
|
|
|
|
|
5
|
|
|
use OneSheet\Xml\StyleXml; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Styler |
|
9
|
|
|
* |
|
10
|
|
|
* @package OneSheet |
|
11
|
|
|
*/ |
|
12
|
|
|
class Styler |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Style[] |
|
16
|
|
|
*/ |
|
17
|
|
|
private $styles = array(); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $hashes = array(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Styler constructor. |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
2 |
|
$this->addStyle(new Style()); |
|
30
|
2 |
|
$grey = new Style(); |
|
31
|
2 |
|
$this->addStyle($grey->fill()->setPattern('grey125')->style()); |
|
32
|
2 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Register a new style if it doesnt exists. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Style $style |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function addStyle(Style $style) |
|
40
|
|
|
{ |
|
41
|
2 |
|
if (!isset($this->hashes[$style->getHash()])) { |
|
42
|
2 |
|
$this->registerStyle($style, $style->getHash()); |
|
43
|
2 |
|
} else { |
|
44
|
1 |
|
$style->setId($this->styles[$this->hashes[$style->getHash()]]->getId()); |
|
45
|
|
|
} |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Register a new style. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Style $style |
|
52
|
|
|
* @param string $styleHash |
|
53
|
|
|
*/ |
|
54
|
2 |
|
private function registerStyle(Style $style, $styleHash) |
|
55
|
|
|
{ |
|
56
|
2 |
|
$style->setId(count($this->styles))->getId(); |
|
|
|
|
|
|
57
|
2 |
|
$this->styles[$style->getId()] = $style; |
|
58
|
2 |
|
$this->hashes[$styleHash] = $style->getId(); |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return style by its id. |
|
63
|
|
|
* |
|
64
|
|
|
* @param int $id |
|
65
|
|
|
* @return Style |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getStyleById($id) |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->styles[$id]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Return entire xml string for the style sheet. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function getStyleSheetXml() |
|
78
|
|
|
{ |
|
79
|
1 |
|
$fontsXml = $fillsXml = $cellXfsXml = ''; |
|
80
|
1 |
|
foreach ($this->styles as $style) { |
|
81
|
1 |
|
$cellXfsXml .= $style->asXml(); |
|
82
|
1 |
|
$fontsXml .= $style->font()->asXml(); |
|
83
|
1 |
|
$fillsXml .= $style->fill()->asXml(); |
|
84
|
1 |
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
return sprintf(StyleXml::STYLE_SHEET_XML, |
|
87
|
1 |
|
sprintf(StyleXml::FONTS_XML, count($this->styles), $fontsXml), |
|
88
|
1 |
|
sprintf(StyleXml::FILLS_XML, count($this->styles), $fillsXml), |
|
89
|
1 |
|
sprintf(StyleXml::CELL_XFS_XML, count($this->styles), $cellXfsXml) |
|
90
|
1 |
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: