Subiendo proyecto completo sin restricciones de git ignore

This commit is contained in:
Jose Sanchez
2023-08-17 11:44:02 -04:00
parent a0d4f5ba3b
commit 20f1c60600
19921 changed files with 2509159 additions and 45 deletions

View File

@@ -0,0 +1,545 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
class Alignment extends Supervisor
{
// Horizontal alignment styles
const HORIZONTAL_GENERAL = 'general';
const HORIZONTAL_LEFT = 'left';
const HORIZONTAL_RIGHT = 'right';
const HORIZONTAL_CENTER = 'center';
const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
const HORIZONTAL_JUSTIFY = 'justify';
const HORIZONTAL_FILL = 'fill';
const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only
private const HORIZONTAL_CENTER_CONTINUOUS_LC = 'centercontinuous';
// Mapping for horizontal alignment
const HORIZONTAL_ALIGNMENT_FOR_XLSX = [
self::HORIZONTAL_LEFT => self::HORIZONTAL_LEFT,
self::HORIZONTAL_RIGHT => self::HORIZONTAL_RIGHT,
self::HORIZONTAL_CENTER => self::HORIZONTAL_CENTER,
self::HORIZONTAL_CENTER_CONTINUOUS => self::HORIZONTAL_CENTER_CONTINUOUS,
self::HORIZONTAL_JUSTIFY => self::HORIZONTAL_JUSTIFY,
self::HORIZONTAL_FILL => self::HORIZONTAL_FILL,
self::HORIZONTAL_DISTRIBUTED => self::HORIZONTAL_DISTRIBUTED,
];
// Mapping for horizontal alignment CSS
const HORIZONTAL_ALIGNMENT_FOR_HTML = [
self::HORIZONTAL_LEFT => self::HORIZONTAL_LEFT,
self::HORIZONTAL_RIGHT => self::HORIZONTAL_RIGHT,
self::HORIZONTAL_CENTER => self::HORIZONTAL_CENTER,
self::HORIZONTAL_CENTER_CONTINUOUS => self::HORIZONTAL_CENTER,
self::HORIZONTAL_JUSTIFY => self::HORIZONTAL_JUSTIFY,
//self::HORIZONTAL_FILL => self::HORIZONTAL_FILL, // no reasonable equivalent for fill
self::HORIZONTAL_DISTRIBUTED => self::HORIZONTAL_JUSTIFY,
];
// Vertical alignment styles
const VERTICAL_BOTTOM = 'bottom';
const VERTICAL_TOP = 'top';
const VERTICAL_CENTER = 'center';
const VERTICAL_JUSTIFY = 'justify';
const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only
// Vertical alignment CSS
private const VERTICAL_BASELINE = 'baseline';
private const VERTICAL_MIDDLE = 'middle';
private const VERTICAL_SUB = 'sub';
private const VERTICAL_SUPER = 'super';
private const VERTICAL_TEXT_BOTTOM = 'text-bottom';
private const VERTICAL_TEXT_TOP = 'text-top';
// Mapping for vertical alignment
const VERTICAL_ALIGNMENT_FOR_XLSX = [
self::VERTICAL_BOTTOM => self::VERTICAL_BOTTOM,
self::VERTICAL_TOP => self::VERTICAL_TOP,
self::VERTICAL_CENTER => self::VERTICAL_CENTER,
self::VERTICAL_JUSTIFY => self::VERTICAL_JUSTIFY,
self::VERTICAL_DISTRIBUTED => self::VERTICAL_DISTRIBUTED,
// css settings that arent't in sync with Excel
self::VERTICAL_BASELINE => self::VERTICAL_BOTTOM,
self::VERTICAL_MIDDLE => self::VERTICAL_CENTER,
self::VERTICAL_SUB => self::VERTICAL_BOTTOM,
self::VERTICAL_SUPER => self::VERTICAL_TOP,
self::VERTICAL_TEXT_BOTTOM => self::VERTICAL_BOTTOM,
self::VERTICAL_TEXT_TOP => self::VERTICAL_TOP,
];
// Mapping for vertical alignment for Html
const VERTICAL_ALIGNMENT_FOR_HTML = [
self::VERTICAL_BOTTOM => self::VERTICAL_BOTTOM,
self::VERTICAL_TOP => self::VERTICAL_TOP,
self::VERTICAL_CENTER => self::VERTICAL_MIDDLE,
self::VERTICAL_JUSTIFY => self::VERTICAL_MIDDLE,
self::VERTICAL_DISTRIBUTED => self::VERTICAL_MIDDLE,
// css settings that arent't in sync with Excel
self::VERTICAL_BASELINE => self::VERTICAL_BASELINE,
self::VERTICAL_MIDDLE => self::VERTICAL_MIDDLE,
self::VERTICAL_SUB => self::VERTICAL_SUB,
self::VERTICAL_SUPER => self::VERTICAL_SUPER,
self::VERTICAL_TEXT_BOTTOM => self::VERTICAL_TEXT_BOTTOM,
self::VERTICAL_TEXT_TOP => self::VERTICAL_TEXT_TOP,
];
// Read order
const READORDER_CONTEXT = 0;
const READORDER_LTR = 1;
const READORDER_RTL = 2;
// Special value for Text Rotation
const TEXTROTATION_STACK_EXCEL = 255;
const TEXTROTATION_STACK_PHPSPREADSHEET = -165; // 90 - 255
/**
* Horizontal alignment.
*
* @var null|string
*/
protected $horizontal = self::HORIZONTAL_GENERAL;
/**
* Vertical alignment.
*
* @var null|string
*/
protected $vertical = self::VERTICAL_BOTTOM;
/**
* Text rotation.
*
* @var null|int
*/
protected $textRotation = 0;
/**
* Wrap text.
*
* @var bool
*/
protected $wrapText = false;
/**
* Shrink to fit.
*
* @var bool
*/
protected $shrinkToFit = false;
/**
* Indent - only possible with horizontal alignment left and right.
*
* @var int
*/
protected $indent = 0;
/**
* Read order.
*
* @var int
*/
protected $readOrder = 0;
/**
* Create a new Alignment.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
if ($isConditional) {
$this->horizontal = null;
$this->vertical = null;
$this->textRotation = null;
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Alignment
*/
public function getSharedComponent()
{
/** @var Style */
$parent = $this->parent;
return $parent->getSharedComponent()->getAlignment();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['alignment' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
* [
* 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
* 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
* 'textRotation' => 0,
* 'wrapText' => TRUE
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())
->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['horizontal'])) {
$this->setHorizontal($styleArray['horizontal']);
}
if (isset($styleArray['vertical'])) {
$this->setVertical($styleArray['vertical']);
}
if (isset($styleArray['textRotation'])) {
$this->setTextRotation($styleArray['textRotation']);
}
if (isset($styleArray['wrapText'])) {
$this->setWrapText($styleArray['wrapText']);
}
if (isset($styleArray['shrinkToFit'])) {
$this->setShrinkToFit($styleArray['shrinkToFit']);
}
if (isset($styleArray['indent'])) {
$this->setIndent($styleArray['indent']);
}
if (isset($styleArray['readOrder'])) {
$this->setReadOrder($styleArray['readOrder']);
}
}
return $this;
}
/**
* Get Horizontal.
*
* @return null|string
*/
public function getHorizontal()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHorizontal();
}
return $this->horizontal;
}
/**
* Set Horizontal.
*
* @param string $horizontalAlignment see self::HORIZONTAL_*
*
* @return $this
*/
public function setHorizontal(string $horizontalAlignment)
{
$horizontalAlignment = strtolower($horizontalAlignment);
if ($horizontalAlignment === self::HORIZONTAL_CENTER_CONTINUOUS_LC) {
$horizontalAlignment = self::HORIZONTAL_CENTER_CONTINUOUS;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['horizontal' => $horizontalAlignment]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->horizontal = $horizontalAlignment;
}
return $this;
}
/**
* Get Vertical.
*
* @return null|string
*/
public function getVertical()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getVertical();
}
return $this->vertical;
}
/**
* Set Vertical.
*
* @param string $verticalAlignment see self::VERTICAL_*
*
* @return $this
*/
public function setVertical($verticalAlignment)
{
$verticalAlignment = strtolower($verticalAlignment);
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['vertical' => $verticalAlignment]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->vertical = $verticalAlignment;
}
return $this;
}
/**
* Get TextRotation.
*
* @return null|int
*/
public function getTextRotation()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getTextRotation();
}
return $this->textRotation;
}
/**
* Set TextRotation.
*
* @param int $angleInDegrees
*
* @return $this
*/
public function setTextRotation($angleInDegrees)
{
// Excel2007 value 255 => PhpSpreadsheet value -165
if ($angleInDegrees == self::TEXTROTATION_STACK_EXCEL) {
$angleInDegrees = self::TEXTROTATION_STACK_PHPSPREADSHEET;
}
// Set rotation
if (($angleInDegrees >= -90 && $angleInDegrees <= 90) || $angleInDegrees == self::TEXTROTATION_STACK_PHPSPREADSHEET) {
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['textRotation' => $angleInDegrees]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->textRotation = $angleInDegrees;
}
} else {
throw new PhpSpreadsheetException('Text rotation should be a value between -90 and 90.');
}
return $this;
}
/**
* Get Wrap Text.
*
* @return bool
*/
public function getWrapText()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getWrapText();
}
return $this->wrapText;
}
/**
* Set Wrap Text.
*
* @param bool $wrapped
*
* @return $this
*/
public function setWrapText($wrapped)
{
if ($wrapped == '') {
$wrapped = false;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['wrapText' => $wrapped]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->wrapText = $wrapped;
}
return $this;
}
/**
* Get Shrink to fit.
*
* @return bool
*/
public function getShrinkToFit()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getShrinkToFit();
}
return $this->shrinkToFit;
}
/**
* Set Shrink to fit.
*
* @param bool $shrink
*
* @return $this
*/
public function setShrinkToFit($shrink)
{
if ($shrink == '') {
$shrink = false;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['shrinkToFit' => $shrink]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->shrinkToFit = $shrink;
}
return $this;
}
/**
* Get indent.
*
* @return int
*/
public function getIndent()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getIndent();
}
return $this->indent;
}
/**
* Set indent.
*
* @param int $indent
*
* @return $this
*/
public function setIndent($indent)
{
if ($indent > 0) {
if (
$this->getHorizontal() != self::HORIZONTAL_GENERAL &&
$this->getHorizontal() != self::HORIZONTAL_LEFT &&
$this->getHorizontal() != self::HORIZONTAL_RIGHT &&
$this->getHorizontal() != self::HORIZONTAL_DISTRIBUTED
) {
$indent = 0; // indent not supported
}
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['indent' => $indent]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->indent = $indent;
}
return $this;
}
/**
* Get read order.
*
* @return int
*/
public function getReadOrder()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getReadOrder();
}
return $this->readOrder;
}
/**
* Set read order.
*
* @param int $readOrder
*
* @return $this
*/
public function setReadOrder($readOrder)
{
if ($readOrder < 0 || $readOrder > 2) {
$readOrder = 0;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['readOrder' => $readOrder]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->readOrder = $readOrder;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->horizontal .
$this->vertical .
$this->textRotation .
($this->wrapText ? 't' : 'f') .
($this->shrinkToFit ? 't' : 'f') .
$this->indent .
$this->readOrder .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'horizontal', $this->getHorizontal());
$this->exportArray2($exportedArray, 'indent', $this->getIndent());
$this->exportArray2($exportedArray, 'readOrder', $this->getReadOrder());
$this->exportArray2($exportedArray, 'shrinkToFit', $this->getShrinkToFit());
$this->exportArray2($exportedArray, 'textRotation', $this->getTextRotation());
$this->exportArray2($exportedArray, 'vertical', $this->getVertical());
$this->exportArray2($exportedArray, 'wrapText', $this->getWrapText());
return $exportedArray;
}
}

View File

@@ -0,0 +1,244 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
class Border extends Supervisor
{
// Border style
const BORDER_NONE = 'none';
const BORDER_DASHDOT = 'dashDot';
const BORDER_DASHDOTDOT = 'dashDotDot';
const BORDER_DASHED = 'dashed';
const BORDER_DOTTED = 'dotted';
const BORDER_DOUBLE = 'double';
const BORDER_HAIR = 'hair';
const BORDER_MEDIUM = 'medium';
const BORDER_MEDIUMDASHDOT = 'mediumDashDot';
const BORDER_MEDIUMDASHDOTDOT = 'mediumDashDotDot';
const BORDER_MEDIUMDASHED = 'mediumDashed';
const BORDER_SLANTDASHDOT = 'slantDashDot';
const BORDER_THICK = 'thick';
const BORDER_THIN = 'thin';
const BORDER_OMIT = 'omit'; // should be used only for Conditional
/**
* Border style.
*
* @var string
*/
protected $borderStyle = self::BORDER_NONE;
/**
* Border color.
*
* @var Color
*/
protected $color;
/**
* @var null|int
*/
public $colorIndex;
/**
* Create a new Border.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, bool $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
// bind parent if we are a supervisor
if ($isSupervisor) {
$this->color->bindParent($this, 'color');
}
if ($isConditional) {
$this->borderStyle = self::BORDER_OMIT;
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Border
*/
public function getSharedComponent()
{
/** @var Style */
$parent = $this->parent;
/** @var Borders $sharedComponent */
$sharedComponent = $parent->getSharedComponent();
switch ($this->parentPropertyName) {
case 'bottom':
return $sharedComponent->getBottom();
case 'diagonal':
return $sharedComponent->getDiagonal();
case 'left':
return $sharedComponent->getLeft();
case 'right':
return $sharedComponent->getRight();
case 'top':
return $sharedComponent->getTop();
}
throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.');
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
/** @var Style */
$parent = $this->parent;
return $parent->/** @scrutinizer ignore-call */ getStyleArray([$this->parentPropertyName => $array]);
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->getTop()->applyFromArray(
* [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['borderStyle'])) {
$this->setBorderStyle($styleArray['borderStyle']);
}
if (isset($styleArray['color'])) {
$this->getColor()->applyFromArray($styleArray['color']);
}
}
return $this;
}
/**
* Get Border style.
*
* @return string
*/
public function getBorderStyle()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getBorderStyle();
}
return $this->borderStyle;
}
/**
* Set Border style.
*
* @param bool|string $style
* When passing a boolean, FALSE equates Border::BORDER_NONE
* and TRUE to Border::BORDER_MEDIUM
*
* @return $this
*/
public function setBorderStyle($style)
{
if (empty($style)) {
$style = self::BORDER_NONE;
} elseif (is_bool($style)) {
$style = self::BORDER_MEDIUM;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['borderStyle' => $style]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->borderStyle = $style;
}
return $this;
}
/**
* Get Border Color.
*
* @return Color
*/
public function getColor()
{
return $this->color;
}
/**
* Set Border Color.
*
* @return $this
*/
public function setColor(Color $color)
{
// make sure parameter is a real color and not a supervisor
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
if ($this->isSupervisor) {
$styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->color = $color;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->borderStyle .
$this->color->getHashCode() .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'borderStyle', $this->getBorderStyle());
$this->exportArray2($exportedArray, 'color', $this->getColor());
return $exportedArray;
}
}

View File

@@ -0,0 +1,424 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
class Borders extends Supervisor
{
// Diagonal directions
const DIAGONAL_NONE = 0;
const DIAGONAL_UP = 1;
const DIAGONAL_DOWN = 2;
const DIAGONAL_BOTH = 3;
/**
* Left.
*
* @var Border
*/
protected $left;
/**
* Right.
*
* @var Border
*/
protected $right;
/**
* Top.
*
* @var Border
*/
protected $top;
/**
* Bottom.
*
* @var Border
*/
protected $bottom;
/**
* Diagonal.
*
* @var Border
*/
protected $diagonal;
/**
* DiagonalDirection.
*
* @var int
*/
protected $diagonalDirection;
/**
* All borders pseudo-border. Only applies to supervisor.
*
* @var Border
*/
protected $allBorders;
/**
* Outline pseudo-border. Only applies to supervisor.
*
* @var Border
*/
protected $outline;
/**
* Inside pseudo-border. Only applies to supervisor.
*
* @var Border
*/
protected $inside;
/**
* Vertical pseudo-border. Only applies to supervisor.
*
* @var Border
*/
protected $vertical;
/**
* Horizontal pseudo-border. Only applies to supervisor.
*
* @var Border
*/
protected $horizontal;
/**
* Create a new Borders.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, bool $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
$this->left = new Border($isSupervisor, $isConditional);
$this->right = new Border($isSupervisor, $isConditional);
$this->top = new Border($isSupervisor, $isConditional);
$this->bottom = new Border($isSupervisor, $isConditional);
$this->diagonal = new Border($isSupervisor, $isConditional);
$this->diagonalDirection = self::DIAGONAL_NONE;
// Specially for supervisor
if ($isSupervisor) {
// Initialize pseudo-borders
$this->allBorders = new Border(true, $isConditional);
$this->outline = new Border(true, $isConditional);
$this->inside = new Border(true, $isConditional);
$this->vertical = new Border(true, $isConditional);
$this->horizontal = new Border(true, $isConditional);
// bind parent if we are a supervisor
$this->left->bindParent($this, 'left');
$this->right->bindParent($this, 'right');
$this->top->bindParent($this, 'top');
$this->bottom->bindParent($this, 'bottom');
$this->diagonal->bindParent($this, 'diagonal');
$this->allBorders->bindParent($this, 'allBorders');
$this->outline->bindParent($this, 'outline');
$this->inside->bindParent($this, 'inside');
$this->vertical->bindParent($this, 'vertical');
$this->horizontal->bindParent($this, 'horizontal');
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Borders
*/
public function getSharedComponent()
{
/** @var Style */
$parent = $this->parent;
return $parent->getSharedComponent()->getBorders();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['borders' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
* [
* 'bottom' => [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ],
* 'top' => [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ]
* ]
* );
* </code>
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getBorders()->applyFromArray(
* [
* 'allBorders' => [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ]
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['left'])) {
$this->getLeft()->applyFromArray($styleArray['left']);
}
if (isset($styleArray['right'])) {
$this->getRight()->applyFromArray($styleArray['right']);
}
if (isset($styleArray['top'])) {
$this->getTop()->applyFromArray($styleArray['top']);
}
if (isset($styleArray['bottom'])) {
$this->getBottom()->applyFromArray($styleArray['bottom']);
}
if (isset($styleArray['diagonal'])) {
$this->getDiagonal()->applyFromArray($styleArray['diagonal']);
}
if (isset($styleArray['diagonalDirection'])) {
$this->setDiagonalDirection($styleArray['diagonalDirection']);
}
if (isset($styleArray['allBorders'])) {
$this->getLeft()->applyFromArray($styleArray['allBorders']);
$this->getRight()->applyFromArray($styleArray['allBorders']);
$this->getTop()->applyFromArray($styleArray['allBorders']);
$this->getBottom()->applyFromArray($styleArray['allBorders']);
}
}
return $this;
}
/**
* Get Left.
*
* @return Border
*/
public function getLeft()
{
return $this->left;
}
/**
* Get Right.
*
* @return Border
*/
public function getRight()
{
return $this->right;
}
/**
* Get Top.
*
* @return Border
*/
public function getTop()
{
return $this->top;
}
/**
* Get Bottom.
*
* @return Border
*/
public function getBottom()
{
return $this->bottom;
}
/**
* Get Diagonal.
*
* @return Border
*/
public function getDiagonal()
{
return $this->diagonal;
}
/**
* Get AllBorders (pseudo-border). Only applies to supervisor.
*
* @return Border
*/
public function getAllBorders()
{
if (!$this->isSupervisor) {
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
}
return $this->allBorders;
}
/**
* Get Outline (pseudo-border). Only applies to supervisor.
*
* @return Border
*/
public function getOutline()
{
if (!$this->isSupervisor) {
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
}
return $this->outline;
}
/**
* Get Inside (pseudo-border). Only applies to supervisor.
*
* @return Border
*/
public function getInside()
{
if (!$this->isSupervisor) {
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
}
return $this->inside;
}
/**
* Get Vertical (pseudo-border). Only applies to supervisor.
*
* @return Border
*/
public function getVertical()
{
if (!$this->isSupervisor) {
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
}
return $this->vertical;
}
/**
* Get Horizontal (pseudo-border). Only applies to supervisor.
*
* @return Border
*/
public function getHorizontal()
{
if (!$this->isSupervisor) {
throw new PhpSpreadsheetException('Can only get pseudo-border for supervisor.');
}
return $this->horizontal;
}
/**
* Get DiagonalDirection.
*
* @return int
*/
public function getDiagonalDirection()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getDiagonalDirection();
}
return $this->diagonalDirection;
}
/**
* Set DiagonalDirection.
*
* @param int $direction see self::DIAGONAL_*
*
* @return $this
*/
public function setDiagonalDirection($direction)
{
if ($direction == '') {
$direction = self::DIAGONAL_NONE;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['diagonalDirection' => $direction]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->diagonalDirection = $direction;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashcode();
}
return md5(
$this->getLeft()->getHashCode() .
$this->getRight()->getHashCode() .
$this->getTop()->getHashCode() .
$this->getBottom()->getHashCode() .
$this->getDiagonal()->getHashCode() .
$this->getDiagonalDirection() .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'bottom', $this->getBottom());
$this->exportArray2($exportedArray, 'diagonal', $this->getDiagonal());
$this->exportArray2($exportedArray, 'diagonalDirection', $this->getDiagonalDirection());
$this->exportArray2($exportedArray, 'left', $this->getLeft());
$this->exportArray2($exportedArray, 'right', $this->getRight());
$this->exportArray2($exportedArray, 'top', $this->getTop());
return $exportedArray;
}
}

View File

@@ -0,0 +1,427 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
class Color extends Supervisor
{
const NAMED_COLORS = [
'Black',
'White',
'Red',
'Green',
'Blue',
'Yellow',
'Magenta',
'Cyan',
];
// Colors
const COLOR_BLACK = 'FF000000';
const COLOR_WHITE = 'FFFFFFFF';
const COLOR_RED = 'FFFF0000';
const COLOR_DARKRED = 'FF800000';
const COLOR_BLUE = 'FF0000FF';
const COLOR_DARKBLUE = 'FF000080';
const COLOR_GREEN = 'FF00FF00';
const COLOR_DARKGREEN = 'FF008000';
const COLOR_YELLOW = 'FFFFFF00';
const COLOR_DARKYELLOW = 'FF808000';
const COLOR_MAGENTA = 'FFFF00FF';
const COLOR_CYAN = 'FF00FFFF';
const NAMED_COLOR_TRANSLATIONS = [
'Black' => self::COLOR_BLACK,
'White' => self::COLOR_WHITE,
'Red' => self::COLOR_RED,
'Green' => self::COLOR_GREEN,
'Blue' => self::COLOR_BLUE,
'Yellow' => self::COLOR_YELLOW,
'Magenta' => self::COLOR_MAGENTA,
'Cyan' => self::COLOR_CYAN,
];
const VALIDATE_ARGB_SIZE = 8;
const VALIDATE_RGB_SIZE = 6;
const VALIDATE_COLOR_6 = '/^[A-F0-9]{6}$/i';
const VALIDATE_COLOR_8 = '/^[A-F0-9]{8}$/i';
private const INDEXED_COLORS = [
1 => 'FF000000', // System Colour #1 - Black
2 => 'FFFFFFFF', // System Colour #2 - White
3 => 'FFFF0000', // System Colour #3 - Red
4 => 'FF00FF00', // System Colour #4 - Green
5 => 'FF0000FF', // System Colour #5 - Blue
6 => 'FFFFFF00', // System Colour #6 - Yellow
7 => 'FFFF00FF', // System Colour #7- Magenta
8 => 'FF00FFFF', // System Colour #8- Cyan
9 => 'FF800000', // Standard Colour #9
10 => 'FF008000', // Standard Colour #10
11 => 'FF000080', // Standard Colour #11
12 => 'FF808000', // Standard Colour #12
13 => 'FF800080', // Standard Colour #13
14 => 'FF008080', // Standard Colour #14
15 => 'FFC0C0C0', // Standard Colour #15
16 => 'FF808080', // Standard Colour #16
17 => 'FF9999FF', // Chart Fill Colour #17
18 => 'FF993366', // Chart Fill Colour #18
19 => 'FFFFFFCC', // Chart Fill Colour #19
20 => 'FFCCFFFF', // Chart Fill Colour #20
21 => 'FF660066', // Chart Fill Colour #21
22 => 'FFFF8080', // Chart Fill Colour #22
23 => 'FF0066CC', // Chart Fill Colour #23
24 => 'FFCCCCFF', // Chart Fill Colour #24
25 => 'FF000080', // Chart Line Colour #25
26 => 'FFFF00FF', // Chart Line Colour #26
27 => 'FFFFFF00', // Chart Line Colour #27
28 => 'FF00FFFF', // Chart Line Colour #28
29 => 'FF800080', // Chart Line Colour #29
30 => 'FF800000', // Chart Line Colour #30
31 => 'FF008080', // Chart Line Colour #31
32 => 'FF0000FF', // Chart Line Colour #32
33 => 'FF00CCFF', // Standard Colour #33
34 => 'FFCCFFFF', // Standard Colour #34
35 => 'FFCCFFCC', // Standard Colour #35
36 => 'FFFFFF99', // Standard Colour #36
37 => 'FF99CCFF', // Standard Colour #37
38 => 'FFFF99CC', // Standard Colour #38
39 => 'FFCC99FF', // Standard Colour #39
40 => 'FFFFCC99', // Standard Colour #40
41 => 'FF3366FF', // Standard Colour #41
42 => 'FF33CCCC', // Standard Colour #42
43 => 'FF99CC00', // Standard Colour #43
44 => 'FFFFCC00', // Standard Colour #44
45 => 'FFFF9900', // Standard Colour #45
46 => 'FFFF6600', // Standard Colour #46
47 => 'FF666699', // Standard Colour #47
48 => 'FF969696', // Standard Colour #48
49 => 'FF003366', // Standard Colour #49
50 => 'FF339966', // Standard Colour #50
51 => 'FF003300', // Standard Colour #51
52 => 'FF333300', // Standard Colour #52
53 => 'FF993300', // Standard Colour #53
54 => 'FF993366', // Standard Colour #54
55 => 'FF333399', // Standard Colour #55
56 => 'FF333333', // Standard Colour #56
];
/**
* ARGB - Alpha RGB.
*
* @var null|string
*/
protected $argb;
/** @var bool */
private $hasChanged = false;
/**
* Create a new Color.
*
* @param string $colorValue ARGB value for the colour, or named colour
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($colorValue = self::COLOR_BLACK, $isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
if (!$isConditional) {
$this->argb = $this->validateColor($colorValue) ?: self::COLOR_BLACK;
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Color
*/
public function getSharedComponent()
{
/** @var Style */
$parent = $this->parent;
/** @var Border|Fill $sharedComponent */
$sharedComponent = $parent->getSharedComponent();
if ($sharedComponent instanceof Fill) {
if ($this->parentPropertyName === 'endColor') {
return $sharedComponent->getEndColor();
}
return $sharedComponent->getStartColor();
}
return $sharedComponent->getColor();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
/** @var Style */
$parent = $this->parent;
return $parent->/** @scrutinizer ignore-call */ getStyleArray([$this->parentPropertyName => $array]);
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray(['rgb' => '808080']);
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['rgb'])) {
$this->setRGB($styleArray['rgb']);
}
if (isset($styleArray['argb'])) {
$this->setARGB($styleArray['argb']);
}
}
return $this;
}
private function validateColor(?string $colorValue): string
{
if ($colorValue === null || $colorValue === '') {
return self::COLOR_BLACK;
}
$named = ucfirst(strtolower($colorValue));
if (array_key_exists($named, self::NAMED_COLOR_TRANSLATIONS)) {
return self::NAMED_COLOR_TRANSLATIONS[$named];
}
if (preg_match(self::VALIDATE_COLOR_8, $colorValue) === 1) {
return $colorValue;
}
if (preg_match(self::VALIDATE_COLOR_6, $colorValue) === 1) {
return 'FF' . $colorValue;
}
return '';
}
/**
* Get ARGB.
*/
public function getARGB(): ?string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getARGB();
}
return $this->argb;
}
/**
* Set ARGB.
*
* @param string $colorValue ARGB value, or a named color
*
* @return $this
*/
public function setARGB(?string $colorValue = self::COLOR_BLACK)
{
$this->hasChanged = true;
$colorValue = $this->validateColor($colorValue);
if ($colorValue === '') {
return $this;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['argb' => $colorValue]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->argb = $colorValue;
}
return $this;
}
/**
* Get RGB.
*/
public function getRGB(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getRGB();
}
return substr($this->argb ?? '', 2);
}
/**
* Set RGB.
*
* @param string $colorValue RGB value, or a named color
*
* @return $this
*/
public function setRGB(?string $colorValue = self::COLOR_BLACK)
{
return $this->setARGB($colorValue);
}
/**
* Get a specified colour component of an RGB value.
*
* @param string $rgbValue The colour as an RGB value (e.g. FF00CCCC or CCDDEE
* @param int $offset Position within the RGB value to extract
* @param bool $hex Flag indicating whether the component should be returned as a hex or a
* decimal value
*
* @return int|string The extracted colour component
*/
private static function getColourComponent($rgbValue, $offset, $hex = true)
{
$colour = substr($rgbValue, $offset, 2) ?: '';
if (preg_match('/^[0-9a-f]{2}$/i', $colour) !== 1) {
$colour = '00';
}
return ($hex) ? $colour : (int) hexdec($colour);
}
/**
* Get the red colour component of an RGB value.
*
* @param string $rgbValue The colour as an RGB value (e.g. FF00CCCC or CCDDEE
* @param bool $hex Flag indicating whether the component should be returned as a hex or a
* decimal value
*
* @return int|string The red colour component
*/
public static function getRed($rgbValue, $hex = true)
{
return self::getColourComponent($rgbValue, strlen($rgbValue) - 6, $hex);
}
/**
* Get the green colour component of an RGB value.
*
* @param string $rgbValue The colour as an RGB value (e.g. FF00CCCC or CCDDEE
* @param bool $hex Flag indicating whether the component should be returned as a hex or a
* decimal value
*
* @return int|string The green colour component
*/
public static function getGreen($rgbValue, $hex = true)
{
return self::getColourComponent($rgbValue, strlen($rgbValue) - 4, $hex);
}
/**
* Get the blue colour component of an RGB value.
*
* @param string $rgbValue The colour as an RGB value (e.g. FF00CCCC or CCDDEE
* @param bool $hex Flag indicating whether the component should be returned as a hex or a
* decimal value
*
* @return int|string The blue colour component
*/
public static function getBlue($rgbValue, $hex = true)
{
return self::getColourComponent($rgbValue, strlen($rgbValue) - 2, $hex);
}
/**
* Adjust the brightness of a color.
*
* @param string $hexColourValue The colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
* @param float $adjustPercentage The percentage by which to adjust the colour as a float from -1 to 1
*
* @return string The adjusted colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
*/
public static function changeBrightness($hexColourValue, $adjustPercentage)
{
$rgba = (strlen($hexColourValue) === 8);
$adjustPercentage = max(-1.0, min(1.0, $adjustPercentage));
/** @var int $red */
$red = self::getRed($hexColourValue, false);
/** @var int $green */
$green = self::getGreen($hexColourValue, false);
/** @var int $blue */
$blue = self::getBlue($hexColourValue, false);
return (($rgba) ? 'FF' : '') . RgbTint::rgbAndTintToRgb($red, $green, $blue, $adjustPercentage);
}
/**
* Get indexed color.
*
* @param int $colorIndex Index entry point into the colour array
* @param bool $background Flag to indicate whether default background or foreground colour
* should be returned if the indexed colour doesn't exist
*/
public static function indexedColor($colorIndex, $background = false, ?array $palette = null): self
{
// Clean parameter
$colorIndex = (int) $colorIndex;
if (empty($palette)) {
if (isset(self::INDEXED_COLORS[$colorIndex])) {
return new self(self::INDEXED_COLORS[$colorIndex]);
}
} else {
if (isset($palette[$colorIndex])) {
return new self($palette[$colorIndex]);
}
}
return ($background) ? new self(self::COLOR_WHITE) : new self(self::COLOR_BLACK);
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->argb .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'argb', $this->getARGB());
return $exportedArray;
}
public function getHasChanged(): bool
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->hasChanged;
}
return $this->hasChanged;
}
}

View File

@@ -0,0 +1,361 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\IComparable;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar;
class Conditional implements IComparable
{
// Condition types
const CONDITION_NONE = 'none';
const CONDITION_BEGINSWITH = 'beginsWith';
const CONDITION_CELLIS = 'cellIs';
const CONDITION_CONTAINSBLANKS = 'containsBlanks';
const CONDITION_CONTAINSERRORS = 'containsErrors';
const CONDITION_CONTAINSTEXT = 'containsText';
const CONDITION_DATABAR = 'dataBar';
const CONDITION_ENDSWITH = 'endsWith';
const CONDITION_EXPRESSION = 'expression';
const CONDITION_NOTCONTAINSBLANKS = 'notContainsBlanks';
const CONDITION_NOTCONTAINSERRORS = 'notContainsErrors';
const CONDITION_NOTCONTAINSTEXT = 'notContainsText';
const CONDITION_TIMEPERIOD = 'timePeriod';
const CONDITION_DUPLICATES = 'duplicateValues';
const CONDITION_UNIQUE = 'uniqueValues';
private const CONDITION_TYPES = [
self::CONDITION_BEGINSWITH,
self::CONDITION_CELLIS,
self::CONDITION_CONTAINSBLANKS,
self::CONDITION_CONTAINSERRORS,
self::CONDITION_CONTAINSTEXT,
self::CONDITION_DATABAR,
self::CONDITION_DUPLICATES,
self::CONDITION_ENDSWITH,
self::CONDITION_EXPRESSION,
self::CONDITION_NONE,
self::CONDITION_NOTCONTAINSBLANKS,
self::CONDITION_NOTCONTAINSERRORS,
self::CONDITION_NOTCONTAINSTEXT,
self::CONDITION_TIMEPERIOD,
self::CONDITION_UNIQUE,
];
// Operator types
const OPERATOR_NONE = '';
const OPERATOR_BEGINSWITH = 'beginsWith';
const OPERATOR_ENDSWITH = 'endsWith';
const OPERATOR_EQUAL = 'equal';
const OPERATOR_GREATERTHAN = 'greaterThan';
const OPERATOR_GREATERTHANOREQUAL = 'greaterThanOrEqual';
const OPERATOR_LESSTHAN = 'lessThan';
const OPERATOR_LESSTHANOREQUAL = 'lessThanOrEqual';
const OPERATOR_NOTEQUAL = 'notEqual';
const OPERATOR_CONTAINSTEXT = 'containsText';
const OPERATOR_NOTCONTAINS = 'notContains';
const OPERATOR_BETWEEN = 'between';
const OPERATOR_NOTBETWEEN = 'notBetween';
const TIMEPERIOD_TODAY = 'today';
const TIMEPERIOD_YESTERDAY = 'yesterday';
const TIMEPERIOD_TOMORROW = 'tomorrow';
const TIMEPERIOD_LAST_7_DAYS = 'last7Days';
const TIMEPERIOD_LAST_WEEK = 'lastWeek';
const TIMEPERIOD_THIS_WEEK = 'thisWeek';
const TIMEPERIOD_NEXT_WEEK = 'nextWeek';
const TIMEPERIOD_LAST_MONTH = 'lastMonth';
const TIMEPERIOD_THIS_MONTH = 'thisMonth';
const TIMEPERIOD_NEXT_MONTH = 'nextMonth';
/**
* Condition type.
*
* @var string
*/
private $conditionType = self::CONDITION_NONE;
/**
* Operator type.
*
* @var string
*/
private $operatorType = self::OPERATOR_NONE;
/**
* Text.
*
* @var string
*/
private $text;
/**
* Stop on this condition, if it matches.
*
* @var bool
*/
private $stopIfTrue = false;
/**
* Condition.
*
* @var (bool|float|int|string)[]
*/
private $condition = [];
/**
* @var ConditionalDataBar
*/
private $dataBar;
/**
* Style.
*
* @var Style
*/
private $style;
/** @var bool */
private $noFormatSet = false;
/**
* Create a new Conditional.
*/
public function __construct()
{
// Initialise values
$this->style = new Style(false, true);
}
public function getNoFormatSet(): bool
{
return $this->noFormatSet;
}
public function setNoFormatSet(bool $noFormatSet): self
{
$this->noFormatSet = $noFormatSet;
return $this;
}
/**
* Get Condition type.
*
* @return string
*/
public function getConditionType()
{
return $this->conditionType;
}
/**
* Set Condition type.
*
* @param string $type Condition type, see self::CONDITION_*
*
* @return $this
*/
public function setConditionType($type)
{
$this->conditionType = $type;
return $this;
}
/**
* Get Operator type.
*
* @return string
*/
public function getOperatorType()
{
return $this->operatorType;
}
/**
* Set Operator type.
*
* @param string $type Conditional operator type, see self::OPERATOR_*
*
* @return $this
*/
public function setOperatorType($type)
{
$this->operatorType = $type;
return $this;
}
/**
* Get text.
*
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* Set text.
*
* @param string $text
*
* @return $this
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* Get StopIfTrue.
*
* @return bool
*/
public function getStopIfTrue()
{
return $this->stopIfTrue;
}
/**
* Set StopIfTrue.
*
* @param bool $stopIfTrue
*
* @return $this
*/
public function setStopIfTrue($stopIfTrue)
{
$this->stopIfTrue = $stopIfTrue;
return $this;
}
/**
* Get Conditions.
*
* @return (bool|float|int|string)[]
*/
public function getConditions()
{
return $this->condition;
}
/**
* Set Conditions.
*
* @param (bool|float|int|string)[]|bool|float|int|string $conditions Condition
*
* @return $this
*/
public function setConditions($conditions)
{
if (!is_array($conditions)) {
$conditions = [$conditions];
}
$this->condition = $conditions;
return $this;
}
/**
* Add Condition.
*
* @param bool|float|int|string $condition Condition
*
* @return $this
*/
public function addCondition($condition)
{
$this->condition[] = $condition;
return $this;
}
/**
* Get Style.
*
* @return Style
*/
public function getStyle()
{
return $this->style;
}
/**
* Set Style.
*
* @return $this
*/
public function setStyle(Style $style)
{
$this->style = $style;
return $this;
}
/**
* get DataBar.
*
* @return null|ConditionalDataBar
*/
public function getDataBar()
{
return $this->dataBar;
}
/**
* set DataBar.
*
* @return $this
*/
public function setDataBar(ConditionalDataBar $dataBar)
{
$this->dataBar = $dataBar;
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
return md5(
$this->conditionType .
$this->operatorType .
implode(';', $this->condition) .
$this->style->getHashCode() .
__CLASS__
);
}
/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$vars = get_object_vars($this);
foreach ($vars as $key => $value) {
if (is_object($value)) {
$this->$key = clone $value;
} else {
$this->$key = $value;
}
}
}
/**
* Verify if param is valid condition type.
*/
public static function isValidConditionType(string $type): bool
{
return in_array($type, self::CONDITION_TYPES);
}
}

View File

@@ -0,0 +1,313 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class CellMatcher
{
public const COMPARISON_OPERATORS = [
Conditional::OPERATOR_EQUAL => '=',
Conditional::OPERATOR_GREATERTHAN => '>',
Conditional::OPERATOR_GREATERTHANOREQUAL => '>=',
Conditional::OPERATOR_LESSTHAN => '<',
Conditional::OPERATOR_LESSTHANOREQUAL => '<=',
Conditional::OPERATOR_NOTEQUAL => '<>',
];
public const COMPARISON_RANGE_OPERATORS = [
Conditional::OPERATOR_BETWEEN => 'IF(AND(A1>=%s,A1<=%s),TRUE,FALSE)',
Conditional::OPERATOR_NOTBETWEEN => 'IF(AND(A1>=%s,A1<=%s),FALSE,TRUE)',
];
public const COMPARISON_DUPLICATES_OPERATORS = [
Conditional::CONDITION_DUPLICATES => "COUNTIF('%s'!%s,%s)>1",
Conditional::CONDITION_UNIQUE => "COUNTIF('%s'!%s,%s)=1",
];
/**
* @var Cell
*/
protected $cell;
/**
* @var int
*/
protected $cellRow;
/**
* @var Worksheet
*/
protected $worksheet;
/**
* @var int
*/
protected $cellColumn;
/**
* @var string
*/
protected $conditionalRange;
/**
* @var string
*/
protected $referenceCell;
/**
* @var int
*/
protected $referenceRow;
/**
* @var int
*/
protected $referenceColumn;
/**
* @var Calculation
*/
protected $engine;
public function __construct(Cell $cell, string $conditionalRange)
{
$this->cell = $cell;
$this->worksheet = $cell->getWorksheet();
[$this->cellColumn, $this->cellRow] = Coordinate::indexesFromString($this->cell->getCoordinate());
$this->setReferenceCellForExpressions($conditionalRange);
$this->engine = Calculation::getInstance($this->worksheet->getParent());
}
protected function setReferenceCellForExpressions(string $conditionalRange): void
{
$conditionalRange = Coordinate::splitRange(str_replace('$', '', strtoupper($conditionalRange)));
[$this->referenceCell] = $conditionalRange[0];
[$this->referenceColumn, $this->referenceRow] = Coordinate::indexesFromString($this->referenceCell);
// Convert our conditional range to an absolute conditional range, so it can be used "pinned" in formulae
$rangeSets = [];
foreach ($conditionalRange as $rangeSet) {
$absoluteRangeSet = array_map(
[Coordinate::class, 'absoluteCoordinate'],
$rangeSet
);
$rangeSets[] = implode(':', $absoluteRangeSet);
}
$this->conditionalRange = implode(',', $rangeSets);
}
public function evaluateConditional(Conditional $conditional): bool
{
// Some calculations may modify the stored cell; so reset it before every evaluation.
$cellColumn = Coordinate::stringFromColumnIndex($this->cellColumn);
$cellAddress = "{$cellColumn}{$this->cellRow}";
$this->cell = $this->worksheet->getCell($cellAddress);
switch ($conditional->getConditionType()) {
case Conditional::CONDITION_CELLIS:
return $this->processOperatorComparison($conditional);
case Conditional::CONDITION_DUPLICATES:
case Conditional::CONDITION_UNIQUE:
return $this->processDuplicatesComparison($conditional);
case Conditional::CONDITION_CONTAINSTEXT:
// Expression is NOT(ISERROR(SEARCH("<TEXT>",<Cell Reference>)))
case Conditional::CONDITION_NOTCONTAINSTEXT:
// Expression is ISERROR(SEARCH("<TEXT>",<Cell Reference>))
case Conditional::CONDITION_BEGINSWITH:
// Expression is LEFT(<Cell Reference>,LEN("<TEXT>"))="<TEXT>"
case Conditional::CONDITION_ENDSWITH:
// Expression is RIGHT(<Cell Reference>,LEN("<TEXT>"))="<TEXT>"
case Conditional::CONDITION_CONTAINSBLANKS:
// Expression is LEN(TRIM(<Cell Reference>))=0
case Conditional::CONDITION_NOTCONTAINSBLANKS:
// Expression is LEN(TRIM(<Cell Reference>))>0
case Conditional::CONDITION_CONTAINSERRORS:
// Expression is ISERROR(<Cell Reference>)
case Conditional::CONDITION_NOTCONTAINSERRORS:
// Expression is NOT(ISERROR(<Cell Reference>))
case Conditional::CONDITION_TIMEPERIOD:
// Expression varies, depending on specified timePeriod value, e.g.
// Yesterday FLOOR(<Cell Reference>,1)=TODAY()-1
// Today FLOOR(<Cell Reference>,1)=TODAY()
// Tomorrow FLOOR(<Cell Reference>,1)=TODAY()+1
// Last 7 Days AND(TODAY()-FLOOR(<Cell Reference>,1)<=6,FLOOR(<Cell Reference>,1)<=TODAY())
case Conditional::CONDITION_EXPRESSION:
return $this->processExpression($conditional);
}
return false;
}
/**
* @param mixed $value
*
* @return float|int|string
*/
protected function wrapValue($value)
{
if (!is_numeric($value)) {
if (is_bool($value)) {
return $value ? 'TRUE' : 'FALSE';
} elseif ($value === null) {
return 'NULL';
}
return '"' . $value . '"';
}
return $value;
}
/**
* @return float|int|string
*/
protected function wrapCellValue()
{
return $this->wrapValue($this->cell->getCalculatedValue());
}
/**
* @return float|int|string
*/
protected function conditionCellAdjustment(array $matches)
{
$column = $matches[6];
$row = $matches[7];
if (strpos($column, '$') === false) {
$column = Coordinate::columnIndexFromString($column);
$column += $this->cellColumn - $this->referenceColumn;
$column = Coordinate::stringFromColumnIndex($column);
}
if (strpos($row, '$') === false) {
$row += $this->cellRow - $this->referenceRow;
}
if (!empty($matches[4])) {
$worksheet = $this->worksheet->getParentOrThrow()->getSheetByName(trim($matches[4], "'"));
if ($worksheet === null) {
return $this->wrapValue(null);
}
return $this->wrapValue(
$worksheet
->getCell(str_replace('$', '', "{$column}{$row}"))
->getCalculatedValue()
);
}
return $this->wrapValue(
$this->worksheet
->getCell(str_replace('$', '', "{$column}{$row}"))
->getCalculatedValue()
);
}
protected function cellConditionCheck(string $condition): string
{
$splitCondition = explode(Calculation::FORMULA_STRING_QUOTE, $condition);
$i = false;
foreach ($splitCondition as &$value) {
// Only count/replace in alternating array entries (ie. not in quoted strings)
$i = $i === false;
if ($i) {
$value = (string) preg_replace_callback(
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/i',
[$this, 'conditionCellAdjustment'],
$value
);
}
}
unset($value);
// Then rebuild the condition string to return it
return implode(Calculation::FORMULA_STRING_QUOTE, $splitCondition);
}
protected function adjustConditionsForCellReferences(array $conditions): array
{
return array_map(
[$this, 'cellConditionCheck'],
$conditions
);
}
protected function processOperatorComparison(Conditional $conditional): bool
{
if (array_key_exists($conditional->getOperatorType(), self::COMPARISON_RANGE_OPERATORS)) {
return $this->processRangeOperator($conditional);
}
$operator = self::COMPARISON_OPERATORS[$conditional->getOperatorType()];
$conditions = $this->adjustConditionsForCellReferences($conditional->getConditions());
$expression = sprintf('%s%s%s', (string) $this->wrapCellValue(), $operator, (string) array_pop($conditions));
return $this->evaluateExpression($expression);
}
protected function processRangeOperator(Conditional $conditional): bool
{
$conditions = $this->adjustConditionsForCellReferences($conditional->getConditions());
sort($conditions);
$expression = sprintf(
(string) preg_replace(
'/\bA1\b/i',
(string) $this->wrapCellValue(),
self::COMPARISON_RANGE_OPERATORS[$conditional->getOperatorType()]
),
...$conditions
);
return $this->evaluateExpression($expression);
}
protected function processDuplicatesComparison(Conditional $conditional): bool
{
$worksheetName = $this->cell->getWorksheet()->getTitle();
$expression = sprintf(
self::COMPARISON_DUPLICATES_OPERATORS[$conditional->getConditionType()],
$worksheetName,
$this->conditionalRange,
$this->cellConditionCheck($this->cell->getCalculatedValue())
);
return $this->evaluateExpression($expression);
}
protected function processExpression(Conditional $conditional): bool
{
$conditions = $this->adjustConditionsForCellReferences($conditional->getConditions());
$expression = array_pop($conditions);
$expression = (string) preg_replace(
'/\b' . $this->referenceCell . '\b/i',
(string) $this->wrapCellValue(),
$expression
);
return $this->evaluateExpression($expression);
}
protected function evaluateExpression(string $expression): bool
{
$expression = "={$expression}";
try {
$this->engine->flushInstance();
$result = (bool) $this->engine->calculateFormula($expression);
} catch (Exception $e) {
return false;
}
return $result;
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\Style;
class CellStyleAssessor
{
/**
* @var CellMatcher
*/
protected $cellMatcher;
/**
* @var StyleMerger
*/
protected $styleMerger;
public function __construct(Cell $cell, string $conditionalRange)
{
$this->cellMatcher = new CellMatcher($cell, $conditionalRange);
$this->styleMerger = new StyleMerger($cell->getStyle());
}
/**
* @param Conditional[] $conditionalStyles
*/
public function matchConditions(array $conditionalStyles = []): Style
{
foreach ($conditionalStyles as $conditional) {
/** @var Conditional $conditional */
if ($this->cellMatcher->evaluateConditional($conditional) === true) {
// Merging the conditional style into the base style goes in here
$this->styleMerger->mergeStyle($conditional->getStyle());
if ($conditional->getStopIfTrue() === true) {
break;
}
}
}
return $this->styleMerger->getStyle();
}
}

View File

@@ -0,0 +1,93 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
class ConditionalDataBar
{
/** <dataBar> attribute */
/** @var null|bool */
private $showValue;
/** <dataBar> children */
/** @var ?ConditionalFormatValueObject */
private $minimumConditionalFormatValueObject;
/** @var ?ConditionalFormatValueObject */
private $maximumConditionalFormatValueObject;
/** @var string */
private $color;
/** <extLst> */
/** @var ?ConditionalFormattingRuleExtension */
private $conditionalFormattingRuleExt;
/**
* @return null|bool
*/
public function getShowValue()
{
return $this->showValue;
}
/**
* @param bool $showValue
*/
public function setShowValue($showValue): self
{
$this->showValue = $showValue;
return $this;
}
public function getMinimumConditionalFormatValueObject(): ?ConditionalFormatValueObject
{
return $this->minimumConditionalFormatValueObject;
}
public function setMinimumConditionalFormatValueObject(ConditionalFormatValueObject $minimumConditionalFormatValueObject): self
{
$this->minimumConditionalFormatValueObject = $minimumConditionalFormatValueObject;
return $this;
}
public function getMaximumConditionalFormatValueObject(): ?ConditionalFormatValueObject
{
return $this->maximumConditionalFormatValueObject;
}
public function setMaximumConditionalFormatValueObject(ConditionalFormatValueObject $maximumConditionalFormatValueObject): self
{
$this->maximumConditionalFormatValueObject = $maximumConditionalFormatValueObject;
return $this;
}
public function getColor(): string
{
return $this->color;
}
public function setColor(string $color): self
{
$this->color = $color;
return $this;
}
public function getConditionalFormattingRuleExt(): ?ConditionalFormattingRuleExtension
{
return $this->conditionalFormattingRuleExt;
}
public function setConditionalFormattingRuleExt(ConditionalFormattingRuleExtension $conditionalFormattingRuleExt): self
{
$this->conditionalFormattingRuleExt = $conditionalFormattingRuleExt;
return $this;
}
}

View File

@@ -0,0 +1,290 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
class ConditionalDataBarExtension
{
/** <dataBar> attributes */
/** @var int */
private $minLength;
/** @var int */
private $maxLength;
/** @var null|bool */
private $border;
/** @var null|bool */
private $gradient;
/** @var string */
private $direction;
/** @var null|bool */
private $negativeBarBorderColorSameAsPositive;
/** @var string */
private $axisPosition;
// <dataBar> children
/** @var ConditionalFormatValueObject */
private $maximumConditionalFormatValueObject;
/** @var ConditionalFormatValueObject */
private $minimumConditionalFormatValueObject;
/** @var string */
private $borderColor;
/** @var string */
private $negativeFillColor;
/** @var string */
private $negativeBorderColor;
/** @var array */
private $axisColor = [
'rgb' => null,
'theme' => null,
'tint' => null,
];
public function getXmlAttributes(): array
{
$ret = [];
foreach (['minLength', 'maxLength', 'direction', 'axisPosition'] as $attrKey) {
if (null !== $this->{$attrKey}) {
$ret[$attrKey] = $this->{$attrKey};
}
}
foreach (['border', 'gradient', 'negativeBarBorderColorSameAsPositive'] as $attrKey) {
if (null !== $this->{$attrKey}) {
$ret[$attrKey] = $this->{$attrKey} ? '1' : '0';
}
}
return $ret;
}
public function getXmlElements(): array
{
$ret = [];
$elms = ['borderColor', 'negativeFillColor', 'negativeBorderColor'];
foreach ($elms as $elmKey) {
if (null !== $this->{$elmKey}) {
$ret[$elmKey] = ['rgb' => $this->{$elmKey}];
}
}
foreach (array_filter($this->axisColor) as $attrKey => $axisColorAttr) {
if (!isset($ret['axisColor'])) {
$ret['axisColor'] = [];
}
$ret['axisColor'][$attrKey] = $axisColorAttr;
}
return $ret;
}
/**
* @return int
*/
public function getMinLength()
{
return $this->minLength;
}
public function setMinLength(int $minLength): self
{
$this->minLength = $minLength;
return $this;
}
/**
* @return int
*/
public function getMaxLength()
{
return $this->maxLength;
}
public function setMaxLength(int $maxLength): self
{
$this->maxLength = $maxLength;
return $this;
}
/**
* @return null|bool
*/
public function getBorder()
{
return $this->border;
}
public function setBorder(bool $border): self
{
$this->border = $border;
return $this;
}
/**
* @return null|bool
*/
public function getGradient()
{
return $this->gradient;
}
public function setGradient(bool $gradient): self
{
$this->gradient = $gradient;
return $this;
}
/**
* @return string
*/
public function getDirection()
{
return $this->direction;
}
public function setDirection(string $direction): self
{
$this->direction = $direction;
return $this;
}
/**
* @return null|bool
*/
public function getNegativeBarBorderColorSameAsPositive()
{
return $this->negativeBarBorderColorSameAsPositive;
}
public function setNegativeBarBorderColorSameAsPositive(bool $negativeBarBorderColorSameAsPositive): self
{
$this->negativeBarBorderColorSameAsPositive = $negativeBarBorderColorSameAsPositive;
return $this;
}
/**
* @return string
*/
public function getAxisPosition()
{
return $this->axisPosition;
}
public function setAxisPosition(string $axisPosition): self
{
$this->axisPosition = $axisPosition;
return $this;
}
/**
* @return ConditionalFormatValueObject
*/
public function getMaximumConditionalFormatValueObject()
{
return $this->maximumConditionalFormatValueObject;
}
public function setMaximumConditionalFormatValueObject(ConditionalFormatValueObject $maximumConditionalFormatValueObject): self
{
$this->maximumConditionalFormatValueObject = $maximumConditionalFormatValueObject;
return $this;
}
/**
* @return ConditionalFormatValueObject
*/
public function getMinimumConditionalFormatValueObject()
{
return $this->minimumConditionalFormatValueObject;
}
public function setMinimumConditionalFormatValueObject(ConditionalFormatValueObject $minimumConditionalFormatValueObject): self
{
$this->minimumConditionalFormatValueObject = $minimumConditionalFormatValueObject;
return $this;
}
/**
* @return string
*/
public function getBorderColor()
{
return $this->borderColor;
}
public function setBorderColor(string $borderColor): self
{
$this->borderColor = $borderColor;
return $this;
}
/**
* @return string
*/
public function getNegativeFillColor()
{
return $this->negativeFillColor;
}
public function setNegativeFillColor(string $negativeFillColor): self
{
$this->negativeFillColor = $negativeFillColor;
return $this;
}
/**
* @return string
*/
public function getNegativeBorderColor()
{
return $this->negativeBorderColor;
}
public function setNegativeBorderColor(string $negativeBorderColor): self
{
$this->negativeBorderColor = $negativeBorderColor;
return $this;
}
public function getAxisColor(): array
{
return $this->axisColor;
}
/**
* @param mixed $rgb
* @param null|mixed $theme
* @param null|mixed $tint
*/
public function setAxisColor($rgb, $theme = null, $tint = null): self
{
$this->axisColor = [
'rgb' => $rgb,
'theme' => $theme,
'tint' => $tint,
];
return $this;
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
class ConditionalFormatValueObject
{
/** @var mixed */
private $type;
/** @var mixed */
private $value;
/** @var mixed */
private $cellFormula;
/**
* ConditionalFormatValueObject constructor.
*
* @param mixed $type
* @param mixed $value
* @param null|mixed $cellFormula
*/
public function __construct($type, $value = null, $cellFormula = null)
{
$this->type = $type;
$this->value = $value;
$this->cellFormula = $cellFormula;
}
/**
* @return mixed
*/
public function getType()
{
return $this->type;
}
/**
* @param mixed $type
*/
public function setType($type): self
{
$this->type = $type;
return $this;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* @param mixed $value
*/
public function setValue($value): self
{
$this->value = $value;
return $this;
}
/**
* @return mixed
*/
public function getCellFormula()
{
return $this->cellFormula;
}
/**
* @param mixed $cellFormula
*/
public function setCellFormula($cellFormula): self
{
$this->cellFormula = $cellFormula;
return $this;
}
}

View File

@@ -0,0 +1,226 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use SimpleXMLElement;
class ConditionalFormattingRuleExtension
{
const CONDITION_EXTENSION_DATABAR = 'dataBar';
/** <conditionalFormatting> attributes */
/** @var string */
private $id;
/** @var string Conditional Formatting Rule */
private $cfRule;
/** <conditionalFormatting> children */
/** @var ConditionalDataBarExtension */
private $dataBar;
/** @var string Sequence of References */
private $sqref;
/**
* ConditionalFormattingRuleExtension constructor.
*/
public function __construct(?string $id = null, string $cfRule = self::CONDITION_EXTENSION_DATABAR)
{
if (null === $id) {
$this->id = '{' . $this->generateUuid() . '}';
} else {
$this->id = $id;
}
$this->cfRule = $cfRule;
}
private function generateUuid(): string
{
$chars = str_split('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx');
foreach ($chars as $i => $char) {
if ($char === 'x') {
$chars[$i] = dechex(random_int(0, 15));
} elseif ($char === 'y') {
$chars[$i] = dechex(random_int(8, 11));
}
}
return implode('', /** @scrutinizer ignore-type */ $chars);
}
public static function parseExtLstXml(?SimpleXMLElement $extLstXml): array
{
$conditionalFormattingRuleExtensions = [];
$conditionalFormattingRuleExtensionXml = null;
if ($extLstXml instanceof SimpleXMLElement) {
foreach ((count($extLstXml) > 0 ? $extLstXml : [$extLstXml]) as $extLst) {
//this uri is conditionalFormattings
//https://docs.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/07d607af-5618-4ca2-b683-6a78dc0d9627
if (isset($extLst->ext['uri']) && (string) $extLst->ext['uri'] === '{78C0D931-6437-407d-A8EE-F0AAD7539E65}') {
$conditionalFormattingRuleExtensionXml = $extLst->ext;
}
}
if ($conditionalFormattingRuleExtensionXml) {
$ns = $conditionalFormattingRuleExtensionXml->getNamespaces(true);
$extFormattingsXml = $conditionalFormattingRuleExtensionXml->children($ns['x14']);
foreach ($extFormattingsXml->children($ns['x14']) as $extFormattingXml) {
$extCfRuleXml = $extFormattingXml->cfRule;
$attributes = $extCfRuleXml->attributes();
if (!$attributes || ((string) $attributes->type) !== Conditional::CONDITION_DATABAR) {
continue;
}
$extFormattingRuleObj = new self((string) $attributes->id);
$extFormattingRuleObj->setSqref((string) $extFormattingXml->children($ns['xm'])->sqref);
$conditionalFormattingRuleExtensions[$extFormattingRuleObj->getId()] = $extFormattingRuleObj;
$extDataBarObj = new ConditionalDataBarExtension();
$extFormattingRuleObj->setDataBarExt($extDataBarObj);
$dataBarXml = $extCfRuleXml->dataBar;
self::parseExtDataBarAttributesFromXml($extDataBarObj, $dataBarXml);
self::parseExtDataBarElementChildrenFromXml($extDataBarObj, $dataBarXml, $ns);
}
}
}
return $conditionalFormattingRuleExtensions;
}
private static function parseExtDataBarAttributesFromXml(
ConditionalDataBarExtension $extDataBarObj,
SimpleXMLElement $dataBarXml
): void {
$dataBarAttribute = $dataBarXml->attributes();
if ($dataBarAttribute === null) {
return;
}
if ($dataBarAttribute->minLength) {
$extDataBarObj->setMinLength((int) $dataBarAttribute->minLength);
}
if ($dataBarAttribute->maxLength) {
$extDataBarObj->setMaxLength((int) $dataBarAttribute->maxLength);
}
if ($dataBarAttribute->border) {
$extDataBarObj->setBorder((bool) (string) $dataBarAttribute->border);
}
if ($dataBarAttribute->gradient) {
$extDataBarObj->setGradient((bool) (string) $dataBarAttribute->gradient);
}
if ($dataBarAttribute->direction) {
$extDataBarObj->setDirection((string) $dataBarAttribute->direction);
}
if ($dataBarAttribute->negativeBarBorderColorSameAsPositive) {
$extDataBarObj->setNegativeBarBorderColorSameAsPositive((bool) (string) $dataBarAttribute->negativeBarBorderColorSameAsPositive);
}
if ($dataBarAttribute->axisPosition) {
$extDataBarObj->setAxisPosition((string) $dataBarAttribute->axisPosition);
}
}
/** @param array|SimpleXMLElement $ns */
private static function parseExtDataBarElementChildrenFromXml(ConditionalDataBarExtension $extDataBarObj, SimpleXMLElement $dataBarXml, $ns): void
{
if ($dataBarXml->borderColor) {
$attributes = $dataBarXml->borderColor->attributes();
if ($attributes !== null) {
$extDataBarObj->setBorderColor((string) $attributes['rgb']);
}
}
if ($dataBarXml->negativeFillColor) {
$attributes = $dataBarXml->negativeFillColor->attributes();
if ($attributes !== null) {
$extDataBarObj->setNegativeFillColor((string) $attributes['rgb']);
}
}
if ($dataBarXml->negativeBorderColor) {
$attributes = $dataBarXml->negativeBorderColor->attributes();
if ($attributes !== null) {
$extDataBarObj->setNegativeBorderColor((string) $attributes['rgb']);
}
}
if ($dataBarXml->axisColor) {
$axisColorAttr = $dataBarXml->axisColor->attributes();
if ($axisColorAttr !== null) {
$extDataBarObj->setAxisColor((string) $axisColorAttr['rgb'], (string) $axisColorAttr['theme'], (string) $axisColorAttr['tint']);
}
}
$cfvoIndex = 0;
foreach ($dataBarXml->cfvo as $cfvo) {
$f = (string) $cfvo->/** @scrutinizer ignore-call */ children($ns['xm'])->f;
/** @scrutinizer ignore-call */
$attributes = $cfvo->attributes();
if (!($attributes)) {
continue;
}
if ($cfvoIndex === 0) {
$extDataBarObj->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $attributes['type'], null, (empty($f) ? null : $f)));
}
if ($cfvoIndex === 1) {
$extDataBarObj->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $attributes['type'], null, (empty($f) ? null : $f)));
}
++$cfvoIndex;
}
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): self
{
$this->id = $id;
return $this;
}
public function getCfRule(): string
{
return $this->cfRule;
}
public function setCfRule(string $cfRule): self
{
$this->cfRule = $cfRule;
return $this;
}
public function getDataBarExt(): ConditionalDataBarExtension
{
return $this->dataBar;
}
public function setDataBarExt(ConditionalDataBarExtension $dataBar): self
{
$this->dataBar = $dataBar;
return $this;
}
public function getSqref(): string
{
return $this->sqref;
}
public function setSqref(string $sqref): self
{
$this->sqref = $sqref;
return $this;
}
}

View File

@@ -0,0 +1,118 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Borders;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\Style;
class StyleMerger
{
/**
* @var Style
*/
protected $baseStyle;
public function __construct(Style $baseStyle)
{
$this->baseStyle = $baseStyle;
}
public function getStyle(): Style
{
return $this->baseStyle;
}
public function mergeStyle(Style $style): void
{
if ($style->getNumberFormat() !== null && $style->getNumberFormat()->getFormatCode() !== null) {
$this->baseStyle->getNumberFormat()->setFormatCode($style->getNumberFormat()->getFormatCode());
}
if ($style->getFont() !== null) {
$this->mergeFontStyle($this->baseStyle->getFont(), $style->getFont());
}
if ($style->getFill() !== null) {
$this->mergeFillStyle($this->baseStyle->getFill(), $style->getFill());
}
if ($style->getBorders() !== null) {
$this->mergeBordersStyle($this->baseStyle->getBorders(), $style->getBorders());
}
}
protected function mergeFontStyle(Font $baseFontStyle, Font $fontStyle): void
{
if ($fontStyle->getBold() !== null) {
$baseFontStyle->setBold($fontStyle->getBold());
}
if ($fontStyle->getItalic() !== null) {
$baseFontStyle->setItalic($fontStyle->getItalic());
}
if ($fontStyle->getStrikethrough() !== null) {
$baseFontStyle->setStrikethrough($fontStyle->getStrikethrough());
}
if ($fontStyle->getUnderline() !== null) {
$baseFontStyle->setUnderline($fontStyle->getUnderline());
}
if ($fontStyle->getColor() !== null && $fontStyle->getColor()->getARGB() !== null) {
$baseFontStyle->setColor($fontStyle->getColor());
}
}
protected function mergeFillStyle(Fill $baseFillStyle, Fill $fillStyle): void
{
if ($fillStyle->getFillType() !== null) {
$baseFillStyle->setFillType($fillStyle->getFillType());
}
//if ($fillStyle->getRotation() !== null) {
$baseFillStyle->setRotation($fillStyle->getRotation());
//}
if ($fillStyle->getStartColor() !== null && $fillStyle->getStartColor()->getARGB() !== null) {
$baseFillStyle->setStartColor($fillStyle->getStartColor());
}
if ($fillStyle->getEndColor() !== null && $fillStyle->getEndColor()->getARGB() !== null) {
$baseFillStyle->setEndColor($fillStyle->getEndColor());
}
}
protected function mergeBordersStyle(Borders $baseBordersStyle, Borders $bordersStyle): void
{
if ($bordersStyle->getTop() !== null) {
$this->mergeBorderStyle($baseBordersStyle->getTop(), $bordersStyle->getTop());
}
if ($bordersStyle->getBottom() !== null) {
$this->mergeBorderStyle($baseBordersStyle->getBottom(), $bordersStyle->getBottom());
}
if ($bordersStyle->getLeft() !== null) {
$this->mergeBorderStyle($baseBordersStyle->getLeft(), $bordersStyle->getLeft());
}
if ($bordersStyle->getRight() !== null) {
$this->mergeBorderStyle($baseBordersStyle->getRight(), $bordersStyle->getRight());
}
}
protected function mergeBorderStyle(Border $baseBorderStyle, Border $borderStyle): void
{
//if ($borderStyle->getBorderStyle() !== null) {
$baseBorderStyle->setBorderStyle($borderStyle->getBorderStyle());
//}
if ($borderStyle->getColor() !== null && $borderStyle->getColor()->getARGB() !== null) {
$baseBorderStyle->setColor($borderStyle->getColor());
}
}
}

View File

@@ -0,0 +1,95 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard\WizardInterface;
class Wizard
{
public const CELL_VALUE = 'cellValue';
public const TEXT_VALUE = 'textValue';
public const BLANKS = Conditional::CONDITION_CONTAINSBLANKS;
public const NOT_BLANKS = Conditional::CONDITION_NOTCONTAINSBLANKS;
public const ERRORS = Conditional::CONDITION_CONTAINSERRORS;
public const NOT_ERRORS = Conditional::CONDITION_NOTCONTAINSERRORS;
public const EXPRESSION = Conditional::CONDITION_EXPRESSION;
public const FORMULA = Conditional::CONDITION_EXPRESSION;
public const DATES_OCCURRING = 'DateValue';
public const DUPLICATES = Conditional::CONDITION_DUPLICATES;
public const UNIQUE = Conditional::CONDITION_UNIQUE;
public const VALUE_TYPE_LITERAL = 'value';
public const VALUE_TYPE_CELL = 'cell';
public const VALUE_TYPE_FORMULA = 'formula';
/**
* @var string
*/
protected $cellRange;
public function __construct(string $cellRange)
{
$this->cellRange = $cellRange;
}
public function newRule(string $ruleType): WizardInterface
{
switch ($ruleType) {
case self::CELL_VALUE:
return new Wizard\CellValue($this->cellRange);
case self::TEXT_VALUE:
return new Wizard\TextValue($this->cellRange);
case self::BLANKS:
return new Wizard\Blanks($this->cellRange, true);
case self::NOT_BLANKS:
return new Wizard\Blanks($this->cellRange, false);
case self::ERRORS:
return new Wizard\Errors($this->cellRange, true);
case self::NOT_ERRORS:
return new Wizard\Errors($this->cellRange, false);
case self::EXPRESSION:
case self::FORMULA:
return new Wizard\Expression($this->cellRange);
case self::DATES_OCCURRING:
return new Wizard\DateValue($this->cellRange);
case self::DUPLICATES:
return new Wizard\Duplicates($this->cellRange, false);
case self::UNIQUE:
return new Wizard\Duplicates($this->cellRange, true);
default:
throw new Exception('No wizard exists for this CF rule type');
}
}
public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
{
$conditionalType = $conditional->getConditionType();
switch ($conditionalType) {
case Conditional::CONDITION_CELLIS:
return Wizard\CellValue::fromConditional($conditional, $cellRange);
case Conditional::CONDITION_CONTAINSTEXT:
case Conditional::CONDITION_NOTCONTAINSTEXT:
case Conditional::CONDITION_BEGINSWITH:
case Conditional::CONDITION_ENDSWITH:
return Wizard\TextValue::fromConditional($conditional, $cellRange);
case Conditional::CONDITION_CONTAINSBLANKS:
case Conditional::CONDITION_NOTCONTAINSBLANKS:
return Wizard\Blanks::fromConditional($conditional, $cellRange);
case Conditional::CONDITION_CONTAINSERRORS:
case Conditional::CONDITION_NOTCONTAINSERRORS:
return Wizard\Errors::fromConditional($conditional, $cellRange);
case Conditional::CONDITION_TIMEPERIOD:
return Wizard\DateValue::fromConditional($conditional, $cellRange);
case Conditional::CONDITION_EXPRESSION:
return Wizard\Expression::fromConditional($conditional, $cellRange);
case Conditional::CONDITION_DUPLICATES:
case Conditional::CONDITION_UNIQUE:
return Wizard\Duplicates::fromConditional($conditional, $cellRange);
default:
throw new Exception('No wizard exists for this CF rule type');
}
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
/**
* @method Blanks notBlank()
* @method Blanks notEmpty()
* @method Blanks isBlank()
* @method Blanks isEmpty()
*/
class Blanks extends WizardAbstract implements WizardInterface
{
protected const OPERATORS = [
'notBlank' => false,
'isBlank' => true,
'notEmpty' => false,
'empty' => true,
];
protected const EXPRESSIONS = [
Wizard::NOT_BLANKS => 'LEN(TRIM(%s))>0',
Wizard::BLANKS => 'LEN(TRIM(%s))=0',
];
/**
* @var bool
*/
protected $inverse;
public function __construct(string $cellRange, bool $inverse = false)
{
parent::__construct($cellRange);
$this->inverse = $inverse;
}
protected function inverse(bool $inverse): void
{
$this->inverse = $inverse;
}
protected function getExpression(): void
{
$this->expression = sprintf(
self::EXPRESSIONS[$this->inverse ? Wizard::BLANKS : Wizard::NOT_BLANKS],
$this->referenceCell
);
}
public function getConditional(): Conditional
{
$this->getExpression();
$conditional = new Conditional();
$conditional->setConditionType(
$this->inverse ? Conditional::CONDITION_CONTAINSBLANKS : Conditional::CONDITION_NOTCONTAINSBLANKS
);
$conditional->setConditions([$this->expression]);
$conditional->setStyle($this->getStyle());
$conditional->setStopIfTrue($this->getStopIfTrue());
return $conditional;
}
public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
{
if (
$conditional->getConditionType() !== Conditional::CONDITION_CONTAINSBLANKS &&
$conditional->getConditionType() !== Conditional::CONDITION_NOTCONTAINSBLANKS
) {
throw new Exception('Conditional is not a Blanks CF Rule conditional');
}
$wizard = new self($cellRange);
$wizard->style = $conditional->getStyle();
$wizard->stopIfTrue = $conditional->getStopIfTrue();
$wizard->inverse = $conditional->getConditionType() === Conditional::CONDITION_CONTAINSBLANKS;
return $wizard;
}
/**
* @param string $methodName
* @param mixed[] $arguments
*/
public function __call($methodName, $arguments): self
{
if (!array_key_exists($methodName, self::OPERATORS)) {
throw new Exception('Invalid Operation for Blanks CF Rule Wizard');
}
$this->inverse(self::OPERATORS[$methodName]);
return $this;
}
}

View File

@@ -0,0 +1,200 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\CellMatcher;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
/**
* @method CellValue equals($value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method CellValue notEquals($value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method CellValue greaterThan($value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method CellValue greaterThanOrEqual($value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method CellValue lessThan($value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method CellValue lessThanOrEqual($value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method CellValue between($value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method CellValue notBetween($value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method CellValue and($value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
*/
class CellValue extends WizardAbstract implements WizardInterface
{
protected const MAGIC_OPERATIONS = [
'equals' => Conditional::OPERATOR_EQUAL,
'notEquals' => Conditional::OPERATOR_NOTEQUAL,
'greaterThan' => Conditional::OPERATOR_GREATERTHAN,
'greaterThanOrEqual' => Conditional::OPERATOR_GREATERTHANOREQUAL,
'lessThan' => Conditional::OPERATOR_LESSTHAN,
'lessThanOrEqual' => Conditional::OPERATOR_LESSTHANOREQUAL,
'between' => Conditional::OPERATOR_BETWEEN,
'notBetween' => Conditional::OPERATOR_NOTBETWEEN,
];
protected const SINGLE_OPERATORS = CellMatcher::COMPARISON_OPERATORS;
protected const RANGE_OPERATORS = CellMatcher::COMPARISON_RANGE_OPERATORS;
/** @var string */
protected $operator = Conditional::OPERATOR_EQUAL;
/** @var array */
protected $operand = [0];
/**
* @var string[]
*/
protected $operandValueType = [];
public function __construct(string $cellRange)
{
parent::__construct($cellRange);
}
protected function operator(string $operator): void
{
if ((!isset(self::SINGLE_OPERATORS[$operator])) && (!isset(self::RANGE_OPERATORS[$operator]))) {
throw new Exception('Invalid Operator for Cell Value CF Rule Wizard');
}
$this->operator = $operator;
}
/**
* @param mixed $operand
*/
protected function operand(int $index, $operand, string $operandValueType = Wizard::VALUE_TYPE_LITERAL): void
{
if (is_string($operand)) {
$operand = $this->validateOperand($operand, $operandValueType);
}
$this->operand[$index] = $operand;
$this->operandValueType[$index] = $operandValueType;
}
/**
* @param mixed $value
*
* @return float|int|string
*/
protected function wrapValue($value, string $operandValueType)
{
if (!is_numeric($value) && !is_bool($value) && null !== $value) {
if ($operandValueType === Wizard::VALUE_TYPE_LITERAL) {
return '"' . str_replace('"', '""', $value) . '"';
}
return $this->cellConditionCheck($value);
}
if (null === $value) {
$value = 'NULL';
} elseif (is_bool($value)) {
$value = $value ? 'TRUE' : 'FALSE';
}
return $value;
}
public function getConditional(): Conditional
{
if (!isset(self::RANGE_OPERATORS[$this->operator])) {
unset($this->operand[1], $this->operandValueType[1]);
}
$values = array_map([$this, 'wrapValue'], $this->operand, $this->operandValueType);
$conditional = new Conditional();
$conditional->setConditionType(Conditional::CONDITION_CELLIS);
$conditional->setOperatorType($this->operator);
$conditional->setConditions($values);
$conditional->setStyle($this->getStyle());
$conditional->setStopIfTrue($this->getStopIfTrue());
return $conditional;
}
protected static function unwrapString(string $condition): string
{
if ((strpos($condition, '"') === 0) && (strpos(strrev($condition), '"') === 0)) {
$condition = substr($condition, 1, -1);
}
return str_replace('""', '"', $condition);
}
public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
{
if ($conditional->getConditionType() !== Conditional::CONDITION_CELLIS) {
throw new Exception('Conditional is not a Cell Value CF Rule conditional');
}
$wizard = new self($cellRange);
$wizard->style = $conditional->getStyle();
$wizard->stopIfTrue = $conditional->getStopIfTrue();
$wizard->operator = $conditional->getOperatorType();
$conditions = $conditional->getConditions();
foreach ($conditions as $index => $condition) {
// Best-guess to try and identify if the text is a string literal, a cell reference or a formula?
$operandValueType = Wizard::VALUE_TYPE_LITERAL;
if (is_string($condition)) {
if (Calculation::keyInExcelConstants($condition)) {
$condition = Calculation::getExcelConstants($condition);
} elseif (preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '$/i', $condition)) {
$operandValueType = Wizard::VALUE_TYPE_CELL;
$condition = self::reverseAdjustCellRef($condition, $cellRange);
} elseif (
preg_match('/\(\)/', $condition) ||
preg_match('/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/i', $condition)
) {
$operandValueType = Wizard::VALUE_TYPE_FORMULA;
$condition = self::reverseAdjustCellRef($condition, $cellRange);
} else {
$condition = self::unwrapString($condition);
}
}
$wizard->operand($index, $condition, $operandValueType);
}
return $wizard;
}
/**
* @param string $methodName
* @param mixed[] $arguments
*/
public function __call($methodName, $arguments): self
{
if (!isset(self::MAGIC_OPERATIONS[$methodName]) && $methodName !== 'and') {
throw new Exception('Invalid Operator for Cell Value CF Rule Wizard');
}
if ($methodName === 'and') {
if (!isset(self::RANGE_OPERATORS[$this->operator])) {
throw new Exception('AND Value is only appropriate for range operators');
}
// Scrutinizer ignores its own suggested workaround.
//$this->operand(1, /** @scrutinizer ignore-type */ ...$arguments);
if (count($arguments) < 2) {
$this->operand(1, $arguments[0]);
} else {
$this->operand(1, $arguments[0], $arguments[1]);
}
return $this;
}
$this->operator(self::MAGIC_OPERATIONS[$methodName]);
//$this->operand(0, ...$arguments);
if (count($arguments) < 2) {
$this->operand(0, $arguments[0]);
} else {
$this->operand(0, $arguments[0], $arguments[1]);
}
return $this;
}
}

View File

@@ -0,0 +1,111 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
/**
* @method DateValue yesterday()
* @method DateValue today()
* @method DateValue tomorrow()
* @method DateValue lastSevenDays()
* @method DateValue lastWeek()
* @method DateValue thisWeek()
* @method DateValue nextWeek()
* @method DateValue lastMonth()
* @method DateValue thisMonth()
* @method DateValue nextMonth()
*/
class DateValue extends WizardAbstract implements WizardInterface
{
protected const MAGIC_OPERATIONS = [
'yesterday' => Conditional::TIMEPERIOD_YESTERDAY,
'today' => Conditional::TIMEPERIOD_TODAY,
'tomorrow' => Conditional::TIMEPERIOD_TOMORROW,
'lastSevenDays' => Conditional::TIMEPERIOD_LAST_7_DAYS,
'last7Days' => Conditional::TIMEPERIOD_LAST_7_DAYS,
'lastWeek' => Conditional::TIMEPERIOD_LAST_WEEK,
'thisWeek' => Conditional::TIMEPERIOD_THIS_WEEK,
'nextWeek' => Conditional::TIMEPERIOD_NEXT_WEEK,
'lastMonth' => Conditional::TIMEPERIOD_LAST_MONTH,
'thisMonth' => Conditional::TIMEPERIOD_THIS_MONTH,
'nextMonth' => Conditional::TIMEPERIOD_NEXT_MONTH,
];
protected const EXPRESSIONS = [
Conditional::TIMEPERIOD_YESTERDAY => 'FLOOR(%s,1)=TODAY()-1',
Conditional::TIMEPERIOD_TODAY => 'FLOOR(%s,1)=TODAY()',
Conditional::TIMEPERIOD_TOMORROW => 'FLOOR(%s,1)=TODAY()+1',
Conditional::TIMEPERIOD_LAST_7_DAYS => 'AND(TODAY()-FLOOR(%s,1)<=6,FLOOR(%s,1)<=TODAY())',
Conditional::TIMEPERIOD_LAST_WEEK => 'AND(TODAY()-ROUNDDOWN(%s,0)>=(WEEKDAY(TODAY())),TODAY()-ROUNDDOWN(%s,0)<(WEEKDAY(TODAY())+7))',
Conditional::TIMEPERIOD_THIS_WEEK => 'AND(TODAY()-ROUNDDOWN(%s,0)<=WEEKDAY(TODAY())-1,ROUNDDOWN(%s,0)-TODAY()<=7-WEEKDAY(TODAY()))',
Conditional::TIMEPERIOD_NEXT_WEEK => 'AND(ROUNDDOWN(%s,0)-TODAY()>(7-WEEKDAY(TODAY())),ROUNDDOWN(%s,0)-TODAY()<(15-WEEKDAY(TODAY())))',
Conditional::TIMEPERIOD_LAST_MONTH => 'AND(MONTH(%s)=MONTH(EDATE(TODAY(),0-1)),YEAR(%s)=YEAR(EDATE(TODAY(),0-1)))',
Conditional::TIMEPERIOD_THIS_MONTH => 'AND(MONTH(%s)=MONTH(TODAY()),YEAR(%s)=YEAR(TODAY()))',
Conditional::TIMEPERIOD_NEXT_MONTH => 'AND(MONTH(%s)=MONTH(EDATE(TODAY(),0+1)),YEAR(%s)=YEAR(EDATE(TODAY(),0+1)))',
];
/** @var string */
protected $operator;
public function __construct(string $cellRange)
{
parent::__construct($cellRange);
}
protected function operator(string $operator): void
{
$this->operator = $operator;
}
protected function setExpression(): void
{
$referenceCount = substr_count(self::EXPRESSIONS[$this->operator], '%s');
$references = array_fill(0, $referenceCount, $this->referenceCell);
$this->expression = sprintf(self::EXPRESSIONS[$this->operator], ...$references);
}
public function getConditional(): Conditional
{
$this->setExpression();
$conditional = new Conditional();
$conditional->setConditionType(Conditional::CONDITION_TIMEPERIOD);
$conditional->setText($this->operator);
$conditional->setConditions([$this->expression]);
$conditional->setStyle($this->getStyle());
$conditional->setStopIfTrue($this->getStopIfTrue());
return $conditional;
}
public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
{
if ($conditional->getConditionType() !== Conditional::CONDITION_TIMEPERIOD) {
throw new Exception('Conditional is not a Date Value CF Rule conditional');
}
$wizard = new self($cellRange);
$wizard->style = $conditional->getStyle();
$wizard->stopIfTrue = $conditional->getStopIfTrue();
$wizard->operator = $conditional->getText();
return $wizard;
}
/**
* @param string $methodName
* @param mixed[] $arguments
*/
public function __call($methodName, $arguments): self
{
if (!isset(self::MAGIC_OPERATIONS[$methodName])) {
throw new Exception('Invalid Operation for Date Value CF Rule Wizard');
}
$this->operator(self::MAGIC_OPERATIONS[$methodName]);
return $this;
}
}

View File

@@ -0,0 +1,78 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
/**
* @method Errors duplicates()
* @method Errors unique()
*/
class Duplicates extends WizardAbstract implements WizardInterface
{
protected const OPERATORS = [
'duplicates' => false,
'unique' => true,
];
/**
* @var bool
*/
protected $inverse;
public function __construct(string $cellRange, bool $inverse = false)
{
parent::__construct($cellRange);
$this->inverse = $inverse;
}
protected function inverse(bool $inverse): void
{
$this->inverse = $inverse;
}
public function getConditional(): Conditional
{
$conditional = new Conditional();
$conditional->setConditionType(
$this->inverse ? Conditional::CONDITION_UNIQUE : Conditional::CONDITION_DUPLICATES
);
$conditional->setStyle($this->getStyle());
$conditional->setStopIfTrue($this->getStopIfTrue());
return $conditional;
}
public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
{
if (
$conditional->getConditionType() !== Conditional::CONDITION_DUPLICATES &&
$conditional->getConditionType() !== Conditional::CONDITION_UNIQUE
) {
throw new Exception('Conditional is not a Duplicates CF Rule conditional');
}
$wizard = new self($cellRange);
$wizard->style = $conditional->getStyle();
$wizard->stopIfTrue = $conditional->getStopIfTrue();
$wizard->inverse = $conditional->getConditionType() === Conditional::CONDITION_UNIQUE;
return $wizard;
}
/**
* @param string $methodName
* @param mixed[] $arguments
*/
public function __call($methodName, $arguments): self
{
if (!array_key_exists($methodName, self::OPERATORS)) {
throw new Exception('Invalid Operation for Errors CF Rule Wizard');
}
$this->inverse(self::OPERATORS[$methodName]);
return $this;
}
}

View File

@@ -0,0 +1,95 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
/**
* @method Errors notError()
* @method Errors isError()
*/
class Errors extends WizardAbstract implements WizardInterface
{
protected const OPERATORS = [
'notError' => false,
'isError' => true,
];
protected const EXPRESSIONS = [
Wizard::NOT_ERRORS => 'NOT(ISERROR(%s))',
Wizard::ERRORS => 'ISERROR(%s)',
];
/**
* @var bool
*/
protected $inverse;
public function __construct(string $cellRange, bool $inverse = false)
{
parent::__construct($cellRange);
$this->inverse = $inverse;
}
protected function inverse(bool $inverse): void
{
$this->inverse = $inverse;
}
protected function getExpression(): void
{
$this->expression = sprintf(
self::EXPRESSIONS[$this->inverse ? Wizard::ERRORS : Wizard::NOT_ERRORS],
$this->referenceCell
);
}
public function getConditional(): Conditional
{
$this->getExpression();
$conditional = new Conditional();
$conditional->setConditionType(
$this->inverse ? Conditional::CONDITION_CONTAINSERRORS : Conditional::CONDITION_NOTCONTAINSERRORS
);
$conditional->setConditions([$this->expression]);
$conditional->setStyle($this->getStyle());
$conditional->setStopIfTrue($this->getStopIfTrue());
return $conditional;
}
public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
{
if (
$conditional->getConditionType() !== Conditional::CONDITION_CONTAINSERRORS &&
$conditional->getConditionType() !== Conditional::CONDITION_NOTCONTAINSERRORS
) {
throw new Exception('Conditional is not an Errors CF Rule conditional');
}
$wizard = new self($cellRange);
$wizard->style = $conditional->getStyle();
$wizard->stopIfTrue = $conditional->getStopIfTrue();
$wizard->inverse = $conditional->getConditionType() === Conditional::CONDITION_CONTAINSERRORS;
return $wizard;
}
/**
* @param string $methodName
* @param mixed[] $arguments
*/
public function __call($methodName, $arguments): self
{
if (!array_key_exists($methodName, self::OPERATORS)) {
throw new Exception('Invalid Operation for Errors CF Rule Wizard');
}
$this->inverse(self::OPERATORS[$methodName]);
return $this;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
/**
* @method Expression formula(string $expression)
*/
class Expression extends WizardAbstract implements WizardInterface
{
/**
* @var string
*/
protected $expression;
public function __construct(string $cellRange)
{
parent::__construct($cellRange);
}
public function expression(string $expression): self
{
$expression = $this->validateOperand($expression, Wizard::VALUE_TYPE_FORMULA);
$this->expression = $expression;
return $this;
}
public function getConditional(): Conditional
{
$expression = $this->adjustConditionsForCellReferences([$this->expression]);
$conditional = new Conditional();
$conditional->setConditionType(Conditional::CONDITION_EXPRESSION);
$conditional->setConditions($expression);
$conditional->setStyle($this->getStyle());
$conditional->setStopIfTrue($this->getStopIfTrue());
return $conditional;
}
public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
{
if ($conditional->getConditionType() !== Conditional::CONDITION_EXPRESSION) {
throw new Exception('Conditional is not an Expression CF Rule conditional');
}
$wizard = new self($cellRange);
$wizard->style = $conditional->getStyle();
$wizard->stopIfTrue = $conditional->getStopIfTrue();
$wizard->expression = self::reverseAdjustCellRef((string) ($conditional->getConditions()[0]), $cellRange);
return $wizard;
}
/**
* @param string $methodName
* @param mixed[] $arguments
*/
public function __call($methodName, $arguments): self
{
if ($methodName !== 'formula') {
throw new Exception('Invalid Operation for Expression CF Rule Wizard');
}
// Scrutinizer ignores its own recommendation
//$this->expression(/** @scrutinizer ignore-type */ ...$arguments);
$this->expression($arguments[0]);
return $this;
}
}

View File

@@ -0,0 +1,164 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
/**
* @method TextValue contains(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method TextValue doesNotContain(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method TextValue doesntContain(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method TextValue beginsWith(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method TextValue startsWith(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
* @method TextValue endsWith(string $value, string $operandValueType = Wizard::VALUE_TYPE_LITERAL)
*/
class TextValue extends WizardAbstract implements WizardInterface
{
protected const MAGIC_OPERATIONS = [
'contains' => Conditional::OPERATOR_CONTAINSTEXT,
'doesntContain' => Conditional::OPERATOR_NOTCONTAINS,
'doesNotContain' => Conditional::OPERATOR_NOTCONTAINS,
'beginsWith' => Conditional::OPERATOR_BEGINSWITH,
'startsWith' => Conditional::OPERATOR_BEGINSWITH,
'endsWith' => Conditional::OPERATOR_ENDSWITH,
];
protected const OPERATORS = [
Conditional::OPERATOR_CONTAINSTEXT => Conditional::CONDITION_CONTAINSTEXT,
Conditional::OPERATOR_NOTCONTAINS => Conditional::CONDITION_NOTCONTAINSTEXT,
Conditional::OPERATOR_BEGINSWITH => Conditional::CONDITION_BEGINSWITH,
Conditional::OPERATOR_ENDSWITH => Conditional::CONDITION_ENDSWITH,
];
protected const EXPRESSIONS = [
Conditional::OPERATOR_CONTAINSTEXT => 'NOT(ISERROR(SEARCH(%s,%s)))',
Conditional::OPERATOR_NOTCONTAINS => 'ISERROR(SEARCH(%s,%s))',
Conditional::OPERATOR_BEGINSWITH => 'LEFT(%s,LEN(%s))=%s',
Conditional::OPERATOR_ENDSWITH => 'RIGHT(%s,LEN(%s))=%s',
];
/** @var string */
protected $operator;
/** @var string */
protected $operand;
/**
* @var string
*/
protected $operandValueType;
public function __construct(string $cellRange)
{
parent::__construct($cellRange);
}
protected function operator(string $operator): void
{
if (!isset(self::OPERATORS[$operator])) {
throw new Exception('Invalid Operator for Text Value CF Rule Wizard');
}
$this->operator = $operator;
}
protected function operand(string $operand, string $operandValueType = Wizard::VALUE_TYPE_LITERAL): void
{
$operand = $this->validateOperand($operand, $operandValueType);
$this->operand = $operand;
$this->operandValueType = $operandValueType;
}
protected function wrapValue(string $value): string
{
return '"' . $value . '"';
}
protected function setExpression(): void
{
$operand = $this->operandValueType === Wizard::VALUE_TYPE_LITERAL
? $this->wrapValue(str_replace('"', '""', $this->operand))
: $this->cellConditionCheck($this->operand);
if (
$this->operator === Conditional::OPERATOR_CONTAINSTEXT ||
$this->operator === Conditional::OPERATOR_NOTCONTAINS
) {
$this->expression = sprintf(self::EXPRESSIONS[$this->operator], $operand, $this->referenceCell);
} else {
$this->expression = sprintf(self::EXPRESSIONS[$this->operator], $this->referenceCell, $operand, $operand);
}
}
public function getConditional(): Conditional
{
$this->setExpression();
$conditional = new Conditional();
$conditional->setConditionType(self::OPERATORS[$this->operator]);
$conditional->setOperatorType($this->operator);
$conditional->setText(
$this->operandValueType !== Wizard::VALUE_TYPE_LITERAL
? $this->cellConditionCheck($this->operand)
: $this->operand
);
$conditional->setConditions([$this->expression]);
$conditional->setStyle($this->getStyle());
$conditional->setStopIfTrue($this->getStopIfTrue());
return $conditional;
}
public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): WizardInterface
{
if (!in_array($conditional->getConditionType(), self::OPERATORS, true)) {
throw new Exception('Conditional is not a Text Value CF Rule conditional');
}
$wizard = new self($cellRange);
$wizard->operator = (string) array_search($conditional->getConditionType(), self::OPERATORS, true);
$wizard->style = $conditional->getStyle();
$wizard->stopIfTrue = $conditional->getStopIfTrue();
// Best-guess to try and identify if the text is a string literal, a cell reference or a formula?
$wizard->operandValueType = Wizard::VALUE_TYPE_LITERAL;
$condition = $conditional->getText();
if (preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '$/i', $condition)) {
$wizard->operandValueType = Wizard::VALUE_TYPE_CELL;
$condition = self::reverseAdjustCellRef($condition, $cellRange);
} elseif (
preg_match('/\(\)/', $condition) ||
preg_match('/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/i', $condition)
) {
$wizard->operandValueType = Wizard::VALUE_TYPE_FORMULA;
}
$wizard->operand = $condition;
return $wizard;
}
/**
* @param string $methodName
* @param mixed[] $arguments
*/
public function __call($methodName, $arguments): self
{
if (!isset(self::MAGIC_OPERATIONS[$methodName])) {
throw new Exception('Invalid Operation for Text Value CF Rule Wizard');
}
$this->operator(self::MAGIC_OPERATIONS[$methodName]);
//$this->operand(...$arguments);
if (count($arguments) < 2) {
$this->operand($arguments[0]);
} else {
$this->operand($arguments[0], $arguments[1]);
}
return $this;
}
}

View File

@@ -0,0 +1,199 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Style\Style;
abstract class WizardAbstract
{
/**
* @var ?Style
*/
protected $style;
/**
* @var string
*/
protected $expression;
/**
* @var string
*/
protected $cellRange;
/**
* @var string
*/
protected $referenceCell;
/**
* @var int
*/
protected $referenceRow;
/**
* @var bool
*/
protected $stopIfTrue = false;
/**
* @var int
*/
protected $referenceColumn;
public function __construct(string $cellRange)
{
$this->setCellRange($cellRange);
}
public function getCellRange(): string
{
return $this->cellRange;
}
public function setCellRange(string $cellRange): void
{
$this->cellRange = $cellRange;
$this->setReferenceCellForExpressions($cellRange);
}
protected function setReferenceCellForExpressions(string $conditionalRange): void
{
$conditionalRange = Coordinate::splitRange(str_replace('$', '', strtoupper($conditionalRange)));
[$this->referenceCell] = $conditionalRange[0];
[$this->referenceColumn, $this->referenceRow] = Coordinate::indexesFromString($this->referenceCell);
}
public function getStopIfTrue(): bool
{
return $this->stopIfTrue;
}
public function setStopIfTrue(bool $stopIfTrue): void
{
$this->stopIfTrue = $stopIfTrue;
}
public function getStyle(): Style
{
return $this->style ?? new Style(false, true);
}
public function setStyle(Style $style): void
{
$this->style = $style;
}
protected function validateOperand(string $operand, string $operandValueType = Wizard::VALUE_TYPE_LITERAL): string
{
if (
$operandValueType === Wizard::VALUE_TYPE_LITERAL &&
substr($operand, 0, 1) === '"' &&
substr($operand, -1) === '"'
) {
$operand = str_replace('""', '"', substr($operand, 1, -1));
} elseif ($operandValueType === Wizard::VALUE_TYPE_FORMULA && substr($operand, 0, 1) === '=') {
$operand = substr($operand, 1);
}
return $operand;
}
protected static function reverseCellAdjustment(array $matches, int $referenceColumn, int $referenceRow): string
{
$worksheet = $matches[1];
$column = $matches[6];
$row = $matches[7];
if (strpos($column, '$') === false) {
$column = Coordinate::columnIndexFromString($column);
$column -= $referenceColumn - 1;
$column = Coordinate::stringFromColumnIndex($column);
}
if (strpos($row, '$') === false) {
$row -= $referenceRow - 1;
}
return "{$worksheet}{$column}{$row}";
}
public static function reverseAdjustCellRef(string $condition, string $cellRange): string
{
$conditionalRange = Coordinate::splitRange(str_replace('$', '', strtoupper($cellRange)));
[$referenceCell] = $conditionalRange[0];
[$referenceColumnIndex, $referenceRow] = Coordinate::indexesFromString($referenceCell);
$splitCondition = explode(Calculation::FORMULA_STRING_QUOTE, $condition);
$i = false;
foreach ($splitCondition as &$value) {
// Only count/replace in alternating array entries (ie. not in quoted strings)
$i = $i === false;
if ($i) {
$value = (string) preg_replace_callback(
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/i',
function ($matches) use ($referenceColumnIndex, $referenceRow) {
return self::reverseCellAdjustment($matches, $referenceColumnIndex, $referenceRow);
},
$value
);
}
}
unset($value);
// Then rebuild the condition string to return it
return implode(Calculation::FORMULA_STRING_QUOTE, $splitCondition);
}
protected function conditionCellAdjustment(array $matches): string
{
$worksheet = $matches[1];
$column = $matches[6];
$row = $matches[7];
if (strpos($column, '$') === false) {
$column = Coordinate::columnIndexFromString($column);
$column += $this->referenceColumn - 1;
$column = Coordinate::stringFromColumnIndex($column);
}
if (strpos($row, '$') === false) {
$row += $this->referenceRow - 1;
}
return "{$worksheet}{$column}{$row}";
}
protected function cellConditionCheck(string $condition): string
{
$splitCondition = explode(Calculation::FORMULA_STRING_QUOTE, $condition);
$i = false;
foreach ($splitCondition as &$value) {
// Only count/replace in alternating array entries (ie. not in quoted strings)
$i = $i === false;
if ($i) {
$value = (string) preg_replace_callback(
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/i',
[$this, 'conditionCellAdjustment'],
$value
);
}
}
unset($value);
// Then rebuild the condition string to return it
return implode(Calculation::FORMULA_STRING_QUOTE, $splitCondition);
}
protected function adjustConditionsForCellReferences(array $conditions): array
{
return array_map(
[$this, 'cellConditionCheck'],
$conditions
);
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
use PhpOffice\PhpSpreadsheet\Style\Style;
interface WizardInterface
{
public function getCellRange(): string;
public function setCellRange(string $cellRange): void;
public function getStyle(): Style;
public function setStyle(Style $style): void;
public function getStopIfTrue(): bool;
public function setStopIfTrue(bool $stopIfTrue): void;
public function getConditional(): Conditional;
public static function fromConditional(Conditional $conditional, string $cellRange = 'A1'): self;
}

View File

@@ -0,0 +1,347 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
class Fill extends Supervisor
{
// Fill types
const FILL_NONE = 'none';
const FILL_SOLID = 'solid';
const FILL_GRADIENT_LINEAR = 'linear';
const FILL_GRADIENT_PATH = 'path';
const FILL_PATTERN_DARKDOWN = 'darkDown';
const FILL_PATTERN_DARKGRAY = 'darkGray';
const FILL_PATTERN_DARKGRID = 'darkGrid';
const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
const FILL_PATTERN_DARKUP = 'darkUp';
const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
const FILL_PATTERN_GRAY0625 = 'gray0625';
const FILL_PATTERN_GRAY125 = 'gray125';
const FILL_PATTERN_LIGHTDOWN = 'lightDown';
const FILL_PATTERN_LIGHTGRAY = 'lightGray';
const FILL_PATTERN_LIGHTGRID = 'lightGrid';
const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
const FILL_PATTERN_LIGHTUP = 'lightUp';
const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
/**
* @var null|int
*/
public $startcolorIndex;
/**
* @var null|int
*/
public $endcolorIndex;
/**
* Fill type.
*
* @var null|string
*/
protected $fillType = self::FILL_NONE;
/**
* Rotation.
*
* @var float
*/
protected $rotation = 0.0;
/**
* Start color.
*
* @var Color
*/
protected $startColor;
/**
* End color.
*
* @var Color
*/
protected $endColor;
/** @var bool */
private $colorChanged = false;
/**
* Create a new Fill.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
if ($isConditional) {
$this->fillType = null;
}
$this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
$this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
// bind parent if we are a supervisor
if ($isSupervisor) {
$this->startColor->bindParent($this, 'startColor');
$this->endColor->bindParent($this, 'endColor');
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Fill
*/
public function getSharedComponent()
{
/** @var Style */
$parent = $this->parent;
return $parent->getSharedComponent()->getFill();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['fill' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
* [
* 'fillType' => Fill::FILL_GRADIENT_LINEAR,
* 'rotation' => 0.0,
* 'startColor' => [
* 'rgb' => '000000'
* ],
* 'endColor' => [
* 'argb' => 'FFFFFFFF'
* ]
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['fillType'])) {
$this->setFillType($styleArray['fillType']);
}
if (isset($styleArray['rotation'])) {
$this->setRotation($styleArray['rotation']);
}
if (isset($styleArray['startColor'])) {
$this->getStartColor()->applyFromArray($styleArray['startColor']);
}
if (isset($styleArray['endColor'])) {
$this->getEndColor()->applyFromArray($styleArray['endColor']);
}
if (isset($styleArray['color'])) {
$this->getStartColor()->applyFromArray($styleArray['color']);
$this->getEndColor()->applyFromArray($styleArray['color']);
}
}
return $this;
}
/**
* Get Fill Type.
*
* @return null|string
*/
public function getFillType()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getFillType();
}
return $this->fillType;
}
/**
* Set Fill Type.
*
* @param string $fillType Fill type, see self::FILL_*
*
* @return $this
*/
public function setFillType($fillType)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['fillType' => $fillType]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->fillType = $fillType;
}
return $this;
}
/**
* Get Rotation.
*
* @return float
*/
public function getRotation()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getRotation();
}
return $this->rotation;
}
/**
* Set Rotation.
*
* @param float $angleInDegrees
*
* @return $this
*/
public function setRotation($angleInDegrees)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['rotation' => $angleInDegrees]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->rotation = $angleInDegrees;
}
return $this;
}
/**
* Get Start Color.
*
* @return Color
*/
public function getStartColor()
{
return $this->startColor;
}
/**
* Set Start Color.
*
* @return $this
*/
public function setStartColor(Color $color)
{
$this->colorChanged = true;
// make sure parameter is a real color and not a supervisor
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
if ($this->isSupervisor) {
$styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->startColor = $color;
}
return $this;
}
/**
* Get End Color.
*
* @return Color
*/
public function getEndColor()
{
return $this->endColor;
}
/**
* Set End Color.
*
* @return $this
*/
public function setEndColor(Color $color)
{
$this->colorChanged = true;
// make sure parameter is a real color and not a supervisor
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
if ($this->isSupervisor) {
$styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->endColor = $color;
}
return $this;
}
public function getColorsChanged(): bool
{
if ($this->isSupervisor) {
$changed = $this->getSharedComponent()->colorChanged;
} else {
$changed = $this->colorChanged;
}
return $changed || $this->startColor->getHasChanged() || $this->endColor->getHasChanged();
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
// Note that we don't care about colours for fill type NONE, but could have duplicate NONEs with
// different hashes if we don't explicitly prevent this
return md5(
$this->getFillType() .
$this->getRotation() .
($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '') .
($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '') .
((string) $this->getColorsChanged()) .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'fillType', $this->getFillType());
$this->exportArray2($exportedArray, 'rotation', $this->getRotation());
if ($this->getColorsChanged()) {
$this->exportArray2($exportedArray, 'endColor', $this->getEndColor());
$this->exportArray2($exportedArray, 'startColor', $this->getStartColor());
}
return $exportedArray;
}
}

View File

@@ -0,0 +1,854 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
class Font extends Supervisor
{
// Underline types
const UNDERLINE_NONE = 'none';
const UNDERLINE_DOUBLE = 'double';
const UNDERLINE_DOUBLEACCOUNTING = 'doubleAccounting';
const UNDERLINE_SINGLE = 'single';
const UNDERLINE_SINGLEACCOUNTING = 'singleAccounting';
/**
* Font Name.
*
* @var null|string
*/
protected $name = 'Calibri';
/**
* The following 7 are used only for chart titles, I think.
*
*@var string
*/
private $latin = '';
/** @var string */
private $eastAsian = '';
/** @var string */
private $complexScript = '';
/** @var int */
private $baseLine = 0;
/** @var string */
private $strikeType = '';
/** @var ?ChartColor */
private $underlineColor;
/** @var ?ChartColor */
private $chartColor;
// end of chart title items
/**
* Font Size.
*
* @var null|float
*/
protected $size = 11;
/**
* Bold.
*
* @var null|bool
*/
protected $bold = false;
/**
* Italic.
*
* @var null|bool
*/
protected $italic = false;
/**
* Superscript.
*
* @var null|bool
*/
protected $superscript = false;
/**
* Subscript.
*
* @var null|bool
*/
protected $subscript = false;
/**
* Underline.
*
* @var null|string
*/
protected $underline = self::UNDERLINE_NONE;
/**
* Strikethrough.
*
* @var null|bool
*/
protected $strikethrough = false;
/**
* Foreground color.
*
* @var Color
*/
protected $color;
/**
* @var null|int
*/
public $colorIndex;
/** @var string */
protected $scheme = '';
/**
* Create a new Font.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
if ($isConditional) {
$this->name = null;
$this->size = null;
$this->bold = null;
$this->italic = null;
$this->superscript = null;
$this->subscript = null;
$this->underline = null;
$this->strikethrough = null;
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
} else {
$this->color = new Color(Color::COLOR_BLACK, $isSupervisor);
}
// bind parent if we are a supervisor
if ($isSupervisor) {
$this->color->bindParent($this, 'color');
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Font
*/
public function getSharedComponent()
{
/** @var Style */
$parent = $this->parent;
return $parent->getSharedComponent()->getFont();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['font' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->applyFromArray(
* [
* 'name' => 'Arial',
* 'bold' => TRUE,
* 'italic' => FALSE,
* 'underline' => \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE,
* 'strikethrough' => FALSE,
* 'color' => [
* 'rgb' => '808080'
* ]
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['name'])) {
$this->setName($styleArray['name']);
}
if (isset($styleArray['latin'])) {
$this->setLatin($styleArray['latin']);
}
if (isset($styleArray['eastAsian'])) {
$this->setEastAsian($styleArray['eastAsian']);
}
if (isset($styleArray['complexScript'])) {
$this->setComplexScript($styleArray['complexScript']);
}
if (isset($styleArray['bold'])) {
$this->setBold($styleArray['bold']);
}
if (isset($styleArray['italic'])) {
$this->setItalic($styleArray['italic']);
}
if (isset($styleArray['superscript'])) {
$this->setSuperscript($styleArray['superscript']);
}
if (isset($styleArray['subscript'])) {
$this->setSubscript($styleArray['subscript']);
}
if (isset($styleArray['underline'])) {
$this->setUnderline($styleArray['underline']);
}
if (isset($styleArray['strikethrough'])) {
$this->setStrikethrough($styleArray['strikethrough']);
}
if (isset($styleArray['color'])) {
$this->getColor()->applyFromArray($styleArray['color']);
}
if (isset($styleArray['size'])) {
$this->setSize($styleArray['size']);
}
if (isset($styleArray['chartColor'])) {
$this->chartColor = $styleArray['chartColor'];
}
if (isset($styleArray['scheme'])) {
$this->setScheme($styleArray['scheme']);
}
}
return $this;
}
/**
* Get Name.
*
* @return null|string
*/
public function getName()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getName();
}
return $this->name;
}
public function getLatin(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getLatin();
}
return $this->latin;
}
public function getEastAsian(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getEastAsian();
}
return $this->eastAsian;
}
public function getComplexScript(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getComplexScript();
}
return $this->complexScript;
}
/**
* Set Name and turn off Scheme.
*
* @param string $fontname
*/
public function setName($fontname): self
{
if ($fontname == '') {
$fontname = 'Calibri';
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['name' => $fontname]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->name = $fontname;
}
return $this->setScheme('');
}
public function setLatin(string $fontname): self
{
if ($fontname == '') {
$fontname = 'Calibri';
}
if (!$this->isSupervisor) {
$this->latin = $fontname;
} else {
// should never be true
// @codeCoverageIgnoreStart
$styleArray = $this->getStyleArray(['latin' => $fontname]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
// @codeCoverageIgnoreEnd
}
return $this;
}
public function setEastAsian(string $fontname): self
{
if ($fontname == '') {
$fontname = 'Calibri';
}
if (!$this->isSupervisor) {
$this->eastAsian = $fontname;
} else {
// should never be true
// @codeCoverageIgnoreStart
$styleArray = $this->getStyleArray(['eastAsian' => $fontname]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
// @codeCoverageIgnoreEnd
}
return $this;
}
public function setComplexScript(string $fontname): self
{
if ($fontname == '') {
$fontname = 'Calibri';
}
if (!$this->isSupervisor) {
$this->complexScript = $fontname;
} else {
// should never be true
// @codeCoverageIgnoreStart
$styleArray = $this->getStyleArray(['complexScript' => $fontname]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
// @codeCoverageIgnoreEnd
}
return $this;
}
/**
* Get Size.
*
* @return null|float
*/
public function getSize()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getSize();
}
return $this->size;
}
/**
* Set Size.
*
* @param mixed $sizeInPoints A float representing the value of a positive measurement in points (1/72 of an inch)
*
* @return $this
*/
public function setSize($sizeInPoints, bool $nullOk = false)
{
if (is_string($sizeInPoints) || is_int($sizeInPoints)) {
$sizeInPoints = (float) $sizeInPoints; // $pValue = 0 if given string is not numeric
}
// Size must be a positive floating point number
// ECMA-376-1:2016, part 1, chapter 18.4.11 sz (Font Size), p. 1536
if (!is_float($sizeInPoints) || !($sizeInPoints > 0)) {
if (!$nullOk || $sizeInPoints !== null) {
$sizeInPoints = 10.0;
}
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['size' => $sizeInPoints]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->size = $sizeInPoints;
}
return $this;
}
/**
* Get Bold.
*
* @return null|bool
*/
public function getBold()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getBold();
}
return $this->bold;
}
/**
* Set Bold.
*
* @param bool $bold
*
* @return $this
*/
public function setBold($bold)
{
if ($bold == '') {
$bold = false;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['bold' => $bold]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->bold = $bold;
}
return $this;
}
/**
* Get Italic.
*
* @return null|bool
*/
public function getItalic()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getItalic();
}
return $this->italic;
}
/**
* Set Italic.
*
* @param bool $italic
*
* @return $this
*/
public function setItalic($italic)
{
if ($italic == '') {
$italic = false;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['italic' => $italic]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->italic = $italic;
}
return $this;
}
/**
* Get Superscript.
*
* @return null|bool
*/
public function getSuperscript()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getSuperscript();
}
return $this->superscript;
}
/**
* Set Superscript.
*
* @return $this
*/
public function setSuperscript(bool $superscript)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['superscript' => $superscript]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->superscript = $superscript;
if ($this->superscript) {
$this->subscript = false;
}
}
return $this;
}
/**
* Get Subscript.
*
* @return null|bool
*/
public function getSubscript()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getSubscript();
}
return $this->subscript;
}
/**
* Set Subscript.
*
* @return $this
*/
public function setSubscript(bool $subscript)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['subscript' => $subscript]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->subscript = $subscript;
if ($this->subscript) {
$this->superscript = false;
}
}
return $this;
}
public function getBaseLine(): int
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getBaseLine();
}
return $this->baseLine;
}
public function setBaseLine(int $baseLine): self
{
if (!$this->isSupervisor) {
$this->baseLine = $baseLine;
} else {
// should never be true
// @codeCoverageIgnoreStart
$styleArray = $this->getStyleArray(['baseLine' => $baseLine]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
// @codeCoverageIgnoreEnd
}
return $this;
}
public function getStrikeType(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getStrikeType();
}
return $this->strikeType;
}
public function setStrikeType(string $strikeType): self
{
if (!$this->isSupervisor) {
$this->strikeType = $strikeType;
} else {
// should never be true
// @codeCoverageIgnoreStart
$styleArray = $this->getStyleArray(['strikeType' => $strikeType]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
// @codeCoverageIgnoreEnd
}
return $this;
}
public function getUnderlineColor(): ?ChartColor
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getUnderlineColor();
}
return $this->underlineColor;
}
public function setUnderlineColor(array $colorArray): self
{
if (!$this->isSupervisor) {
$this->underlineColor = new ChartColor($colorArray);
} else {
// should never be true
// @codeCoverageIgnoreStart
$styleArray = $this->getStyleArray(['underlineColor' => $colorArray]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
// @codeCoverageIgnoreEnd
}
return $this;
}
public function getChartColor(): ?ChartColor
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getChartColor();
}
return $this->chartColor;
}
public function setChartColor(array $colorArray): self
{
if (!$this->isSupervisor) {
$this->chartColor = new ChartColor($colorArray);
} else {
// should never be true
// @codeCoverageIgnoreStart
$styleArray = $this->getStyleArray(['chartColor' => $colorArray]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
// @codeCoverageIgnoreEnd
}
return $this;
}
public function setChartColorFromObject(?ChartColor $chartColor): self
{
$this->chartColor = $chartColor;
return $this;
}
/**
* Get Underline.
*
* @return null|string
*/
public function getUnderline()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getUnderline();
}
return $this->underline;
}
/**
* Set Underline.
*
* @param bool|string $underlineStyle \PhpOffice\PhpSpreadsheet\Style\Font underline type
* If a boolean is passed, then TRUE equates to UNDERLINE_SINGLE,
* false equates to UNDERLINE_NONE
*
* @return $this
*/
public function setUnderline($underlineStyle)
{
if (is_bool($underlineStyle)) {
$underlineStyle = ($underlineStyle) ? self::UNDERLINE_SINGLE : self::UNDERLINE_NONE;
} elseif ($underlineStyle == '') {
$underlineStyle = self::UNDERLINE_NONE;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['underline' => $underlineStyle]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->underline = $underlineStyle;
}
return $this;
}
/**
* Get Strikethrough.
*
* @return null|bool
*/
public function getStrikethrough()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getStrikethrough();
}
return $this->strikethrough;
}
/**
* Set Strikethrough.
*
* @param bool $strikethru
*
* @return $this
*/
public function setStrikethrough($strikethru)
{
if ($strikethru == '') {
$strikethru = false;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['strikethrough' => $strikethru]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->strikethrough = $strikethru;
}
return $this;
}
/**
* Get Color.
*
* @return Color
*/
public function getColor()
{
return $this->color;
}
/**
* Set Color.
*
* @return $this
*/
public function setColor(Color $color)
{
// make sure parameter is a real color and not a supervisor
$color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
if ($this->isSupervisor) {
$styleArray = $this->getColor()->getStyleArray(['argb' => $color->getARGB()]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->color = $color;
}
return $this;
}
private function hashChartColor(?ChartColor $underlineColor): string
{
if ($underlineColor === null) {
return '';
}
return
$underlineColor->getValue()
. $underlineColor->getType()
. (string) $underlineColor->getAlpha();
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->name .
$this->size .
($this->bold ? 't' : 'f') .
($this->italic ? 't' : 'f') .
($this->superscript ? 't' : 'f') .
($this->subscript ? 't' : 'f') .
$this->underline .
($this->strikethrough ? 't' : 'f') .
$this->color->getHashCode() .
$this->scheme .
implode(
'*',
[
$this->latin,
$this->eastAsian,
$this->complexScript,
$this->strikeType,
$this->hashChartColor($this->chartColor),
$this->hashChartColor($this->underlineColor),
(string) $this->baseLine,
]
) .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'baseLine', $this->getBaseLine());
$this->exportArray2($exportedArray, 'bold', $this->getBold());
$this->exportArray2($exportedArray, 'chartColor', $this->getChartColor());
$this->exportArray2($exportedArray, 'color', $this->getColor());
$this->exportArray2($exportedArray, 'complexScript', $this->getComplexScript());
$this->exportArray2($exportedArray, 'eastAsian', $this->getEastAsian());
$this->exportArray2($exportedArray, 'italic', $this->getItalic());
$this->exportArray2($exportedArray, 'latin', $this->getLatin());
$this->exportArray2($exportedArray, 'name', $this->getName());
$this->exportArray2($exportedArray, 'scheme', $this->getScheme());
$this->exportArray2($exportedArray, 'size', $this->getSize());
$this->exportArray2($exportedArray, 'strikethrough', $this->getStrikethrough());
$this->exportArray2($exportedArray, 'strikeType', $this->getStrikeType());
$this->exportArray2($exportedArray, 'subscript', $this->getSubscript());
$this->exportArray2($exportedArray, 'superscript', $this->getSuperscript());
$this->exportArray2($exportedArray, 'underline', $this->getUnderline());
$this->exportArray2($exportedArray, 'underlineColor', $this->getUnderlineColor());
return $exportedArray;
}
public function getScheme(): string
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getScheme();
}
return $this->scheme;
}
public function setScheme(string $scheme): self
{
if ($scheme === '' || $scheme === 'major' || $scheme === 'minor') {
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['scheme' => $scheme]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->scheme = $scheme;
}
}
return $this;
}
}

View File

@@ -0,0 +1,457 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
class NumberFormat extends Supervisor
{
// Pre-defined formats
const FORMAT_GENERAL = 'General';
const FORMAT_TEXT = '@';
const FORMAT_NUMBER = '0';
const FORMAT_NUMBER_0 = '0.0';
const FORMAT_NUMBER_00 = '0.00';
const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00';
const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-';
const FORMAT_PERCENTAGE = '0%';
const FORMAT_PERCENTAGE_0 = '0.0%';
const FORMAT_PERCENTAGE_00 = '0.00%';
/** @deprecated 1.26 use FORMAT_DATE_YYYYMMDD instead */
const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd';
const FORMAT_DATE_YYYYMMDD = 'yyyy-mm-dd';
const FORMAT_DATE_DDMMYYYY = 'dd/mm/yyyy';
const FORMAT_DATE_DMYSLASH = 'd/m/yy';
const FORMAT_DATE_DMYMINUS = 'd-m-yy';
const FORMAT_DATE_DMMINUS = 'd-m';
const FORMAT_DATE_MYMINUS = 'm-yy';
const FORMAT_DATE_XLSX14 = 'mm-dd-yy';
const FORMAT_DATE_XLSX15 = 'd-mmm-yy';
const FORMAT_DATE_XLSX16 = 'd-mmm';
const FORMAT_DATE_XLSX17 = 'mmm-yy';
const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm';
const FORMAT_DATE_DATETIME = 'd/m/yy h:mm';
const FORMAT_DATE_TIME1 = 'h:mm AM/PM';
const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM';
const FORMAT_DATE_TIME3 = 'h:mm';
const FORMAT_DATE_TIME4 = 'h:mm:ss';
const FORMAT_DATE_TIME5 = 'mm:ss';
const FORMAT_DATE_TIME6 = 'h:mm:ss';
const FORMAT_DATE_TIME7 = 'i:s.S';
const FORMAT_DATE_TIME8 = 'h:mm:ss;@';
const FORMAT_DATE_YYYYMMDDSLASH = 'yyyy/mm/dd;@';
const DATE_TIME_OR_DATETIME_ARRAY = [
self::FORMAT_DATE_YYYYMMDD,
self::FORMAT_DATE_DDMMYYYY,
self::FORMAT_DATE_DMYSLASH,
self::FORMAT_DATE_DMYMINUS,
self::FORMAT_DATE_DMMINUS,
self::FORMAT_DATE_MYMINUS,
self::FORMAT_DATE_XLSX14,
self::FORMAT_DATE_XLSX15,
self::FORMAT_DATE_XLSX16,
self::FORMAT_DATE_XLSX17,
self::FORMAT_DATE_XLSX22,
self::FORMAT_DATE_DATETIME,
self::FORMAT_DATE_TIME1,
self::FORMAT_DATE_TIME2,
self::FORMAT_DATE_TIME3,
self::FORMAT_DATE_TIME4,
self::FORMAT_DATE_TIME5,
self::FORMAT_DATE_TIME6,
self::FORMAT_DATE_TIME7,
self::FORMAT_DATE_TIME8,
self::FORMAT_DATE_YYYYMMDDSLASH,
];
const TIME_OR_DATETIME_ARRAY = [
self::FORMAT_DATE_XLSX22,
self::FORMAT_DATE_DATETIME,
self::FORMAT_DATE_TIME1,
self::FORMAT_DATE_TIME2,
self::FORMAT_DATE_TIME3,
self::FORMAT_DATE_TIME4,
self::FORMAT_DATE_TIME5,
self::FORMAT_DATE_TIME6,
self::FORMAT_DATE_TIME7,
self::FORMAT_DATE_TIME8,
];
/** @deprecated 1.28 use FORMAT_CURRENCY_USD_INTEGER instead */
const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0_-';
const FORMAT_CURRENCY_USD_INTEGER = '$#,##0_-';
const FORMAT_CURRENCY_USD = '$#,##0.00_-';
/** @deprecated 1.28 use FORMAT_CURRENCY_EUR_INTEGER instead */
const FORMAT_CURRENCY_EUR_SIMPLE = '#,##0_-"€"';
const FORMAT_CURRENCY_EUR_INTEGER = '#,##0_-[$€]';
const FORMAT_CURRENCY_EUR = '#,##0.00_-[$€]';
const FORMAT_ACCOUNTING_USD = '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)';
const FORMAT_ACCOUNTING_EUR = '_("€"* #,##0.00_);_("€"* \(#,##0.00\);_("€"* "-"??_);_(@_)';
/**
* Excel built-in number formats.
*
* @var array
*/
protected static $builtInFormats;
/**
* Excel built-in number formats (flipped, for faster lookups).
*
* @var array
*/
protected static $flippedBuiltInFormats;
/**
* Format Code.
*
* @var null|string
*/
protected $formatCode = self::FORMAT_GENERAL;
/**
* Built-in format Code.
*
* @var false|int
*/
protected $builtInFormatCode = 0;
/**
* Create a new NumberFormat.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
if ($isConditional) {
$this->formatCode = null;
$this->builtInFormatCode = false;
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return NumberFormat
*/
public function getSharedComponent()
{
/** @var Style */
$parent = $this->parent;
return $parent->getSharedComponent()->getNumberFormat();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['numberFormat' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getNumberFormat()->applyFromArray(
* [
* 'formatCode' => NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['formatCode'])) {
$this->setFormatCode($styleArray['formatCode']);
}
}
return $this;
}
/**
* Get Format Code.
*
* @return null|string
*/
public function getFormatCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getFormatCode();
}
if (is_int($this->builtInFormatCode)) {
return self::builtInFormatCode($this->builtInFormatCode);
}
return $this->formatCode;
}
/**
* Set Format Code.
*
* @param string $formatCode see self::FORMAT_*
*
* @return $this
*/
public function setFormatCode(string $formatCode)
{
if ($formatCode == '') {
$formatCode = self::FORMAT_GENERAL;
}
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['formatCode' => $formatCode]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->formatCode = $formatCode;
$this->builtInFormatCode = self::builtInFormatCodeIndex($formatCode);
}
return $this;
}
/**
* Get Built-In Format Code.
*
* @return false|int
*/
public function getBuiltInFormatCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getBuiltInFormatCode();
}
// Scrutinizer says this could return true. It is wrong.
return $this->builtInFormatCode;
}
/**
* Set Built-In Format Code.
*
* @param int $formatCodeIndex Id of the built-in format code to use
*
* @return $this
*/
public function setBuiltInFormatCode(int $formatCodeIndex)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['formatCode' => self::builtInFormatCode($formatCodeIndex)]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->builtInFormatCode = $formatCodeIndex;
$this->formatCode = self::builtInFormatCode($formatCodeIndex);
}
return $this;
}
/**
* Fill built-in format codes.
*/
private static function fillBuiltInFormatCodes(): void
{
// [MS-OI29500: Microsoft Office Implementation Information for ISO/IEC-29500 Standard Compliance]
// 18.8.30. numFmt (Number Format)
//
// The ECMA standard defines built-in format IDs
// 14: "mm-dd-yy"
// 22: "m/d/yy h:mm"
// 37: "#,##0 ;(#,##0)"
// 38: "#,##0 ;[Red](#,##0)"
// 39: "#,##0.00;(#,##0.00)"
// 40: "#,##0.00;[Red](#,##0.00)"
// 47: "mmss.0"
// KOR fmt 55: "yyyy-mm-dd"
// Excel defines built-in format IDs
// 14: "m/d/yyyy"
// 22: "m/d/yyyy h:mm"
// 37: "#,##0_);(#,##0)"
// 38: "#,##0_);[Red](#,##0)"
// 39: "#,##0.00_);(#,##0.00)"
// 40: "#,##0.00_);[Red](#,##0.00)"
// 47: "mm:ss.0"
// KOR fmt 55: "yyyy/mm/dd"
// Built-in format codes
if (empty(self::$builtInFormats)) {
self::$builtInFormats = [];
// General
self::$builtInFormats[0] = self::FORMAT_GENERAL;
self::$builtInFormats[1] = '0';
self::$builtInFormats[2] = '0.00';
self::$builtInFormats[3] = '#,##0';
self::$builtInFormats[4] = '#,##0.00';
self::$builtInFormats[9] = '0%';
self::$builtInFormats[10] = '0.00%';
self::$builtInFormats[11] = '0.00E+00';
self::$builtInFormats[12] = '# ?/?';
self::$builtInFormats[13] = '# ??/??';
self::$builtInFormats[14] = 'm/d/yyyy'; // Despite ECMA 'mm-dd-yy';
self::$builtInFormats[15] = 'd-mmm-yy';
self::$builtInFormats[16] = 'd-mmm';
self::$builtInFormats[17] = 'mmm-yy';
self::$builtInFormats[18] = 'h:mm AM/PM';
self::$builtInFormats[19] = 'h:mm:ss AM/PM';
self::$builtInFormats[20] = 'h:mm';
self::$builtInFormats[21] = 'h:mm:ss';
self::$builtInFormats[22] = 'm/d/yyyy h:mm'; // Despite ECMA 'm/d/yy h:mm';
self::$builtInFormats[37] = '#,##0_);(#,##0)'; // Despite ECMA '#,##0 ;(#,##0)';
self::$builtInFormats[38] = '#,##0_);[Red](#,##0)'; // Despite ECMA '#,##0 ;[Red](#,##0)';
self::$builtInFormats[39] = '#,##0.00_);(#,##0.00)'; // Despite ECMA '#,##0.00;(#,##0.00)';
self::$builtInFormats[40] = '#,##0.00_);[Red](#,##0.00)'; // Despite ECMA '#,##0.00;[Red](#,##0.00)';
self::$builtInFormats[44] = '_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)';
self::$builtInFormats[45] = 'mm:ss';
self::$builtInFormats[46] = '[h]:mm:ss';
self::$builtInFormats[47] = 'mm:ss.0'; // Despite ECMA 'mmss.0';
self::$builtInFormats[48] = '##0.0E+0';
self::$builtInFormats[49] = '@';
// CHT
self::$builtInFormats[27] = '[$-404]e/m/d';
self::$builtInFormats[30] = 'm/d/yy';
self::$builtInFormats[36] = '[$-404]e/m/d';
self::$builtInFormats[50] = '[$-404]e/m/d';
self::$builtInFormats[57] = '[$-404]e/m/d';
// THA
self::$builtInFormats[59] = 't0';
self::$builtInFormats[60] = 't0.00';
self::$builtInFormats[61] = 't#,##0';
self::$builtInFormats[62] = 't#,##0.00';
self::$builtInFormats[67] = 't0%';
self::$builtInFormats[68] = 't0.00%';
self::$builtInFormats[69] = 't# ?/?';
self::$builtInFormats[70] = 't# ??/??';
// JPN
self::$builtInFormats[28] = '[$-411]ggge"年"m"月"d"日"';
self::$builtInFormats[29] = '[$-411]ggge"年"m"月"d"日"';
self::$builtInFormats[31] = 'yyyy"年"m"月"d"日"';
self::$builtInFormats[32] = 'h"時"mm"分"';
self::$builtInFormats[33] = 'h"時"mm"分"ss"秒"';
self::$builtInFormats[34] = 'yyyy"年"m"月"';
self::$builtInFormats[35] = 'm"月"d"日"';
self::$builtInFormats[51] = '[$-411]ggge"年"m"月"d"日"';
self::$builtInFormats[52] = 'yyyy"年"m"月"';
self::$builtInFormats[53] = 'm"月"d"日"';
self::$builtInFormats[54] = '[$-411]ggge"年"m"月"d"日"';
self::$builtInFormats[55] = 'yyyy"年"m"月"';
self::$builtInFormats[56] = 'm"月"d"日"';
self::$builtInFormats[58] = '[$-411]ggge"年"m"月"d"日"';
// Flip array (for faster lookups)
self::$flippedBuiltInFormats = array_flip(self::$builtInFormats);
}
}
/**
* Get built-in format code.
*
* @param int $index
*
* @return string
*/
public static function builtInFormatCode($index)
{
// Clean parameter
$index = (int) $index;
// Ensure built-in format codes are available
self::fillBuiltInFormatCodes();
// Lookup format code
if (isset(self::$builtInFormats[$index])) {
return self::$builtInFormats[$index];
}
return '';
}
/**
* Get built-in format code index.
*
* @param string $formatCodeIndex
*
* @return false|int
*/
public static function builtInFormatCodeIndex($formatCodeIndex)
{
// Ensure built-in format codes are available
self::fillBuiltInFormatCodes();
// Lookup format code
if (array_key_exists($formatCodeIndex, self::$flippedBuiltInFormats)) {
return self::$flippedBuiltInFormats[$formatCodeIndex];
}
return false;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->formatCode .
$this->builtInFormatCode .
__CLASS__
);
}
/**
* Convert a value in a pre-defined format to a PHP string.
*
* @param mixed $value Value to format
* @param string $format Format code: see = self::FORMAT_* for predefined values;
* or can be any valid MS Excel custom format string
* @param array $callBack Callback function for additional formatting of string
*
* @return string Formatted string
*/
public static function toFormattedString($value, $format, $callBack = null)
{
return NumberFormat\Formatter::toFormattedString($value, $format, $callBack);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'formatCode', $this->getFormatCode());
return $exportedArray;
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
abstract class BaseFormatter
{
protected static function stripQuotes(string $format): string
{
// Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
return str_replace(['"', '*'], '', $format);
}
}

View File

@@ -0,0 +1,182 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Shared\Date;
class DateFormatter
{
/**
* Search/replace values to convert Excel date/time format masks to PHP format masks.
*/
private const DATE_FORMAT_REPLACEMENTS = [
// first remove escapes related to non-format characters
'\\' => '',
// 12-hour suffix
'am/pm' => 'A',
// 4-digit year
'e' => 'Y',
'yyyy' => 'Y',
// 2-digit year
'yy' => 'y',
// first letter of month - no php equivalent
'mmmmm' => 'M',
// full month name
'mmmm' => 'F',
// short month name
'mmm' => 'M',
// mm is minutes if time, but can also be month w/leading zero
// so we try to identify times be the inclusion of a : separator in the mask
// It isn't perfect, but the best way I know how
':mm' => ':i',
'mm:' => 'i:',
// full day of week name
'dddd' => 'l',
// short day of week name
'ddd' => 'D',
// days leading zero
'dd' => 'd',
// days no leading zero
'd' => 'j',
// fractional seconds - no php equivalent
'.s' => '',
];
/**
* Search/replace values to convert Excel date/time format masks hours to PHP format masks (24 hr clock).
*/
private const DATE_FORMAT_REPLACEMENTS24 = [
'hh' => 'H',
'h' => 'G',
// month leading zero
'mm' => 'm',
// month no leading zero
'm' => 'n',
// seconds
'ss' => 's',
];
/**
* Search/replace values to convert Excel date/time format masks hours to PHP format masks (12 hr clock).
*/
private const DATE_FORMAT_REPLACEMENTS12 = [
'hh' => 'h',
'h' => 'g',
// month leading zero
'mm' => 'm',
// month no leading zero
'm' => 'n',
// seconds
'ss' => 's',
];
private const HOURS_IN_DAY = 24;
private const MINUTES_IN_DAY = 60 * self::HOURS_IN_DAY;
private const SECONDS_IN_DAY = 60 * self::MINUTES_IN_DAY;
private const INTERVAL_PRECISION = 10;
private const INTERVAL_LEADING_ZERO = [
'[hh]',
'[mm]',
'[ss]',
];
private const INTERVAL_ROUND_PRECISION = [
// hours and minutes truncate
'[h]' => self::INTERVAL_PRECISION,
'[hh]' => self::INTERVAL_PRECISION,
'[m]' => self::INTERVAL_PRECISION,
'[mm]' => self::INTERVAL_PRECISION,
// seconds round
'[s]' => 0,
'[ss]' => 0,
];
private const INTERVAL_MULTIPLIER = [
'[h]' => self::HOURS_IN_DAY,
'[hh]' => self::HOURS_IN_DAY,
'[m]' => self::MINUTES_IN_DAY,
'[mm]' => self::MINUTES_IN_DAY,
'[s]' => self::SECONDS_IN_DAY,
'[ss]' => self::SECONDS_IN_DAY,
];
/** @param mixed $value */
private static function tryInterval(bool &$seekingBracket, string &$block, $value, string $format): void
{
if ($seekingBracket) {
if (false !== strpos($block, $format)) {
$hours = (string) (int) round(
self::INTERVAL_MULTIPLIER[$format] * $value,
self::INTERVAL_ROUND_PRECISION[$format]
);
if (strlen($hours) === 1 && in_array($format, self::INTERVAL_LEADING_ZERO, true)) {
$hours = "0$hours";
}
$block = str_replace($format, $hours, $block);
$seekingBracket = false;
}
}
}
/** @param mixed $value */
public static function format($value, string $format): string
{
// strip off first part containing e.g. [$-F800] or [$USD-409]
// general syntax: [$<Currency string>-<language info>]
// language info is in hexadecimal
// strip off chinese part like [DBNum1][$-804]
$format = (string) preg_replace('/^(\[DBNum\d\])*(\[\$[^\]]*\])/i', '', $format);
// OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case;
// but we don't want to change any quoted strings
/** @var callable */
$callable = [self::class, 'setLowercaseCallback'];
$format = (string) preg_replace_callback('/(?:^|")([^"]*)(?:$|")/', $callable, $format);
// Only process the non-quoted blocks for date format characters
$blocks = explode('"', $format);
foreach ($blocks as $key => &$block) {
if ($key % 2 == 0) {
$block = strtr($block, self::DATE_FORMAT_REPLACEMENTS);
if (!strpos($block, 'A')) {
// 24-hour time format
// when [h]:mm format, the [h] should replace to the hours of the value * 24
$seekingBracket = true;
self::tryInterval($seekingBracket, $block, $value, '[h]');
self::tryInterval($seekingBracket, $block, $value, '[hh]');
self::tryInterval($seekingBracket, $block, $value, '[mm]');
self::tryInterval($seekingBracket, $block, $value, '[m]');
self::tryInterval($seekingBracket, $block, $value, '[s]');
self::tryInterval($seekingBracket, $block, $value, '[ss]');
$block = strtr($block, self::DATE_FORMAT_REPLACEMENTS24);
} else {
// 12-hour time format
$block = strtr($block, self::DATE_FORMAT_REPLACEMENTS12);
}
}
}
$format = implode('"', $blocks);
// escape any quoted characters so that DateTime format() will render them correctly
/** @var callable */
$callback = [self::class, 'escapeQuotesCallback'];
$format = (string) preg_replace_callback('/"(.*)"/U', $callback, $format);
$dateObj = Date::excelToDateTimeObject($value);
// If the colon preceding minute had been quoted, as happens in
// Excel 2003 XML formats, m will not have been changed to i above.
// Change it now.
$format = (string) \preg_replace('/\\\\:m/', ':i', $format);
return $dateObj->format($format);
}
private static function setLowercaseCallback(array $matches): string
{
return mb_strtolower($matches[0]);
}
private static function escapeQuotesCallback(array $matches): string
{
return '\\' . implode('\\', /** @scrutinizer ignore-type */ str_split($matches[1]));
}
}

View File

@@ -0,0 +1,206 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Reader\Xls\Color\BIFF8;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class Formatter
{
/**
* Matches any @ symbol that isn't enclosed in quotes.
*/
private const SYMBOL_AT = '/@(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu';
/**
* Matches any ; symbol that isn't enclosed in quotes, for a "section" split.
*/
private const SECTION_SPLIT = '/;(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu';
/**
* @param mixed $value
* @param mixed $comparisonValue
* @param mixed $defaultComparisonValue
*/
private static function splitFormatComparison(
$value,
?string $condition,
$comparisonValue,
string $defaultCondition,
$defaultComparisonValue
): bool {
if (!$condition) {
$condition = $defaultCondition;
$comparisonValue = $defaultComparisonValue;
}
switch ($condition) {
case '>':
return $value > $comparisonValue;
case '<':
return $value < $comparisonValue;
case '<=':
return $value <= $comparisonValue;
case '<>':
return $value != $comparisonValue;
case '=':
return $value == $comparisonValue;
}
return $value >= $comparisonValue;
}
/** @param mixed $value */
private static function splitFormatForSectionSelection(array $sections, $value): array
{
// Extract the relevant section depending on whether number is positive, negative, or zero?
// Text not supported yet.
// Here is how the sections apply to various values in Excel:
// 1 section: [POSITIVE/NEGATIVE/ZERO/TEXT]
// 2 sections: [POSITIVE/ZERO/TEXT] [NEGATIVE]
// 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO]
// 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
$sectionCount = count($sections);
// Colour could be a named colour, or a numeric index entry in the colour-palette
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . '|color\\s*(\\d+))\\]/mui';
$cond_regex = '/\\[(>|>=|<|<=|=|<>)([+-]?\\d+([.]\\d+)?)\\]/';
$colors = ['', '', '', '', ''];
$conditionOperations = ['', '', '', '', ''];
$conditionComparisonValues = [0, 0, 0, 0, 0];
for ($idx = 0; $idx < $sectionCount; ++$idx) {
if (preg_match($color_regex, $sections[$idx], $matches)) {
if (isset($matches[2])) {
$colors[$idx] = '#' . BIFF8::lookup((int) $matches[2] + 7)['rgb'];
} else {
$colors[$idx] = $matches[0];
}
$sections[$idx] = (string) preg_replace($color_regex, '', $sections[$idx]);
}
if (preg_match($cond_regex, $sections[$idx], $matches)) {
$conditionOperations[$idx] = $matches[1];
$conditionComparisonValues[$idx] = $matches[2];
$sections[$idx] = (string) preg_replace($cond_regex, '', $sections[$idx]);
}
}
$color = $colors[0];
$format = $sections[0];
$absval = $value;
switch ($sectionCount) {
case 2:
$absval = abs($value);
if (!self::splitFormatComparison($value, $conditionOperations[0], $conditionComparisonValues[0], '>=', 0)) {
$color = $colors[1];
$format = $sections[1];
}
break;
case 3:
case 4:
$absval = abs($value);
if (!self::splitFormatComparison($value, $conditionOperations[0], $conditionComparisonValues[0], '>', 0)) {
if (self::splitFormatComparison($value, $conditionOperations[1], $conditionComparisonValues[1], '<', 0)) {
$color = $colors[1];
$format = $sections[1];
} else {
$color = $colors[2];
$format = $sections[2];
}
}
break;
}
return [$color, $format, $absval];
}
/**
* Convert a value in a pre-defined format to a PHP string.
*
* @param null|bool|float|int|RichText|string $value Value to format
* @param string $format Format code: see = self::FORMAT_* for predefined values;
* or can be any valid MS Excel custom format string
* @param array $callBack Callback function for additional formatting of string
*
* @return string Formatted string
*/
public static function toFormattedString($value, $format, $callBack = null)
{
if (is_bool($value)) {
return $value ? Calculation::getTRUE() : Calculation::getFALSE();
}
// For now we do not treat strings in sections, although section 4 of a format code affects strings
// Process a single block format code containing @ for text substitution
if (preg_match(self::SECTION_SPLIT, $format) === 0 && preg_match(self::SYMBOL_AT, $format) === 1) {
return str_replace('"', '', preg_replace(self::SYMBOL_AT, (string) $value, $format) ?? '');
}
// If we have a text value, return it "as is"
if (!is_numeric($value)) {
return (string) $value;
}
// For 'General' format code, we just pass the value although this is not entirely the way Excel does it,
// it seems to round numbers to a total of 10 digits.
if (($format === NumberFormat::FORMAT_GENERAL) || ($format === NumberFormat::FORMAT_TEXT)) {
return (string) $value;
}
// Ignore square-$-brackets prefix in format string, like "[$-411]ge.m.d", "[$-010419]0%", etc
$format = (string) preg_replace('/^\[\$-[^\]]*\]/', '', $format);
$format = (string) preg_replace_callback(
'/(["])(?:(?=(\\\\?))\\2.)*?\\1/u',
function ($matches) {
return str_replace('.', chr(0x00), $matches[0]);
},
$format
);
// Convert any other escaped characters to quoted strings, e.g. (\T to "T")
$format = (string) preg_replace('/(\\\(((.)(?!((AM\/PM)|(A\/P))))|([^ ])))(?=(?:[^"]|"[^"]*")*$)/ui', '"${2}"', $format);
// Get the sections, there can be up to four sections, separated with a semi-colon (but only if not a quoted literal)
$sections = preg_split(self::SECTION_SPLIT, $format) ?: [];
[$colors, $format, $value] = self::splitFormatForSectionSelection($sections, $value);
// In Excel formats, "_" is used to add spacing,
// The following character indicates the size of the spacing, which we can't do in HTML, so we just use a standard space
$format = (string) preg_replace('/_.?/ui', ' ', $format);
// Let's begin inspecting the format and converting the value to a formatted string
if (
// Check for date/time characters (not inside quotes)
(preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format))
// A date/time with a decimal time shouldn't have a digit placeholder before the decimal point
&& (preg_match('/[0\?#]\.(?![^\[]*\])/miu', $format) === 0)
) {
// datetime format
$value = DateFormatter::format($value, $format);
} else {
if (substr($format, 0, 1) === '"' && substr($format, -1, 1) === '"' && substr_count($format, '"') === 2) {
$value = substr($format, 1, -1);
} elseif (preg_match('/[0#, ]%/', $format)) {
// % number format - avoid weird '-0' problem
$value = PercentageFormatter::format(0 + (float) $value, $format);
} else {
$value = NumberFormatter::format($value, $format);
}
}
// Additional formatting provided by callback function
if ($callBack !== null) {
[$writerInstance, $function] = $callBack;
$value = $writerInstance->$function($value, $colors);
}
return str_replace(chr(0x00), '.', $value);
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
class FractionFormatter extends BaseFormatter
{
/**
* @param mixed $value
*/
public static function format($value, string $format): string
{
$format = self::stripQuotes($format);
$value = (float) $value;
$absValue = abs($value);
$sign = ($value < 0.0) ? '-' : '';
$integerPart = floor($absValue);
$decimalPart = self::getDecimal((string) $absValue);
if ($decimalPart === '0') {
return "{$sign}{$integerPart}";
}
$decimalLength = strlen($decimalPart);
$decimalDivisor = 10 ** $decimalLength;
preg_match('/(#?.*\?)\/(\?+|\d+)/', $format, $matches);
$formatIntegerPart = $matches[1];
if (is_numeric($matches[2])) {
$fractionDivisor = 100 / (int) $matches[2];
} else {
/** @var float */
$fractionDivisor = MathTrig\Gcd::evaluate((int) $decimalPart, $decimalDivisor);
}
$adjustedDecimalPart = (int) round((int) $decimalPart / $fractionDivisor, 0);
$adjustedDecimalDivisor = $decimalDivisor / $fractionDivisor;
if ((strpos($formatIntegerPart, '0') !== false)) {
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
} elseif ((strpos($formatIntegerPart, '#') !== false)) {
if ($integerPart == 0) {
return "{$sign}{$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
}
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
} elseif ((substr($formatIntegerPart, 0, 3) == '? ?')) {
if ($integerPart == 0) {
$integerPart = '';
}
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
}
$adjustedDecimalPart += $integerPart * $adjustedDecimalDivisor;
return "{$sign}{$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
}
private static function getDecimal(string $value): string
{
$decimalPart = '0';
if (preg_match('/^\\d*[.](\\d*[1-9])0*$/', $value, $matches) === 1) {
$decimalPart = $matches[1];
}
return $decimalPart;
}
}

View File

@@ -0,0 +1,326 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class NumberFormatter
{
private const NUMBER_REGEX = '/(0+)(\\.?)(0*)/';
private static function mergeComplexNumberFormatMasks(array $numbers, array $masks): array
{
$decimalCount = strlen($numbers[1]);
$postDecimalMasks = [];
do {
$tempMask = array_pop($masks);
if ($tempMask !== null) {
$postDecimalMasks[] = $tempMask;
$decimalCount -= strlen($tempMask);
}
} while ($tempMask !== null && $decimalCount > 0);
return [
implode('.', $masks),
implode('.', array_reverse($postDecimalMasks)),
];
}
/**
* @param mixed $number
*/
private static function processComplexNumberFormatMask($number, string $mask): string
{
/** @var string */
$result = $number;
$maskingBlockCount = preg_match_all('/0+/', $mask, $maskingBlocks, PREG_OFFSET_CAPTURE);
if ($maskingBlockCount > 1) {
$maskingBlocks = array_reverse($maskingBlocks[0]);
$offset = 0;
foreach ($maskingBlocks as $block) {
$size = strlen($block[0]);
$divisor = 10 ** $size;
$offset = $block[1];
/** @var float */
$numberFloat = $number;
$blockValue = sprintf("%0{$size}d", fmod($numberFloat, $divisor));
$number = floor($numberFloat / $divisor);
$mask = substr_replace($mask, $blockValue, $offset, $size);
}
/** @var string */
$numberString = $number;
if ($number > 0) {
$mask = substr_replace($mask, $numberString, $offset, 0);
}
$result = $mask;
}
return self::makeString($result);
}
/**
* @param mixed $number
*/
private static function complexNumberFormatMask($number, string $mask, bool $splitOnPoint = true): string
{
/** @var float */
$numberFloat = $number;
if ($splitOnPoint) {
$masks = explode('.', $mask);
if (count($masks) <= 2) {
$decmask = $masks[1] ?? '';
$decpos = substr_count($decmask, '0');
$numberFloat = round($numberFloat, $decpos);
}
}
$sign = ($numberFloat < 0.0) ? '-' : '';
$number = self::f2s(abs($numberFloat));
if ($splitOnPoint && strpos($mask, '.') !== false && strpos($number, '.') !== false) {
$numbers = explode('.', $number);
$masks = explode('.', $mask);
if (count($masks) > 2) {
$masks = self::mergeComplexNumberFormatMasks($numbers, $masks);
}
$integerPart = self::complexNumberFormatMask($numbers[0], $masks[0], false);
$numlen = strlen($numbers[1]);
$msklen = strlen($masks[1]);
if ($numlen < $msklen) {
$numbers[1] .= str_repeat('0', $msklen - $numlen);
}
$decimalPart = strrev(self::complexNumberFormatMask(strrev($numbers[1]), strrev($masks[1]), false));
$decimalPart = substr($decimalPart, 0, $msklen);
return "{$sign}{$integerPart}.{$decimalPart}";
}
if (strlen($number) < strlen($mask)) {
$number = str_repeat('0', strlen($mask) - strlen($number)) . $number;
}
$result = self::processComplexNumberFormatMask($number, $mask);
return "{$sign}{$result}";
}
public static function f2s(float $f): string
{
return self::floatStringConvertScientific((string) $f);
}
public static function floatStringConvertScientific(string $s): string
{
// convert only normalized form of scientific notation:
// optional sign, single digit 1-9,
// decimal point and digits (allowed to be omitted),
// E (e permitted), optional sign, one or more digits
if (preg_match('/^([+-])?([1-9])([.]([0-9]+))?[eE]([+-]?[0-9]+)$/', $s, $matches) === 1) {
$exponent = (int) $matches[5];
$sign = ($matches[1] === '-') ? '-' : '';
if ($exponent >= 0) {
$exponentPlus1 = $exponent + 1;
$out = $matches[2] . $matches[4];
$len = strlen($out);
if ($len < $exponentPlus1) {
$out .= str_repeat('0', $exponentPlus1 - $len);
}
$out = substr($out, 0, $exponentPlus1) . ((strlen($out) === $exponentPlus1) ? '' : ('.' . substr($out, $exponentPlus1)));
$s = "$sign$out";
} else {
$s = $sign . '0.' . str_repeat('0', -$exponent - 1) . $matches[2] . $matches[4];
}
}
return $s;
}
/**
* @param mixed $value
*/
private static function formatStraightNumericValue($value, string $format, array $matches, bool $useThousands): string
{
/** @var float */
$valueFloat = $value;
$left = $matches[1];
$dec = $matches[2];
$right = $matches[3];
// minimun width of formatted number (including dot)
$minWidth = strlen($left) + strlen($dec) + strlen($right);
if ($useThousands) {
$value = number_format(
$valueFloat,
strlen($right),
StringHelper::getDecimalSeparator(),
StringHelper::getThousandsSeparator()
);
return self::pregReplace(self::NUMBER_REGEX, $value, $format);
}
if (preg_match('/[0#]E[+-]0/i', $format)) {
// Scientific format
$decimals = strlen($right);
$size = $decimals + 3;
return sprintf("%{$size}.{$decimals}E", $valueFloat);
} elseif (preg_match('/0([^\d\.]+)0/', $format) || substr_count($format, '.') > 1) {
if ($valueFloat == floor($valueFloat) && substr_count($format, '.') === 1) {
$value *= 10 ** strlen(explode('.', $format)[1]);
}
$result = self::complexNumberFormatMask($value, $format);
if (strpos($result, 'E') !== false) {
// This is a hack and doesn't match Excel.
// It will, at least, be an accurate representation,
// even if formatted incorrectly.
// This is needed for absolute values >=1E18.
$result = self::f2s($valueFloat);
}
return $result;
}
$sprintf_pattern = "%0$minWidth." . strlen($right) . 'f';
/** @var float */
$valueFloat = $value;
$value = sprintf($sprintf_pattern, round($valueFloat, strlen($right)));
return self::pregReplace(self::NUMBER_REGEX, $value, $format);
}
/**
* @param mixed $value
*/
public static function format($value, string $format): string
{
// The "_" in this string has already been stripped out,
// so this test is never true. Furthermore, testing
// on Excel shows this format uses Euro symbol, not "EUR".
// if ($format === NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE) {
// return 'EUR ' . sprintf('%1.2f', $value);
// }
$baseFormat = $format;
$useThousands = self::areThousandsRequired($format);
$scale = self::scaleThousandsMillions($format);
if (preg_match('/[#\?0]?.*[#\?0]\/(\?+|\d+|#)/', $format)) {
// It's a dirty hack; but replace # and 0 digit placeholders with ?
$format = (string) preg_replace('/[#0]+\//', '?/', $format);
$format = (string) preg_replace('/\/[#0]+/', '/?', $format);
$value = FractionFormatter::format($value, $format);
} else {
// Handle the number itself
// scale number
$value = $value / $scale;
$paddingPlaceholder = (strpos($format, '?') !== false);
// Replace # or ? with 0
$format = self::pregReplace('/[\\#\?](?=(?:[^"]*"[^"]*")*[^"]*\Z)/', '0', $format);
// Remove locale code [$-###] for an LCID
$format = self::pregReplace('/\[\$\-.*\]/', '', $format);
$n = '/\\[[^\\]]+\\]/';
$m = self::pregReplace($n, '', $format);
// Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
$format = self::makeString(str_replace(['"', '*'], '', $format));
if (preg_match(self::NUMBER_REGEX, $m, $matches)) {
// There are placeholders for digits, so inject digits from the value into the mask
$value = self::formatStraightNumericValue($value, $format, $matches, $useThousands);
if ($paddingPlaceholder === true) {
$value = self::padValue($value, $baseFormat);
}
} elseif ($format !== NumberFormat::FORMAT_GENERAL) {
// Yes, I know that this is basically just a hack;
// if there's no placeholders for digits, just return the format mask "as is"
$value = self::makeString(str_replace('?', '', $format));
}
}
if (preg_match('/\[\$(.*)\]/u', $format, $m)) {
// Currency or Accounting
$currencyCode = $m[1];
[$currencyCode] = explode('-', $currencyCode);
if ($currencyCode == '') {
$currencyCode = StringHelper::getCurrencyCode();
}
$value = self::pregReplace('/\[\$([^\]]*)\]/u', $currencyCode, (string) $value);
}
if (
(strpos((string) $value, '0.') !== false) &&
((strpos($baseFormat, '#.') !== false) || (strpos($baseFormat, '?.') !== false))
) {
$value = preg_replace('/(\b)0\.|([^\d])0\./', '${2}.', (string) $value);
}
return (string) $value;
}
/**
* @param array|string $value
*/
private static function makeString($value): string
{
return is_array($value) ? '' : "$value";
}
private static function pregReplace(string $pattern, string $replacement, string $subject): string
{
return self::makeString(preg_replace($pattern, $replacement, $subject) ?? '');
}
public static function padValue(string $value, string $baseFormat): string
{
/** @phpstan-ignore-next-line */
[$preDecimal, $postDecimal] = preg_split('/\.(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu', $baseFormat . '.?');
$length = strlen($value);
if (strpos($postDecimal, '?') !== false) {
$value = str_pad(rtrim($value, '0. '), $length, ' ', STR_PAD_RIGHT);
}
if (strpos($preDecimal, '?') !== false) {
$value = str_pad(ltrim($value, '0, '), $length, ' ', STR_PAD_LEFT);
}
return $value;
}
/**
* Find out if we need thousands separator
* This is indicated by a comma enclosed by a digit placeholders: #, 0 or ?
*/
public static function areThousandsRequired(string &$format): bool
{
$useThousands = (bool) preg_match('/([#\?0]),([#\?0])/', $format);
if ($useThousands) {
$format = self::pregReplace('/([#\?0]),([#\?0])/', '${1}${2}', $format);
}
return $useThousands;
}
/**
* Scale thousands, millions,...
* This is indicated by a number of commas after a digit placeholder: #, or 0.0,, or ?,.
*/
public static function scaleThousandsMillions(string &$format): int
{
$scale = 1; // same as no scale
if (preg_match('/(#|0|\?)(,+)/', $format, $matches)) {
$scale = 1000 ** strlen($matches[2]);
// strip the commas
$format = self::pregReplace('/([#\?0]),+/', '${1}', $format);
}
return $scale;
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class PercentageFormatter extends BaseFormatter
{
/** @param float|int $value */
public static function format($value, string $format): string
{
if ($format === NumberFormat::FORMAT_PERCENTAGE) {
return round((100 * $value), 0) . '%';
}
$value *= 100;
$format = self::stripQuotes($format);
[, $vDecimals] = explode('.', ((string) $value) . '.');
$vDecimalCount = strlen(rtrim($vDecimals, '0'));
$format = str_replace('%', '%%', $format);
$wholePartSize = strlen((string) floor($value));
$decimalPartSize = 0;
$placeHolders = '';
// Number of decimals
if (preg_match('/\.([?0]+)/u', $format, $matches)) {
$decimalPartSize = strlen($matches[1]);
$vMinDecimalCount = strlen(rtrim($matches[1], '?'));
$decimalPartSize = min(max($vMinDecimalCount, $vDecimalCount), $decimalPartSize);
$placeHolders = str_repeat(' ', strlen($matches[1]) - $decimalPartSize);
}
// Number of digits to display before the decimal
if (preg_match('/([#0,]+)\.?/u', $format, $matches)) {
$firstZero = preg_replace('/^[#,]*/', '', $matches[1]) ?? '';
$wholePartSize = max($wholePartSize, strlen($firstZero));
}
$wholePartSize += $decimalPartSize + (int) ($decimalPartSize > 0);
$replacement = "0{$wholePartSize}.{$decimalPartSize}";
$mask = (string) preg_replace('/[#0,]+\.?[?#0,]*/ui', "%{$replacement}f{$placeHolders}", $format);
/** @var float */
$valueFloat = $value;
return sprintf($mask, round($valueFloat, $decimalPartSize));
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use NumberFormatter;
use PhpOffice\PhpSpreadsheet\Exception;
class Accounting extends Currency
{
/**
* @param string $currencyCode the currency symbol or code to display for this mask
* @param int $decimals number of decimal places to display, in the range 0-30
* @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not
* @param bool $currencySymbolPosition indicates whether the currency symbol comes before or after the value
* Possible values are Currency::LEADING_SYMBOL and Currency::TRAILING_SYMBOL
* @param bool $currencySymbolSpacing indicates whether there is spacing between the currency symbol and the value
* Possible values are Currency::SYMBOL_WITH_SPACING and Currency::SYMBOL_WITHOUT_SPACING
* @param ?string $locale Set the locale for the currency format; or leave as the default null.
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
* Note that setting a locale will override any other settings defined in this class
* other than the currency code; or decimals (unless the decimals value is set to 0).
*
* @throws Exception If a provided locale code is not a valid format
*/
public function __construct(
string $currencyCode = '$',
int $decimals = 2,
bool $thousandsSeparator = true,
bool $currencySymbolPosition = self::LEADING_SYMBOL,
bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING,
?string $locale = null
) {
$this->setCurrencyCode($currencyCode);
$this->setThousandsSeparator($thousandsSeparator);
$this->setDecimals($decimals);
$this->setCurrencySymbolPosition($currencySymbolPosition);
$this->setCurrencySymbolSpacing($currencySymbolSpacing);
$this->setLocale($locale);
}
/**
* @throws Exception if the Intl extension and ICU version don't support Accounting formats
*/
protected function getLocaleFormat(): string
{
if (version_compare(PHP_VERSION, '7.4.1', '<')) {
throw new Exception('The Intl extension does not support Accounting Formats below PHP 7.4.1');
}
if ($this->icuVersion() < 53.0) {
throw new Exception('The Intl extension does not support Accounting Formats without ICU 53');
}
// Scrutinizer does not recognize CURRENCY_ACCOUNTING
$formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY_ACCOUNTING);
$mask = $formatter->format();
if ($this->decimals === 0) {
$mask = (string) preg_replace('/\.0+/miu', '', $mask);
}
return str_replace('¤', $this->formatCurrencyCode(), $mask);
}
private function icuVersion(): float
{
[$major, $minor] = explode('.', INTL_ICU_VERSION);
return (float) "{$major}.{$minor}";
}
private function formatCurrencyCode(): string
{
if ($this->locale === null) {
return $this->currencyCode . '*';
}
return "[\${$this->currencyCode}-{$this->locale}]";
}
public function format(): string
{
if ($this->localeFormat !== null) {
return $this->localeFormat;
}
return sprintf(
'_-%s%s%s0%s%s%s_-',
$this->currencySymbolPosition === self::LEADING_SYMBOL ? $this->formatCurrencyCode() : null,
(
$this->currencySymbolPosition === self::LEADING_SYMBOL &&
$this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
) ? "\u{a0}" : '',
$this->thousandsSeparator ? '#,##' : null,
$this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null,
(
$this->currencySymbolPosition === self::TRAILING_SYMBOL &&
$this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
) ? "\u{a0}" : '',
$this->currencySymbolPosition === self::TRAILING_SYMBOL ? $this->formatCurrencyCode() : null
);
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use NumberFormatter;
use PhpOffice\PhpSpreadsheet\Exception;
class Currency extends Number
{
public const LEADING_SYMBOL = true;
public const TRAILING_SYMBOL = false;
public const SYMBOL_WITH_SPACING = true;
public const SYMBOL_WITHOUT_SPACING = false;
protected string $currencyCode = '$';
protected bool $currencySymbolPosition = self::LEADING_SYMBOL;
protected bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING;
/**
* @param string $currencyCode the currency symbol or code to display for this mask
* @param int $decimals number of decimal places to display, in the range 0-30
* @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not
* @param bool $currencySymbolPosition indicates whether the currency symbol comes before or after the value
* Possible values are Currency::LEADING_SYMBOL and Currency::TRAILING_SYMBOL
* @param bool $currencySymbolSpacing indicates whether there is spacing between the currency symbol and the value
* Possible values are Currency::SYMBOL_WITH_SPACING and Currency::SYMBOL_WITHOUT_SPACING
* @param ?string $locale Set the locale for the currency format; or leave as the default null.
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
* Note that setting a locale will override any other settings defined in this class
* other than the currency code; or decimals (unless the decimals value is set to 0).
*
* @throws Exception If a provided locale code is not a valid format
*/
public function __construct(
string $currencyCode = '$',
int $decimals = 2,
bool $thousandsSeparator = true,
bool $currencySymbolPosition = self::LEADING_SYMBOL,
bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING,
?string $locale = null
) {
$this->setCurrencyCode($currencyCode);
$this->setThousandsSeparator($thousandsSeparator);
$this->setDecimals($decimals);
$this->setCurrencySymbolPosition($currencySymbolPosition);
$this->setCurrencySymbolSpacing($currencySymbolSpacing);
$this->setLocale($locale);
}
public function setCurrencyCode(string $currencyCode): void
{
$this->currencyCode = $currencyCode;
}
public function setCurrencySymbolPosition(bool $currencySymbolPosition = self::LEADING_SYMBOL): void
{
$this->currencySymbolPosition = $currencySymbolPosition;
}
public function setCurrencySymbolSpacing(bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING): void
{
$this->currencySymbolSpacing = $currencySymbolSpacing;
}
protected function getLocaleFormat(): string
{
$formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY);
$mask = $formatter->format();
if ($this->decimals === 0) {
$mask = (string) preg_replace('/\.0+/miu', '', $mask);
}
return str_replace('¤', $this->formatCurrencyCode(), $mask);
}
private function formatCurrencyCode(): string
{
if ($this->locale === null) {
return $this->currencyCode;
}
return "[\${$this->currencyCode}-{$this->locale}]";
}
public function format(): string
{
if ($this->localeFormat !== null) {
return $this->localeFormat;
}
return sprintf(
'%s%s%s0%s%s%s',
$this->currencySymbolPosition === self::LEADING_SYMBOL ? $this->formatCurrencyCode() : null,
(
$this->currencySymbolPosition === self::LEADING_SYMBOL &&
$this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
) ? "\u{a0}" : '',
$this->thousandsSeparator ? '#,##' : null,
$this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null,
(
$this->currencySymbolPosition === self::TRAILING_SYMBOL &&
$this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
) ? "\u{a0}" : '',
$this->currencySymbolPosition === self::TRAILING_SYMBOL ? $this->formatCurrencyCode() : null
);
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
class Date extends DateTimeWizard
{
/**
* Year (4 digits), e.g. 2023.
*/
public const YEAR_FULL = 'yyyy';
/**
* Year (last 2 digits), e.g. 23.
*/
public const YEAR_SHORT = 'yy';
public const MONTH_FIRST_LETTER = 'mmmmm';
/**
* Month name, long form, e.g. January.
*/
public const MONTH_NAME_FULL = 'mmmm';
/**
* Month name, short form, e.g. Jan.
*/
public const MONTH_NAME_SHORT = 'mmm';
/**
* Month number with a leading zero if required, e.g. 01.
*/
public const MONTH_NUMBER_LONG = 'mm';
/**
* Month number without a leading zero, e.g. 1.
*/
public const MONTH_NUMBER_SHORT = 'm';
/**
* Day of the week, full form, e.g. Tuesday.
*/
public const WEEKDAY_NAME_LONG = 'dddd';
/**
* Day of the week, short form, e.g. Tue.
*/
public const WEEKDAY_NAME_SHORT = 'ddd';
/**
* Day number with a leading zero, e.g. 03.
*/
public const DAY_NUMBER_LONG = 'dd';
/**
* Day number without a leading zero, e.g. 3.
*/
public const DAY_NUMBER_SHORT = 'd';
protected const DATE_BLOCKS = [
self::YEAR_FULL,
self::YEAR_SHORT,
self::MONTH_FIRST_LETTER,
self::MONTH_NAME_FULL,
self::MONTH_NAME_SHORT,
self::MONTH_NUMBER_LONG,
self::MONTH_NUMBER_SHORT,
self::WEEKDAY_NAME_LONG,
self::WEEKDAY_NAME_SHORT,
self::DAY_NUMBER_LONG,
self::DAY_NUMBER_SHORT,
];
public const SEPARATOR_DASH = '-';
public const SEPARATOR_DOT = '.';
public const SEPARATOR_SLASH = '/';
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
public const SEPARATOR_SPACE = ' ';
protected const DATE_DEFAULT = [
self::YEAR_FULL,
self::MONTH_NUMBER_LONG,
self::DAY_NUMBER_LONG,
];
/**
* @var string[]
*/
protected array $separators;
/**
* @var string[]
*/
protected array $formatBlocks;
/**
* @param null|string|string[] $separators
* If you want to use the same separator for all format blocks, then it can be passed as a string literal;
* if you wish to use different separators, then they should be passed as an array.
* If you want to use only a single format block, then pass a null as the separator argument
*/
public function __construct($separators = self::SEPARATOR_DASH, string ...$formatBlocks)
{
$separators ??= self::SEPARATOR_DASH;
$formatBlocks = (count($formatBlocks) === 0) ? self::DATE_DEFAULT : $formatBlocks;
$this->separators = $this->padSeparatorArray(
is_array($separators) ? $separators : [$separators],
count($formatBlocks) - 1
);
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
}
private function mapFormatBlocks(string $value): string
{
// Any date masking codes are returned as lower case values
if (in_array(mb_strtolower($value), self::DATE_BLOCKS, true)) {
return mb_strtolower($value);
}
// Wrap any string literals in quotes, so that they're clearly defined as string literals
return $this->wrapLiteral($value);
}
public function format(): string
{
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
class DateTime extends DateTimeWizard
{
/**
* @var string[]
*/
protected array $separators;
/**
* @var array<DateTimeWizard|string>
*/
protected array $formatBlocks;
/**
* @param null|string|string[] $separators
* If you want to use only a single format block, then pass a null as the separator argument
* @param DateTimeWizard|string ...$formatBlocks
*/
public function __construct($separators, ...$formatBlocks)
{
$this->separators = $this->padSeparatorArray(
is_array($separators) ? $separators : [$separators],
count($formatBlocks) - 1
);
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
}
/**
* @param DateTimeWizard|string $value
*/
private function mapFormatBlocks($value): string
{
// Any date masking codes are returned as lower case values
if (is_object($value)) {
// We can't explicitly test for Stringable until PHP >= 8.0
return $value;
}
// Wrap any string literals in quotes, so that they're clearly defined as string literals
return $this->wrapLiteral($value);
}
public function format(): string
{
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
abstract class DateTimeWizard implements Wizard
{
protected const NO_ESCAPING_NEEDED = "$+-/():!^&'~{}<>= ";
protected function padSeparatorArray(array $separators, int $count): array
{
$lastSeparator = array_pop($separators);
return $separators + array_fill(0, $count, $lastSeparator);
}
protected function escapeSingleCharacter(string $value): string
{
if (strpos(self::NO_ESCAPING_NEEDED, $value) !== false) {
return $value;
}
return "\\{$value}";
}
protected function wrapLiteral(string $value): string
{
if (mb_strlen($value, 'UTF-8') === 1) {
return $this->escapeSingleCharacter($value);
}
// Wrap any other string literals in quotes, so that they're clearly defined as string literals
return '"' . str_replace('"', '""', $value) . '"';
}
protected function intersperse(string $formatBlock, ?string $separator): string
{
return "{$formatBlock}{$separator}";
}
public function __toString(): string
{
return $this->format();
}
}

View File

@@ -0,0 +1,153 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
class Duration extends DateTimeWizard
{
public const DAYS_DURATION = 'd';
/**
* Hours as a duration (can exceed 24), e.g. 29.
*/
public const HOURS_DURATION = '[h]';
/**
* Hours without a leading zero, e.g. 9.
*/
public const HOURS_SHORT = 'h';
/**
* Hours with a leading zero, e.g. 09.
*/
public const HOURS_LONG = 'hh';
/**
* Minutes as a duration (can exceed 60), e.g. 109.
*/
public const MINUTES_DURATION = '[m]';
/**
* Minutes without a leading zero, e.g. 5.
*/
public const MINUTES_SHORT = 'm';
/**
* Minutes with a leading zero, e.g. 05.
*/
public const MINUTES_LONG = 'mm';
/**
* Seconds as a duration (can exceed 60), e.g. 129.
*/
public const SECONDS_DURATION = '[s]';
/**
* Seconds without a leading zero, e.g. 2.
*/
public const SECONDS_SHORT = 's';
/**
* Seconds with a leading zero, e.g. 02.
*/
public const SECONDS_LONG = 'ss';
protected const DURATION_BLOCKS = [
self::DAYS_DURATION,
self::HOURS_DURATION,
self::HOURS_LONG,
self::HOURS_SHORT,
self::MINUTES_DURATION,
self::MINUTES_LONG,
self::MINUTES_SHORT,
self::SECONDS_DURATION,
self::SECONDS_LONG,
self::SECONDS_SHORT,
];
protected const DURATION_MASKS = [
self::DAYS_DURATION => self::DAYS_DURATION,
self::HOURS_DURATION => self::HOURS_SHORT,
self::MINUTES_DURATION => self::MINUTES_LONG,
self::SECONDS_DURATION => self::SECONDS_LONG,
];
protected const DURATION_DEFAULTS = [
self::HOURS_LONG => self::HOURS_DURATION,
self::HOURS_SHORT => self::HOURS_DURATION,
self::MINUTES_LONG => self::MINUTES_DURATION,
self::MINUTES_SHORT => self::MINUTES_DURATION,
self::SECONDS_LONG => self::SECONDS_DURATION,
self::SECONDS_SHORT => self::SECONDS_DURATION,
];
public const SEPARATOR_COLON = ':';
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
public const SEPARATOR_SPACE = ' ';
public const DURATION_DEFAULT = [
self::HOURS_DURATION,
self::MINUTES_LONG,
self::SECONDS_LONG,
];
/**
* @var string[]
*/
protected array $separators;
/**
* @var string[]
*/
protected array $formatBlocks;
protected bool $durationIsSet = false;
/**
* @param null|string|string[] $separators
* If you want to use the same separator for all format blocks, then it can be passed as a string literal;
* if you wish to use different separators, then they should be passed as an array.
* If you want to use only a single format block, then pass a null as the separator argument
*/
public function __construct($separators = self::SEPARATOR_COLON, string ...$formatBlocks)
{
$separators ??= self::SEPARATOR_COLON;
$formatBlocks = (count($formatBlocks) === 0) ? self::DURATION_DEFAULT : $formatBlocks;
$this->separators = $this->padSeparatorArray(
is_array($separators) ? $separators : [$separators],
count($formatBlocks) - 1
);
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
if ($this->durationIsSet === false) {
// We need at least one duration mask, so if none has been set we change the first mask element
// to a duration.
$this->formatBlocks[0] = self::DURATION_DEFAULTS[mb_strtolower($this->formatBlocks[0])];
}
}
private function mapFormatBlocks(string $value): string
{
// Any duration masking codes are returned as lower case values
if (in_array(mb_strtolower($value), self::DURATION_BLOCKS, true)) {
if (array_key_exists(mb_strtolower($value), self::DURATION_MASKS)) {
if ($this->durationIsSet) {
// We should only have a single duration mask, the first defined in the mask set,
// so convert any additional duration masks to standard time masks.
$value = self::DURATION_MASKS[mb_strtolower($value)];
}
$this->durationIsSet = true;
}
return mb_strtolower($value);
}
// Wrap any string literals in quotes, so that they're clearly defined as string literals
return $this->wrapLiteral($value);
}
public function format(): string
{
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use NumberFormatter;
use PhpOffice\PhpSpreadsheet\Exception;
final class Locale
{
/**
* Language code: ISO-639 2 character, alpha.
* Optional script code: ISO-15924 4 alpha.
* Optional country code: ISO-3166-1, 2 character alpha.
* Separated by underscores or dashes.
*/
public const STRUCTURE = '/^(?P<language>[a-z]{2})([-_](?P<script>[a-z]{4}))?([-_](?P<country>[a-z]{2}))?$/i';
private NumberFormatter $formatter;
public function __construct(?string $locale, int $style)
{
if (class_exists(NumberFormatter::class) === false) {
throw new Exception();
}
$formatterLocale = str_replace('-', '_', $locale ?? '');
$this->formatter = new NumberFormatter($formatterLocale, $style);
if ($this->formatter->getLocale() !== $formatterLocale) {
throw new Exception("Unable to read locale data for '{$locale}'");
}
}
public function format(): string
{
return $this->formatter->getPattern();
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use PhpOffice\PhpSpreadsheet\Exception;
class Number extends NumberBase implements Wizard
{
public const WITH_THOUSANDS_SEPARATOR = true;
public const WITHOUT_THOUSANDS_SEPARATOR = false;
protected bool $thousandsSeparator = true;
/**
* @param int $decimals number of decimal places to display, in the range 0-30
* @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not
* @param ?string $locale Set the locale for the number format; or leave as the default null.
* Locale has no effect for Number Format values, and is retained here only for compatibility
* with the other Wizards.
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
*
* @throws Exception If a provided locale code is not a valid format
*/
public function __construct(
int $decimals = 2,
bool $thousandsSeparator = self::WITH_THOUSANDS_SEPARATOR,
?string $locale = null
) {
$this->setDecimals($decimals);
$this->setThousandsSeparator($thousandsSeparator);
$this->setLocale($locale);
}
public function setThousandsSeparator(bool $thousandsSeparator = self::WITH_THOUSANDS_SEPARATOR): void
{
$this->thousandsSeparator = $thousandsSeparator;
}
/**
* As MS Excel cannot easily handle Lakh, which is the only locale-specific Number format variant,
* we don't use locale with Numbers.
*/
protected function getLocaleFormat(): string
{
return $this->format();
}
public function format(): string
{
return sprintf(
'%s0%s',
$this->thousandsSeparator ? '#,##' : null,
$this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null
);
}
}

View File

@@ -0,0 +1,80 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use NumberFormatter;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
abstract class NumberBase
{
protected const MAX_DECIMALS = 30;
protected int $decimals = 2;
protected ?string $locale = null;
protected ?string $fullLocale = null;
protected ?string $localeFormat = null;
public function setDecimals(int $decimals = 2): void
{
$this->decimals = ($decimals > self::MAX_DECIMALS) ? self::MAX_DECIMALS : max($decimals, 0);
}
/**
* Setting a locale will override any settings defined in this class.
*
* @throws Exception If the locale code is not a valid format
*/
public function setLocale(?string $locale = null): void
{
if ($locale === null) {
$this->localeFormat = $this->locale = $this->fullLocale = null;
return;
}
$this->locale = $this->validateLocale($locale);
if (class_exists(NumberFormatter::class)) {
$this->localeFormat = $this->getLocaleFormat();
}
}
/**
* Stub: should be implemented as a concrete method in concrete wizards.
*/
abstract protected function getLocaleFormat(): string;
/**
* @throws Exception If the locale code is not a valid format
*/
private function validateLocale(string $locale): string
{
if (preg_match(Locale::STRUCTURE, $locale, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new Exception("Invalid locale code '{$locale}'");
}
['language' => $language, 'script' => $script, 'country' => $country] = $matches;
// Set case and separator to match standardised locale case
$language = strtolower($language ?? '');
$script = ($script === null) ? null : ucfirst(strtolower($script));
$country = ($country === null) ? null : strtoupper($country);
$this->fullLocale = implode('-', array_filter([$language, $script, $country]));
return $country === null ? $language : "{$language}-{$country}";
}
public function format(): string
{
return NumberFormat::FORMAT_GENERAL;
}
public function __toString(): string
{
return $this->format();
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use NumberFormatter;
use PhpOffice\PhpSpreadsheet\Exception;
class Percentage extends NumberBase implements Wizard
{
/**
* @param int $decimals number of decimal places to display, in the range 0-30
* @param ?string $locale Set the locale for the percentage format; or leave as the default null.
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
*
* @throws Exception If a provided locale code is not a valid format
*/
public function __construct(int $decimals = 2, ?string $locale = null)
{
$this->setDecimals($decimals);
$this->setLocale($locale);
}
protected function getLocaleFormat(): string
{
$formatter = new Locale($this->fullLocale, NumberFormatter::PERCENT);
return $this->decimals > 0
? str_replace('0', '0.' . str_repeat('0', $this->decimals), $formatter->format())
: $formatter->format();
}
public function format(): string
{
if ($this->localeFormat !== null) {
return $this->localeFormat;
}
return sprintf('0%s%%', $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null);
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use PhpOffice\PhpSpreadsheet\Exception;
class Scientific extends NumberBase implements Wizard
{
/**
* @param int $decimals number of decimal places to display, in the range 0-30
* @param ?string $locale Set the locale for the scientific format; or leave as the default null.
* Locale has no effect for Scientific Format values, and is retained here for compatibility
* with the other Wizards.
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
*
* @throws Exception If a provided locale code is not a valid format
*/
public function __construct(int $decimals = 2, ?string $locale = null)
{
$this->setDecimals($decimals);
$this->setLocale($locale);
}
protected function getLocaleFormat(): string
{
return $this->format();
}
public function format(): string
{
return sprintf('0%sE+00', $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null);
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
class Time extends DateTimeWizard
{
/**
* Hours without a leading zero, e.g. 9.
*/
public const HOURS_SHORT = 'h';
/**
* Hours with a leading zero, e.g. 09.
*/
public const HOURS_LONG = 'hh';
/**
* Minutes without a leading zero, e.g. 5.
*/
public const MINUTES_SHORT = 'm';
/**
* Minutes with a leading zero, e.g. 05.
*/
public const MINUTES_LONG = 'mm';
/**
* Seconds without a leading zero, e.g. 2.
*/
public const SECONDS_SHORT = 's';
/**
* Seconds with a leading zero, e.g. 02.
*/
public const SECONDS_LONG = 'ss';
public const MORNING_AFTERNOON = 'AM/PM';
protected const TIME_BLOCKS = [
self::HOURS_LONG,
self::HOURS_SHORT,
self::MINUTES_LONG,
self::MINUTES_SHORT,
self::SECONDS_LONG,
self::SECONDS_SHORT,
self::MORNING_AFTERNOON,
];
public const SEPARATOR_COLON = ':';
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
public const SEPARATOR_SPACE = ' ';
protected const TIME_DEFAULT = [
self::HOURS_LONG,
self::MINUTES_LONG,
self::SECONDS_LONG,
];
/**
* @var string[]
*/
protected array $separators;
/**
* @var string[]
*/
protected array $formatBlocks;
/**
* @param null|string|string[] $separators
* If you want to use the same separator for all format blocks, then it can be passed as a string literal;
* if you wish to use different separators, then they should be passed as an array.
* If you want to use only a single format block, then pass a null as the separator argument
*/
public function __construct($separators = self::SEPARATOR_COLON, string ...$formatBlocks)
{
$separators ??= self::SEPARATOR_COLON;
$formatBlocks = (count($formatBlocks) === 0) ? self::TIME_DEFAULT : $formatBlocks;
$this->separators = $this->padSeparatorArray(
is_array($separators) ? $separators : [$separators],
count($formatBlocks) - 1
);
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
}
private function mapFormatBlocks(string $value): string
{
// Any date masking codes are returned as lower case values
// except for AM/PM, which is set to uppercase
if (in_array(mb_strtolower($value), self::TIME_BLOCKS, true)) {
return mb_strtolower($value);
} elseif (mb_strtoupper($value) === self::MORNING_AFTERNOON) {
return mb_strtoupper($value);
}
// Wrap any string literals in quotes, so that they're clearly defined as string literals
return $this->wrapLiteral($value);
}
public function format(): string
{
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
interface Wizard
{
public function format(): string;
}

View File

@@ -0,0 +1,198 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
class Protection extends Supervisor
{
/** Protection styles */
const PROTECTION_INHERIT = 'inherit';
const PROTECTION_PROTECTED = 'protected';
const PROTECTION_UNPROTECTED = 'unprotected';
/**
* Locked.
*
* @var string
*/
protected $locked;
/**
* Hidden.
*
* @var string
*/
protected $hidden;
/**
* Create a new Protection.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
parent::__construct($isSupervisor);
// Initialise values
if (!$isConditional) {
$this->locked = self::PROTECTION_INHERIT;
$this->hidden = self::PROTECTION_INHERIT;
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return Protection
*/
public function getSharedComponent()
{
/** @var Style */
$parent = $this->parent;
return $parent->getSharedComponent()->getProtection();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['protection' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray(
* [
* 'locked' => TRUE,
* 'hidden' => FALSE
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
*
* @return $this
*/
public function applyFromArray(array $styleArray)
{
if ($this->isSupervisor) {
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
} else {
if (isset($styleArray['locked'])) {
$this->setLocked($styleArray['locked']);
}
if (isset($styleArray['hidden'])) {
$this->setHidden($styleArray['hidden']);
}
}
return $this;
}
/**
* Get locked.
*
* @return string
*/
public function getLocked()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getLocked();
}
return $this->locked;
}
/**
* Set locked.
*
* @param string $lockType see self::PROTECTION_*
*
* @return $this
*/
public function setLocked($lockType)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['locked' => $lockType]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->locked = $lockType;
}
return $this;
}
/**
* Get hidden.
*
* @return string
*/
public function getHidden()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHidden();
}
return $this->hidden;
}
/**
* Set hidden.
*
* @param string $hiddenType see self::PROTECTION_*
*
* @return $this
*/
public function setHidden($hiddenType)
{
if ($this->isSupervisor) {
$styleArray = $this->getStyleArray(['hidden' => $hiddenType]);
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->hidden = $hiddenType;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getHashCode();
}
return md5(
$this->locked .
$this->hidden .
__CLASS__
);
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'locked', $this->getLocked());
$this->exportArray2($exportedArray, 'hidden', $this->getHidden());
return $exportedArray;
}
}

View File

@@ -0,0 +1,175 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
/**
* Class to handle tint applied to color.
* Code borrows heavily from some Python projects.
*
* @see https://docs.python.org/3/library/colorsys.html
* @see https://gist.github.com/Mike-Honey/b36e651e9a7f1d2e1d60ce1c63b9b633
*/
class RgbTint
{
private const ONE_THIRD = 1.0 / 3.0;
private const ONE_SIXTH = 1.0 / 6.0;
private const TWO_THIRD = 2.0 / 3.0;
private const RGBMAX = 255.0;
/**
* MS excel's tint function expects that HLS is base 240.
*
* @see https://social.msdn.microsoft.com/Forums/en-US/e9d8c136-6d62-4098-9b1b-dac786149f43/excel-color-tint-algorithm-incorrect?forum=os_binaryfile#d3c2ac95-52e0-476b-86f1-e2a697f24969
*/
private const HLSMAX = 240.0;
/**
* Convert red/green/blue to hue/luminance/saturation.
*
* @param float $red 0.0 through 1.0
* @param float $green 0.0 through 1.0
* @param float $blue 0.0 through 1.0
*
* @return float[]
*/
private static function rgbToHls(float $red, float $green, float $blue): array
{
$maxc = max($red, $green, $blue);
$minc = min($red, $green, $blue);
$luminance = ($minc + $maxc) / 2.0;
if ($minc === $maxc) {
return [0.0, $luminance, 0.0];
}
$maxMinusMin = $maxc - $minc;
if ($luminance <= 0.5) {
$s = $maxMinusMin / ($maxc + $minc);
} else {
$s = $maxMinusMin / (2.0 - $maxc - $minc);
}
$rc = ($maxc - $red) / $maxMinusMin;
$gc = ($maxc - $green) / $maxMinusMin;
$bc = ($maxc - $blue) / $maxMinusMin;
if ($red === $maxc) {
$h = $bc - $gc;
} elseif ($green === $maxc) {
$h = 2.0 + $rc - $bc;
} else {
$h = 4.0 + $gc - $rc;
}
$h = self::positiveDecimalPart($h / 6.0);
return [$h, $luminance, $s];
}
/** @var mixed */
private static $scrutinizerZeroPointZero = 0.0;
/**
* Convert hue/luminance/saturation to red/green/blue.
*
* @param float $hue 0.0 through 1.0
* @param float $luminance 0.0 through 1.0
* @param float $saturation 0.0 through 1.0
*
* @return float[]
*/
private static function hlsToRgb($hue, $luminance, $saturation): array
{
if ($saturation === self::$scrutinizerZeroPointZero) {
return [$luminance, $luminance, $luminance];
}
if ($luminance <= 0.5) {
$m2 = $luminance * (1.0 + $saturation);
} else {
$m2 = $luminance + $saturation - ($luminance * $saturation);
}
$m1 = 2.0 * $luminance - $m2;
return [
self::vFunction($m1, $m2, $hue + self::ONE_THIRD),
self::vFunction($m1, $m2, $hue),
self::vFunction($m1, $m2, $hue - self::ONE_THIRD),
];
}
private static function vFunction(float $m1, float $m2, float $hue): float
{
$hue = self::positiveDecimalPart($hue);
if ($hue < self::ONE_SIXTH) {
return $m1 + ($m2 - $m1) * $hue * 6.0;
}
if ($hue < 0.5) {
return $m2;
}
if ($hue < self::TWO_THIRD) {
return $m1 + ($m2 - $m1) * (self::TWO_THIRD - $hue) * 6.0;
}
return $m1;
}
private static function positiveDecimalPart(float $hue): float
{
$hue = fmod($hue, 1.0);
return ($hue >= 0.0) ? $hue : (1.0 + $hue);
}
/**
* Convert red/green/blue to HLSMAX-based hue/luminance/saturation.
*
* @return int[]
*/
private static function rgbToMsHls(int $red, int $green, int $blue): array
{
$red01 = $red / self::RGBMAX;
$green01 = $green / self::RGBMAX;
$blue01 = $blue / self::RGBMAX;
[$hue, $luminance, $saturation] = self::rgbToHls($red01, $green01, $blue01);
return [
(int) round($hue * self::HLSMAX),
(int) round($luminance * self::HLSMAX),
(int) round($saturation * self::HLSMAX),
];
}
/**
* Converts HLSMAX based HLS values to rgb values in the range (0,1).
*
* @return float[]
*/
private static function msHlsToRgb(int $hue, int $lightness, int $saturation): array
{
return self::hlsToRgb($hue / self::HLSMAX, $lightness / self::HLSMAX, $saturation / self::HLSMAX);
}
/**
* Tints HLSMAX based luminance.
*
* @see http://ciintelligence.blogspot.co.uk/2012/02/converting-excel-theme-color-and-tint.html
*/
private static function tintLuminance(float $tint, float $luminance): int
{
if ($tint < 0) {
return (int) round($luminance * (1.0 + $tint));
}
return (int) round($luminance * (1.0 - $tint) + (self::HLSMAX - self::HLSMAX * (1.0 - $tint)));
}
/**
* Return result of tinting supplied rgb as 6 hex digits.
*/
public static function rgbAndTintToRgb(int $red, int $green, int $blue, float $tint): string
{
[$hue, $luminance, $saturation] = self::rgbToMsHls($red, $green, $blue);
[$red, $green, $blue] = self::msHlsToRgb($hue, self::tintLuminance($tint, $luminance), $saturation);
return sprintf(
'%02X%02X%02X',
(int) round($red * self::RGBMAX),
(int) round($green * self::RGBMAX),
(int) round($blue * self::RGBMAX)
);
}
}

View File

@@ -0,0 +1,745 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Style extends Supervisor
{
/**
* Font.
*
* @var Font
*/
protected $font;
/**
* Fill.
*
* @var Fill
*/
protected $fill;
/**
* Borders.
*
* @var Borders
*/
protected $borders;
/**
* Alignment.
*
* @var Alignment
*/
protected $alignment;
/**
* Number Format.
*
* @var NumberFormat
*/
protected $numberFormat;
/**
* Protection.
*
* @var Protection
*/
protected $protection;
/**
* Index of style in collection. Only used for real style.
*
* @var int
*/
protected $index;
/**
* Use Quote Prefix when displaying in cell editor. Only used for real style.
*
* @var bool
*/
protected $quotePrefix = false;
/**
* Internal cache for styles
* Used when applying style on range of cells (column or row) and cleared when
* all cells in range is styled.
*
* PhpSpreadsheet will always minimize the amount of styles used. So cells with
* same styles will reference the same Style instance. To check if two styles
* are similar Style::getHashCode() is used. This call is expensive. To minimize
* the need to call this method we can cache the internal PHP object id of the
* Style in the range. Style::getHashCode() will then only be called when we
* encounter a unique style.
*
* @see Style::applyFromArray()
* @see Style::getHashCode()
*
* @var null|array<string, array>
*/
private static $cachedStyles;
/**
* Create a new Style.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param bool $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
parent::__construct($isSupervisor);
// Initialise values
$this->font = new Font($isSupervisor, $isConditional);
$this->fill = new Fill($isSupervisor, $isConditional);
$this->borders = new Borders($isSupervisor, $isConditional);
$this->alignment = new Alignment($isSupervisor, $isConditional);
$this->numberFormat = new NumberFormat($isSupervisor, $isConditional);
$this->protection = new Protection($isSupervisor, $isConditional);
// bind parent if we are a supervisor
if ($isSupervisor) {
$this->font->bindParent($this);
$this->fill->bindParent($this);
$this->borders->bindParent($this);
$this->alignment->bindParent($this);
$this->numberFormat->bindParent($this);
$this->protection->bindParent($this);
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*/
public function getSharedComponent(): self
{
$activeSheet = $this->getActiveSheet();
$selectedCell = Functions::trimSheetFromCellReference($this->getActiveCell()); // e.g. 'A1'
if ($activeSheet->cellExists($selectedCell)) {
$xfIndex = $activeSheet->getCell($selectedCell)->getXfIndex();
} else {
$xfIndex = 0;
}
return $activeSheet->getParentOrThrow()->getCellXfByIndex($xfIndex);
}
/**
* Get parent. Only used for style supervisor.
*/
public function getParent(): Spreadsheet
{
return $this->getActiveSheet()->getParentOrThrow();
}
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
public function getStyleArray($array)
{
return ['quotePrefix' => $array];
}
/**
* Apply styles from array.
*
* <code>
* $spreadsheet->getActiveSheet()->getStyle('B2')->applyFromArray(
* [
* 'font' => [
* 'name' => 'Arial',
* 'bold' => true,
* 'italic' => false,
* 'underline' => Font::UNDERLINE_DOUBLE,
* 'strikethrough' => false,
* 'color' => [
* 'rgb' => '808080'
* ]
* ],
* 'borders' => [
* 'bottom' => [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ],
* 'top' => [
* 'borderStyle' => Border::BORDER_DASHDOT,
* 'color' => [
* 'rgb' => '808080'
* ]
* ]
* ],
* 'alignment' => [
* 'horizontal' => Alignment::HORIZONTAL_CENTER,
* 'vertical' => Alignment::VERTICAL_CENTER,
* 'wrapText' => true,
* ],
* 'quotePrefix' => true
* ]
* );
* </code>
*
* @param array $styleArray Array containing style information
* @param bool $advancedBorders advanced mode for setting borders
*
* @return $this
*/
public function applyFromArray(array $styleArray, $advancedBorders = true)
{
if ($this->isSupervisor) {
$pRange = $this->getSelectedCells();
// Uppercase coordinate and strip any Worksheet reference from the selected range
$pRange = strtoupper($pRange);
if (strpos($pRange, '!') !== false) {
$pRangeWorksheet = StringHelper::strToUpper(trim(substr($pRange, 0, (int) strrpos($pRange, '!')), "'"));
if ($pRangeWorksheet !== '' && StringHelper::strToUpper($this->getActiveSheet()->getTitle()) !== $pRangeWorksheet) {
throw new Exception('Invalid Worksheet for specified Range');
}
$pRange = strtoupper(Functions::trimSheetFromCellReference($pRange));
}
// Is it a cell range or a single cell?
if (strpos($pRange, ':') === false) {
$rangeA = $pRange;
$rangeB = $pRange;
} else {
[$rangeA, $rangeB] = explode(':', $pRange);
}
// Calculate range outer borders
$rangeStart = Coordinate::coordinateFromString($rangeA);
$rangeEnd = Coordinate::coordinateFromString($rangeB);
$rangeStartIndexes = Coordinate::indexesFromString($rangeA);
$rangeEndIndexes = Coordinate::indexesFromString($rangeB);
$columnStart = $rangeStart[0];
$columnEnd = $rangeEnd[0];
// Make sure we can loop upwards on rows and columns
if ($rangeStartIndexes[0] > $rangeEndIndexes[0] && $rangeStartIndexes[1] > $rangeEndIndexes[1]) {
$tmp = $rangeStartIndexes;
$rangeStartIndexes = $rangeEndIndexes;
$rangeEndIndexes = $tmp;
}
// ADVANCED MODE:
if ($advancedBorders && isset($styleArray['borders'])) {
// 'allBorders' is a shorthand property for 'outline' and 'inside' and
// it applies to components that have not been set explicitly
if (isset($styleArray['borders']['allBorders'])) {
foreach (['outline', 'inside'] as $component) {
if (!isset($styleArray['borders'][$component])) {
$styleArray['borders'][$component] = $styleArray['borders']['allBorders'];
}
}
unset($styleArray['borders']['allBorders']); // not needed any more
}
// 'outline' is a shorthand property for 'top', 'right', 'bottom', 'left'
// it applies to components that have not been set explicitly
if (isset($styleArray['borders']['outline'])) {
foreach (['top', 'right', 'bottom', 'left'] as $component) {
if (!isset($styleArray['borders'][$component])) {
$styleArray['borders'][$component] = $styleArray['borders']['outline'];
}
}
unset($styleArray['borders']['outline']); // not needed any more
}
// 'inside' is a shorthand property for 'vertical' and 'horizontal'
// it applies to components that have not been set explicitly
if (isset($styleArray['borders']['inside'])) {
foreach (['vertical', 'horizontal'] as $component) {
if (!isset($styleArray['borders'][$component])) {
$styleArray['borders'][$component] = $styleArray['borders']['inside'];
}
}
unset($styleArray['borders']['inside']); // not needed any more
}
// width and height characteristics of selection, 1, 2, or 3 (for 3 or more)
$xMax = min($rangeEndIndexes[0] - $rangeStartIndexes[0] + 1, 3);
$yMax = min($rangeEndIndexes[1] - $rangeStartIndexes[1] + 1, 3);
// loop through up to 3 x 3 = 9 regions
for ($x = 1; $x <= $xMax; ++$x) {
// start column index for region
$colStart = ($x == 3) ?
Coordinate::stringFromColumnIndex($rangeEndIndexes[0])
: Coordinate::stringFromColumnIndex($rangeStartIndexes[0] + $x - 1);
// end column index for region
$colEnd = ($x == 1) ?
Coordinate::stringFromColumnIndex($rangeStartIndexes[0])
: Coordinate::stringFromColumnIndex($rangeEndIndexes[0] - $xMax + $x);
for ($y = 1; $y <= $yMax; ++$y) {
// which edges are touching the region
$edges = [];
if ($x == 1) {
// are we at left edge
$edges[] = 'left';
}
if ($x == $xMax) {
// are we at right edge
$edges[] = 'right';
}
if ($y == 1) {
// are we at top edge?
$edges[] = 'top';
}
if ($y == $yMax) {
// are we at bottom edge?
$edges[] = 'bottom';
}
// start row index for region
$rowStart = ($y == 3) ?
$rangeEndIndexes[1] : $rangeStartIndexes[1] + $y - 1;
// end row index for region
$rowEnd = ($y == 1) ?
$rangeStartIndexes[1] : $rangeEndIndexes[1] - $yMax + $y;
// build range for region
$range = $colStart . $rowStart . ':' . $colEnd . $rowEnd;
// retrieve relevant style array for region
$regionStyles = $styleArray;
unset($regionStyles['borders']['inside']);
// what are the inner edges of the region when looking at the selection
$innerEdges = array_diff(['top', 'right', 'bottom', 'left'], $edges);
// inner edges that are not touching the region should take the 'inside' border properties if they have been set
foreach ($innerEdges as $innerEdge) {
switch ($innerEdge) {
case 'top':
case 'bottom':
// should pick up 'horizontal' border property if set
if (isset($styleArray['borders']['horizontal'])) {
$regionStyles['borders'][$innerEdge] = $styleArray['borders']['horizontal'];
} else {
unset($regionStyles['borders'][$innerEdge]);
}
break;
case 'left':
case 'right':
// should pick up 'vertical' border property if set
if (isset($styleArray['borders']['vertical'])) {
$regionStyles['borders'][$innerEdge] = $styleArray['borders']['vertical'];
} else {
unset($regionStyles['borders'][$innerEdge]);
}
break;
}
}
// apply region style to region by calling applyFromArray() in simple mode
$this->getActiveSheet()->getStyle($range)->applyFromArray($regionStyles, false);
}
}
// restore initial cell selection range
$this->getActiveSheet()->getStyle($pRange);
return $this;
}
// SIMPLE MODE:
// Selection type, inspect
if (preg_match('/^[A-Z]+1:[A-Z]+1048576$/', $pRange)) {
$selectionType = 'COLUMN';
// Enable caching of styles
self::$cachedStyles = ['hashByObjId' => [], 'styleByHash' => []];
} elseif (preg_match('/^A\d+:XFD\d+$/', $pRange)) {
$selectionType = 'ROW';
// Enable caching of styles
self::$cachedStyles = ['hashByObjId' => [], 'styleByHash' => []];
} else {
$selectionType = 'CELL';
}
// First loop through columns, rows, or cells to find out which styles are affected by this operation
$oldXfIndexes = $this->getOldXfIndexes($selectionType, $rangeStartIndexes, $rangeEndIndexes, $columnStart, $columnEnd, $styleArray);
// clone each of the affected styles, apply the style array, and add the new styles to the workbook
$workbook = $this->getActiveSheet()->getParentOrThrow();
$newXfIndexes = [];
foreach ($oldXfIndexes as $oldXfIndex => $dummy) {
$style = $workbook->getCellXfByIndex($oldXfIndex);
// $cachedStyles is set when applying style for a range of cells, either column or row
if (self::$cachedStyles === null) {
// Clone the old style and apply style-array
$newStyle = clone $style;
$newStyle->applyFromArray($styleArray);
// Look for existing style we can use instead (reduce memory usage)
$existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode());
} else {
// Style cache is stored by Style::getHashCode(). But calling this method is
// expensive. So we cache the php obj id -> hash.
$objId = spl_object_id($style);
// Look for the original HashCode
$styleHash = self::$cachedStyles['hashByObjId'][$objId] ?? null;
if ($styleHash === null) {
// This object_id is not cached, store the hashcode in case encounter again
$styleHash = self::$cachedStyles['hashByObjId'][$objId] = $style->getHashCode();
}
// Find existing style by hash.
$existingStyle = self::$cachedStyles['styleByHash'][$styleHash] ?? null;
if (!$existingStyle) {
// The old style combined with the new style array is not cached, so we create it now
$newStyle = clone $style;
$newStyle->applyFromArray($styleArray);
// Look for similar style in workbook to reduce memory usage
$existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode());
// Cache the new style by original hashcode
self::$cachedStyles['styleByHash'][$styleHash] = $existingStyle instanceof self ? $existingStyle : $newStyle;
}
}
if ($existingStyle) {
// there is already such cell Xf in our collection
$newXfIndexes[$oldXfIndex] = $existingStyle->getIndex();
} else {
if (!isset($newStyle)) {
// Handle bug in PHPStan, see https://github.com/phpstan/phpstan/issues/5805
// $newStyle should always be defined.
// This block might not be needed in the future
// @codeCoverageIgnoreStart
$newStyle = clone $style;
$newStyle->applyFromArray($styleArray);
// @codeCoverageIgnoreEnd
}
// we don't have such a cell Xf, need to add
$workbook->addCellXf($newStyle);
$newXfIndexes[$oldXfIndex] = $newStyle->getIndex();
}
}
// Loop through columns, rows, or cells again and update the XF index
switch ($selectionType) {
case 'COLUMN':
for ($col = $rangeStartIndexes[0]; $col <= $rangeEndIndexes[0]; ++$col) {
$columnDimension = $this->getActiveSheet()->getColumnDimensionByColumn($col);
$oldXfIndex = $columnDimension->getXfIndex();
$columnDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
}
// Disable caching of styles
self::$cachedStyles = null;
break;
case 'ROW':
for ($row = $rangeStartIndexes[1]; $row <= $rangeEndIndexes[1]; ++$row) {
$rowDimension = $this->getActiveSheet()->getRowDimension($row);
// row without explicit style should be formatted based on default style
$oldXfIndex = $rowDimension->getXfIndex() ?? 0;
$rowDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
}
// Disable caching of styles
self::$cachedStyles = null;
break;
case 'CELL':
for ($col = $rangeStartIndexes[0]; $col <= $rangeEndIndexes[0]; ++$col) {
for ($row = $rangeStartIndexes[1]; $row <= $rangeEndIndexes[1]; ++$row) {
$cell = $this->getActiveSheet()->getCell([$col, $row]);
$oldXfIndex = $cell->getXfIndex();
$cell->setXfIndex($newXfIndexes[$oldXfIndex]);
}
}
break;
}
} else {
// not a supervisor, just apply the style array directly on style object
if (isset($styleArray['fill'])) {
$this->getFill()->applyFromArray($styleArray['fill']);
}
if (isset($styleArray['font'])) {
$this->getFont()->applyFromArray($styleArray['font']);
}
if (isset($styleArray['borders'])) {
$this->getBorders()->applyFromArray($styleArray['borders']);
}
if (isset($styleArray['alignment'])) {
$this->getAlignment()->applyFromArray($styleArray['alignment']);
}
if (isset($styleArray['numberFormat'])) {
$this->getNumberFormat()->applyFromArray($styleArray['numberFormat']);
}
if (isset($styleArray['protection'])) {
$this->getProtection()->applyFromArray($styleArray['protection']);
}
if (isset($styleArray['quotePrefix'])) {
$this->quotePrefix = $styleArray['quotePrefix'];
}
}
return $this;
}
private function getOldXfIndexes(string $selectionType, array $rangeStart, array $rangeEnd, string $columnStart, string $columnEnd, array $styleArray): array
{
$oldXfIndexes = [];
switch ($selectionType) {
case 'COLUMN':
for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
$oldXfIndexes[$this->getActiveSheet()->getColumnDimensionByColumn($col)->getXfIndex()] = true;
}
foreach ($this->getActiveSheet()->getColumnIterator($columnStart, $columnEnd) as $columnIterator) {
$cellIterator = $columnIterator->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $columnCell) {
if ($columnCell !== null) {
$columnCell->getStyle()->applyFromArray($styleArray);
}
}
}
break;
case 'ROW':
for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
if ($this->getActiveSheet()->getRowDimension($row)->getXfIndex() === null) {
$oldXfIndexes[0] = true; // row without explicit style should be formatted based on default style
} else {
$oldXfIndexes[$this->getActiveSheet()->getRowDimension($row)->getXfIndex()] = true;
}
}
foreach ($this->getActiveSheet()->getRowIterator((int) $rangeStart[1], (int) $rangeEnd[1]) as $rowIterator) {
$cellIterator = $rowIterator->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $rowCell) {
if ($rowCell !== null) {
$rowCell->getStyle()->applyFromArray($styleArray);
}
}
}
break;
case 'CELL':
for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
$oldXfIndexes[$this->getActiveSheet()->getCell([$col, $row])->getXfIndex()] = true;
}
}
break;
}
return $oldXfIndexes;
}
/**
* Get Fill.
*
* @return Fill
*/
public function getFill()
{
return $this->fill;
}
/**
* Get Font.
*
* @return Font
*/
public function getFont()
{
return $this->font;
}
/**
* Set font.
*
* @return $this
*/
public function setFont(Font $font)
{
$this->font = $font;
return $this;
}
/**
* Get Borders.
*
* @return Borders
*/
public function getBorders()
{
return $this->borders;
}
/**
* Get Alignment.
*
* @return Alignment
*/
public function getAlignment()
{
return $this->alignment;
}
/**
* Get Number Format.
*
* @return NumberFormat
*/
public function getNumberFormat()
{
return $this->numberFormat;
}
/**
* Get Conditional Styles. Only used on supervisor.
*
* @return Conditional[]
*/
public function getConditionalStyles()
{
return $this->getActiveSheet()->getConditionalStyles($this->getActiveCell());
}
/**
* Set Conditional Styles. Only used on supervisor.
*
* @param Conditional[] $conditionalStyleArray Array of conditional styles
*
* @return $this
*/
public function setConditionalStyles(array $conditionalStyleArray)
{
$this->getActiveSheet()->setConditionalStyles($this->getSelectedCells(), $conditionalStyleArray);
return $this;
}
/**
* Get Protection.
*
* @return Protection
*/
public function getProtection()
{
return $this->protection;
}
/**
* Get quote prefix.
*
* @return bool
*/
public function getQuotePrefix()
{
if ($this->isSupervisor) {
return $this->getSharedComponent()->getQuotePrefix();
}
return $this->quotePrefix;
}
/**
* Set quote prefix.
*
* @param bool $quotePrefix
*
* @return $this
*/
public function setQuotePrefix($quotePrefix)
{
if ($quotePrefix == '') {
$quotePrefix = false;
}
if ($this->isSupervisor) {
$styleArray = ['quotePrefix' => $quotePrefix];
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
} else {
$this->quotePrefix = (bool) $quotePrefix;
}
return $this;
}
/**
* Get hash code.
*
* @return string Hash code
*/
public function getHashCode()
{
return md5(
$this->fill->getHashCode() .
$this->font->getHashCode() .
$this->borders->getHashCode() .
$this->alignment->getHashCode() .
$this->numberFormat->getHashCode() .
$this->protection->getHashCode() .
($this->quotePrefix ? 't' : 'f') .
__CLASS__
);
}
/**
* Get own index in style collection.
*
* @return int
*/
public function getIndex()
{
return $this->index;
}
/**
* Set own index in style collection.
*
* @param int $index
*/
public function setIndex($index): void
{
$this->index = $index;
}
protected function exportArray1(): array
{
$exportedArray = [];
$this->exportArray2($exportedArray, 'alignment', $this->getAlignment());
$this->exportArray2($exportedArray, 'borders', $this->getBorders());
$this->exportArray2($exportedArray, 'fill', $this->getFill());
$this->exportArray2($exportedArray, 'font', $this->getFont());
$this->exportArray2($exportedArray, 'numberFormat', $this->getNumberFormat());
$this->exportArray2($exportedArray, 'protection', $this->getProtection());
$this->exportArray2($exportedArray, 'quotePrefx', $this->getQuotePrefix());
return $exportedArray;
}
}

View File

@@ -0,0 +1,175 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style;
use PhpOffice\PhpSpreadsheet\IComparable;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
abstract class Supervisor implements IComparable
{
/**
* Supervisor?
*
* @var bool
*/
protected $isSupervisor;
/**
* Parent. Only used for supervisor.
*
* @var Spreadsheet|Supervisor
*/
protected $parent;
/**
* Parent property name.
*
* @var null|string
*/
protected $parentPropertyName;
/**
* Create a new Supervisor.
*
* @param bool $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false)
{
// Supervisor?
$this->isSupervisor = $isSupervisor;
}
/**
* Bind parent. Only used for supervisor.
*
* @param Spreadsheet|Supervisor $parent
* @param null|string $parentPropertyName
*
* @return $this
*/
public function bindParent($parent, $parentPropertyName = null)
{
$this->parent = $parent;
$this->parentPropertyName = $parentPropertyName;
return $this;
}
/**
* Is this a supervisor or a cell style component?
*
* @return bool
*/
public function getIsSupervisor()
{
return $this->isSupervisor;
}
/**
* Get the currently active sheet. Only used for supervisor.
*
* @return Worksheet
*/
public function getActiveSheet()
{
return $this->parent->getActiveSheet();
}
/**
* Get the currently active cell coordinate in currently active sheet.
* Only used for supervisor.
*
* @return string E.g. 'A1'
*/
public function getSelectedCells()
{
return $this->getActiveSheet()->getSelectedCells();
}
/**
* Get the currently active cell coordinate in currently active sheet.
* Only used for supervisor.
*
* @return string E.g. 'A1'
*/
public function getActiveCell()
{
return $this->getActiveSheet()->getActiveCell();
}
/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone()
{
$vars = get_object_vars($this);
foreach ($vars as $key => $value) {
if ((is_object($value)) && ($key != 'parent')) {
$this->$key = clone $value;
} else {
$this->$key = $value;
}
}
}
/**
* Export style as array.
*
* Available to anything which extends this class:
* Alignment, Border, Borders, Color, Fill, Font,
* NumberFormat, Protection, and Style.
*/
final public function exportArray(): array
{
return $this->exportArray1();
}
/**
* Abstract method to be implemented in anything which
* extends this class.
*
* This method invokes exportArray2 with the names and values
* of all properties to be included in output array,
* returning that array to exportArray, then to caller.
*/
abstract protected function exportArray1(): array;
/**
* Populate array from exportArray1.
* This method is available to anything which extends this class.
* The parameter index is the key to be added to the array.
* The parameter objOrValue is either a primitive type,
* which is the value added to the array,
* or a Style object to be recursively added via exportArray.
*
* @param mixed $objOrValue
*/
final protected function exportArray2(array &$exportedArray, string $index, $objOrValue): void
{
if ($objOrValue instanceof self) {
$exportedArray[$index] = $objOrValue->exportArray();
} else {
$exportedArray[$index] = $objOrValue;
}
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor.
*
* @return mixed
*/
abstract public function getSharedComponent();
/**
* Build style array from subcomponents.
*
* @param array $array
*
* @return array
*/
abstract public function getStyleArray($array);
}