Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401]
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->hasStyleAlreadyBeenRegistered($style)) { 40 $nextStyleId = count($this->serializedStyleToStyleIdMappingTable); 41 $style->setId($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 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 // Using isset here because it is way faster than array_key_exists... 61 return isset($this->serializedStyleToStyleIdMappingTable[$serializedStyle]); 62 } 63 64 /** 65 * Returns the registered style associated to the given serialization. 66 * 67 * @param string $serializedStyle The serialized style from which the actual style should be fetched from 68 * @return Style 69 */ 70 protected function getStyleFromSerializedStyle($serializedStyle) 71 { 72 $styleId = $this->serializedStyleToStyleIdMappingTable[$serializedStyle]; 73 74 return $this->styleIdToStyleMappingTable[$styleId]; 75 } 76 77 /** 78 * @return Style[] List of registered styles 79 */ 80 public function getRegisteredStyles() 81 { 82 return array_values($this->styleIdToStyleMappingTable); 83 } 84 85 /** 86 * @param int $styleId 87 * @return Style 88 */ 89 public function getStyleFromStyleId($styleId) 90 { 91 return $this->styleIdToStyleMappingTable[$styleId]; 92 } 93 94 /** 95 * Serializes the style for future comparison with other styles. 96 * The ID is excluded from the comparison, as we only care about 97 * actual style properties. 98 * 99 * @param Style $style 100 * @return string The serialized style 101 */ 102 public function serialize(Style $style) 103 { 104 // In order to be able to properly compare style, set static ID value 105 $currentId = $style->getId(); 106 $style->setId(0); 107 108 $serializedStyle = serialize($style); 109 110 $style->setId($currentId); 111 112 return $serializedStyle; 113 } 114 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body