Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace OpenSpout\Writer\Common\Manager\Style;
6
 
7
use OpenSpout\Common\Entity\Style\Style;
8
 
9
/**
10
 * @internal
11
 */
12
abstract class AbstractStyleRegistry
13
{
14
    /** @var array<string, int> [SERIALIZED_STYLE] => [STYLE_ID] mapping table, keeping track of the registered styles */
15
    private array $serializedStyleToStyleIdMappingTable = [];
16
 
17
    /** @var array<int, Style> [STYLE_ID] => [STYLE] mapping table, keeping track of the registered styles */
18
    private array $styleIdToStyleMappingTable = [];
19
 
20
    public function __construct(Style $defaultStyle)
21
    {
22
        // This ensures that the default style is the first one to be registered
23
        $this->registerStyle($defaultStyle);
24
    }
25
 
26
    /**
27
     * Registers the given style as a used style.
28
     * Duplicate styles won't be registered more than once.
29
     *
30
     * @param Style $style The style to be registered
31
     *
32
     * @return Style the registered style, updated with an internal ID
33
     */
34
    public function registerStyle(Style $style): Style
35
    {
36
        $serializedStyle = $this->serialize($style);
37
 
38
        if (!$this->hasSerializedStyleAlreadyBeenRegistered($serializedStyle)) {
39
            $nextStyleId = \count($this->serializedStyleToStyleIdMappingTable);
40
            $style->markAsRegistered($nextStyleId);
41
 
42
            $this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId;
43
            $this->styleIdToStyleMappingTable[$nextStyleId] = $style;
44
        }
45
 
46
        return $this->getStyleFromSerializedStyle($serializedStyle);
47
    }
48
 
49
    /**
50
     * @return Style[] List of registered styles
51
     */
52
    final public function getRegisteredStyles(): array
53
    {
54
        return array_values($this->styleIdToStyleMappingTable);
55
    }
56
 
57
    final public function getStyleFromStyleId(int $styleId): Style
58
    {
59
        return $this->styleIdToStyleMappingTable[$styleId];
60
    }
61
 
62
    /**
63
     * Serializes the style for future comparison with other styles.
64
     * The ID is excluded from the comparison, as we only care about
65
     * actual style properties.
66
     *
67
     * @return string The serialized style
68
     */
69
    final public function serialize(Style $style): string
70
    {
71
        return serialize($style);
72
    }
73
 
74
    /**
75
     * Returns whether the serialized style has already been registered.
76
     *
77
     * @param string $serializedStyle The serialized style
78
     */
79
    private function hasSerializedStyleAlreadyBeenRegistered(string $serializedStyle): bool
80
    {
81
        // Using isset here because it is way faster than array_key_exists...
82
        return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]);
83
    }
84
 
85
    /**
86
     * Returns the registered style associated to the given serialization.
87
     *
88
     * @param string $serializedStyle The serialized style from which the actual style should be fetched from
89
     */
90
    private function getStyleFromSerializedStyle(string $serializedStyle): Style
91
    {
92
        $styleId = $this->serializedStyleToStyleIdMappingTable[$serializedStyle];
93
 
94
        return $this->styleIdToStyleMappingTable[$styleId];
95
    }
96
}