Completed
Pull Request — master (#763)
by Adrien
03:28
created

StyleRegistry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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