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