Differences Between: [Versions 310 and 311] [Versions 39 and 311]
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 public function __construct(Style $defaultStyle) 23 { 24 // This ensures that the default style is the first one to be registered 25 $this->registerStyle($defaultStyle); 26 } 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 public function registerStyle(Style $style) 36 { 37 $serializedStyle = $this->serialize($style); 38 39 if (!$this->hasSerializedStyleAlreadyBeenRegistered($serializedStyle)) { 40 $nextStyleId = \count($this->serializedStyleToStyleIdMappingTable); 41 $style->markAsRegistered($nextStyleId); 42 43 $this->serializedStyleToStyleIdMappingTable[$serializedStyle] = $nextStyleId; 44 $this->styleIdToStyleMappingTable[$nextStyleId] = $style; 45 } 46 47 return $this->getStyleFromSerializedStyle($serializedStyle); 48 } 49 50 /** 51 * Returns whether the serialized style has already been registered. 52 * 53 * @param string $serializedStyle The serialized style 54 * @return bool 55 */ 56 protected function hasSerializedStyleAlreadyBeenRegistered(string $serializedStyle) 57 { 58 // Using isset here because it is way faster than array_key_exists... 59 return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]); 60 } 61 62 /** 63 * Returns the registered style associated to the given serialization. 64 * 65 * @param string $serializedStyle The serialized style from which the actual style should be fetched from 66 * @return Style 67 */ 68 protected function getStyleFromSerializedStyle($serializedStyle) 69 { 70 $styleId = $this->serializedStyleToStyleIdMappingTable[$serializedStyle]; 71 72 return $this->styleIdToStyleMappingTable[$styleId]; 73 } 74 75 /** 76 * @return Style[] List of registered styles 77 */ 78 public function getRegisteredStyles() 79 { 80 return \array_values($this->styleIdToStyleMappingTable); 81 } 82 83 /** 84 * @param int $styleId 85 * @return Style 86 */ 87 public function getStyleFromStyleId($styleId) 88 { 89 return $this->styleIdToStyleMappingTable[$styleId]; 90 } 91 92 /** 93 * Serializes the style for future comparison with other styles. 94 * The ID is excluded from the comparison, as we only care about 95 * actual style properties. 96 * 97 * @param Style $style 98 * @return string The serialized style 99 */ 100 public function serialize(Style $style) 101 { 102 // In order to be able to properly compare style, set static ID value and reset registration 103 $currentId = $style->getId(); 104 $style->unmarkAsRegistered(); 105 106 $serializedStyle = \serialize($style); 107 108 $style->markAsRegistered($currentId); 109 110 return $serializedStyle; 111 } 112 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body