<?php
namespace Box\Spout\Writer\XLSX\Manager\Style;
use Box\Spout\Common\Entity\Style\Style;
/**
* Class StyleRegistry
* Registry for all used styles
*/
class StyleRegistry extends \Box\Spout\Writer\Common\Manager\Style\StyleRegistry
{
/**
> * @see https://msdn.microsoft.com/en-us/library/ff529597(v=office.12).aspx
* @var array
> * @var array Mapping between built-in format and the associated numFmtId
*/
> */
protected $registeredFills = [];
> protected static $builtinNumFormatToIdMapping = [
> 'General' => 0,
/**
> '0' => 1,
* @var array [STYLE_ID] => [FILL_ID] maps a style to a fill declaration
> '0.00' => 2,
*/
> '#,##0' => 3,
protected $styleIdToFillMappingTable = [];
> '#,##0.00' => 4,
> '$#,##0,\-$#,##0' => 5,
/**
> '$#,##0,[Red]\-$#,##0' => 6,
* Excel preserves two default fills with index 0 and 1
> '$#,##0.00,\-$#,##0.00' => 7,
* Since Excel is the dominant vendor - we play along here
> '$#,##0.00,[Red]\-$#,##0.00' => 8,
*
> '0%' => 9,
* @var int The fill index counter for custom fills.
> '0.00%' => 10,
*/
> '0.00E+00' => 11,
protected $fillIndex = 2;
> '# ?/?' => 12,
> '# ??/??' => 13,
/**
> 'mm-dd-yy' => 14,
* @var array
> 'd-mmm-yy' => 15,
*/
> 'd-mmm' => 16,
protected $registeredBorders = [];
> 'mmm-yy' => 17,
> 'h:mm AM/PM' => 18,
/**
> 'h:mm:ss AM/PM' => 19,
* @var array [STYLE_ID] => [BORDER_ID] maps a style to a border declaration
> 'h:mm' => 20,
*/
> 'h:mm:ss' => 21,
protected $styleIdToBorderMappingTable = [];
> 'm/d/yy h:mm' => 22,
>
/**
> '#,##0 ,(#,##0)' => 37,
* XLSX specific operations on the registered styles
> '#,##0 ,[Red](#,##0)' => 38,
*
> '#,##0.00,(#,##0.00)' => 39,
* @param Style $style
> '#,##0.00,[Red](#,##0.00)' => 40,
* @return Style
>
*/
> '_("$"* #,##0.00_),_("$"* \(#,##0.00\),_("$"* "-"??_),_(@_)' => 44,
public function registerStyle(Style $style)
> 'mm:ss' => 45,
{
> '[h]:mm:ss' => 46,
$registeredStyle = parent::registerStyle($style);
> 'mm:ss.0' => 47,
$this->registerFill($registeredStyle);
>
$this->registerBorder($registeredStyle);
> '##0.0E+0' => 48,
> '@' => 49,
return $registeredStyle;
>
}
> '[$-404]e/m/d' => 27,
> 'm/d/yy' => 30,
/**
> 't0' => 59,
* Register a fill definition
> 't0.00' => 60,
*
> 't#,##0' => 61,
* @param Style $style
> 't#,##0.00' => 62,
*/
> 't0%' => 67,
private function registerFill(Style $style)
> 't0.00%' => 68,
{
> 't# ?/?' => 69,
$styleId = $style->getId();
> 't# ??/??' => 70,
> ];
// Currently - only solid backgrounds are supported
>
// so $backgroundColor is a scalar value (RGB Color)
> /**
$backgroundColor = $style->getBackgroundColor();
> * @var array
> */
if ($backgroundColor) {
> protected $registeredFormats = [];
$isBackgroundColorRegistered = isset($this->registeredFills[$backgroundColor]);
>
> /**
// We need to track the already registered background definitions
> * @var array [STYLE_ID] => [FORMAT_ID] maps a style to a format declaration
if ($isBackgroundColorRegistered) {
> */
$registeredStyleId = $this->registeredFills[$backgroundColor];
> protected $styleIdToFormatsMappingTable = [];
$registeredFillId = $this->styleIdToFillMappingTable[$registeredStyleId];
>
$this->styleIdToFillMappingTable[$styleId] = $registeredFillId;
> /**
} else {
> * If the numFmtId is lower than 0xA4 (164 in decimal)
$this->registeredFills[$backgroundColor] = $styleId;
> * then it's a built-in number format.
$this->styleIdToFillMappingTable[$styleId] = $this->fillIndex++;
> * Since Excel is the dominant vendor - we play along here
}
> *
} else {
> * @var int The fill index counter for custom fills.
// The fillId maps a style to a fill declaration
> */
// When there is no background color definition - we default to 0
> protected $formatIndex = 164;
$this->styleIdToFillMappingTable[$styleId] = 0;
>
}
> /**
}
> if ($style->isRegistered()) {
> return $style;
/**
> }
* @param int $styleId
>
* @return int|null Fill ID associated to the given style ID
> $this->registerFormat($registeredStyle);
*/
> * Register a format definition
public function getFillIdForStyleId($styleId)
> *
{
> * @param Style $style
return (isset($this->styleIdToFillMappingTable[$styleId])) ?
> */
$this->styleIdToFillMappingTable[$styleId] :
> protected function registerFormat(Style $style)
null;
> {
}
> $styleId = $style->getId();
>
/**
> $format = $style->getFormat();
* Register a border definition
> if ($format) {
*
> $isFormatRegistered = isset($this->registeredFormats[$format]);
* @param Style $style
>
*/
> // We need to track the already registered format definitions
private function registerBorder(Style $style)
> if ($isFormatRegistered) {
{
> $registeredStyleId = $this->registeredFormats[$format];
$styleId = $style->getId();
> $registeredFormatId = $this->styleIdToFormatsMappingTable[$registeredStyleId];
> $this->styleIdToFormatsMappingTable[$styleId] = $registeredFormatId;
if ($style->shouldApplyBorder()) {
> } else {
$border = $style->getBorder();
> $this->registeredFormats[$format] = $styleId;
$serializedBorder = serialize($border);
>
> $id = self::$builtinNumFormatToIdMapping[$format] ?? $this->formatIndex++;
$isBorderAlreadyRegistered = isset($this->registeredBorders[$serializedBorder]);
> $this->styleIdToFormatsMappingTable[$styleId] = $id;
> }
if ($isBorderAlreadyRegistered) {
> } else {
$registeredStyleId = $this->registeredBorders[$serializedBorder];
> // The formatId maps a style to a format declaration
$registeredBorderId = $this->styleIdToBorderMappingTable[$registeredStyleId];
> // When there is no format definition - we default to 0 ( General )
$this->styleIdToBorderMappingTable[$styleId] = $registeredBorderId;
> $this->styleIdToFormatsMappingTable[$styleId] = 0;
} else {
> }
$this->registeredBorders[$serializedBorder] = $styleId;
> }
$this->styleIdToBorderMappingTable[$styleId] = count($this->registeredBorders);
>
}
> /**
} else {
> * @param int $styleId
// If no border should be applied - the mapping is the default border: 0
> * @return int|null Format ID associated to the given style ID
$this->styleIdToBorderMappingTable[$styleId] = 0;
> */
}
> public function getFormatIdForStyleId($styleId)
}
> {
> return $this->styleIdToFormatsMappingTable[$styleId] ?? null;
/**
> }
* @param int $styleId
>
* @return int|null Fill ID associated to the given style ID
> /**
< $serializedBorder = serialize($border);
> $serializedBorder = \serialize($border);
< $this->styleIdToBorderMappingTable[$styleId] = count($this->registeredBorders);
> $this->styleIdToBorderMappingTable[$styleId] = \count($this->registeredBorders);
{
return (isset($this->styleIdToBorderMappingTable[$styleId])) ?
$this->styleIdToBorderMappingTable[$styleId] :
null;
}
/**
* @return array
*/
public function getRegisteredFills()
{
return $this->registeredFills;
}
/**
* @return array
*/
public function getRegisteredBorders()
{
return $this->registeredBorders;
> }
}
>
}
> /**
> * @return array
> */
> public function getRegisteredFormats()
> {
> return $this->registeredFormats;