1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\Common\Manager\Style; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Writer\Common\Entity\Style\Style; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class StyleRegistry |
9
|
|
|
* Registry for all used styles |
10
|
|
|
* |
11
|
|
|
* @package Box\Spout\Writer\Common\Manager\Style |
12
|
|
|
*/ |
13
|
|
|
class StyleRegistry |
14
|
|
|
{ |
15
|
|
|
/** @var array [SERIALIZED_STYLE] => [STYLE_ID] mapping table, keeping track of the registered styles */ |
16
|
|
|
protected $serializedStyleToStyleIdMappingTable = []; |
17
|
|
|
|
18
|
|
|
/** @var array [STYLE_ID] => [STYLE] mapping table, keeping track of the registered styles */ |
19
|
|
|
protected $styleIdToStyleMappingTable = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Style $defaultStyle |
23
|
|
|
*/ |
24
|
99 |
|
public function __construct(Style $defaultStyle) |
25
|
|
|
{ |
26
|
|
|
// This ensures that the default style is the first one to be registered |
27
|
99 |
|
$this->registerStyle($defaultStyle); |
28
|
99 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Registers the given style as a used style. |
32
|
|
|
* Duplicate styles won't be registered more than once. |
33
|
|
|
* |
34
|
|
|
* @param Style $style The style to be registered |
35
|
|
|
* @return Style The registered style, updated with an internal ID. |
36
|
|
|
*/ |
37
|
99 |
|
public function registerStyle(Style $style) |
38
|
|
|
{ |
39
|
99 |
|
$serializedStyle = $this->serialize($style); |
40
|
|
|
|
41
|
99 |
|
if (!$this->hasStyleAlreadyBeenRegistered($style)) { |
42
|
99 |
|
$nextStyleId = count($this->serializedStyleToStyleIdMappingTable); |
43
|
99 |
|
$style->setId($nextStyleId); |
44
|
|
|
|
45
|
99 |
|
$this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId; |
46
|
99 |
|
$this->styleIdToStyleMappingTable[$nextStyleId] = $style; |
47
|
|
|
} |
48
|
|
|
|
49
|
99 |
|
return $this->getStyleFromSerializedStyle($serializedStyle); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns whether the given style has already been registered. |
54
|
|
|
* |
55
|
|
|
* @param Style $style |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
99 |
|
protected function hasStyleAlreadyBeenRegistered(Style $style) |
59
|
|
|
{ |
60
|
99 |
|
$serializedStyle = $this->serialize($style); |
61
|
|
|
|
62
|
|
|
// Using isset here because it is way faster than array_key_exists... |
63
|
99 |
|
return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the registered style associated to the given serialization. |
68
|
|
|
* |
69
|
|
|
* @param string $serializedStyle The serialized style from which the actual style should be fetched from |
70
|
|
|
* @return Style |
71
|
|
|
*/ |
72
|
99 |
|
protected function getStyleFromSerializedStyle($serializedStyle) |
73
|
|
|
{ |
74
|
99 |
|
$styleId = $this->serializedStyleToStyleIdMappingTable[$serializedStyle]; |
75
|
99 |
|
return $this->styleIdToStyleMappingTable[$styleId]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return Style[] List of registered styles |
80
|
|
|
*/ |
81
|
70 |
|
public function getRegisteredStyles() |
82
|
|
|
{ |
83
|
70 |
|
return array_values($this->styleIdToStyleMappingTable); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param int $styleId |
88
|
|
|
* @return Style |
89
|
|
|
*/ |
90
|
6 |
|
public function getStyleFromStyleId($styleId) |
91
|
|
|
{ |
92
|
6 |
|
return $this->styleIdToStyleMappingTable[$styleId]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Serializes the style for future comparison with other styles. |
97
|
|
|
* The ID is excluded from the comparison, as we only care about |
98
|
|
|
* actual style properties. |
99
|
|
|
* |
100
|
|
|
* @param Style $style |
101
|
|
|
* @return string The serialized style |
102
|
|
|
*/ |
103
|
99 |
|
public function serialize(Style $style) |
104
|
|
|
{ |
105
|
|
|
// In order to be able to properly compare style, set static ID value |
106
|
99 |
|
$currentId = $style->getId(); |
107
|
99 |
|
$style->setId(0); |
108
|
|
|
|
109
|
99 |
|
$serializedStyle = serialize($style); |
110
|
|
|
|
111
|
99 |
|
$style->setId($currentId); |
112
|
|
|
|
113
|
99 |
|
return $serializedStyle; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|