diff --git a/src/PhpSpreadsheet/Comment.php b/src/PhpSpreadsheet/Comment.php index 31f76640..12fb0e22 100644 --- a/src/PhpSpreadsheet/Comment.php +++ b/src/PhpSpreadsheet/Comment.php @@ -3,6 +3,8 @@ namespace PhpOffice\PhpSpreadsheet; use PhpOffice\PhpSpreadsheet\RichText\RichText; +use PhpOffice\PhpSpreadsheet\Style\Alignment; +use PhpOffice\PhpSpreadsheet\Style\Color; class Comment implements IComparable { @@ -58,7 +60,7 @@ class Comment implements IComparable /** * Comment fill color. * - * @var Style\Color + * @var Color */ private $fillColor; @@ -77,8 +79,8 @@ class Comment implements IComparable // Initialise variables $this->author = 'Author'; $this->text = new RichText(); - $this->fillColor = new Style\Color('FFFFFFE1'); - $this->alignment = Style\Alignment::HORIZONTAL_GENERAL; + $this->fillColor = new Color('FFFFFFE1'); + $this->alignment = Alignment::HORIZONTAL_GENERAL; } /** @@ -86,7 +88,7 @@ class Comment implements IComparable * * @return string */ - public function getAuthor() + public function getAuthor(): string { return $this->author; } @@ -98,7 +100,7 @@ class Comment implements IComparable * * @return $this */ - public function setAuthor($author) + public function setAuthor(string $author) { $this->author = $author; @@ -110,7 +112,7 @@ class Comment implements IComparable * * @return RichText */ - public function getText() + public function getText(): RichText { return $this->text; } @@ -118,11 +120,13 @@ class Comment implements IComparable /** * Set Rich text comment. * + * @param RichText $text + * * @return $this */ - public function setText(RichText $pValue) + public function setText(RichText $text) { - $this->text = $pValue; + $this->text = $text; return $this; } @@ -132,7 +136,7 @@ class Comment implements IComparable * * @return string */ - public function getWidth() + public function getWidth(): string { return $this->width; } @@ -140,11 +144,11 @@ class Comment implements IComparable /** * Set comment width (CSS style, i.e. XXpx or YYpt). * - * @param string $width + * @param string $width including units (px or pt) * * @return $this */ - public function setWidth($width) + public function setWidth(string $width) { $this->width = $width; @@ -156,7 +160,7 @@ class Comment implements IComparable * * @return string */ - public function getHeight() + public function getHeight(): string { return $this->height; } @@ -164,13 +168,13 @@ class Comment implements IComparable /** * Set comment height (CSS style, i.e. XXpx or YYpt). * - * @param string $value + * @param string $height including units (px or pt) * * @return $this */ - public function setHeight($value) + public function setHeight(string $height) { - $this->height = $value; + $this->height = $height; return $this; } @@ -180,7 +184,7 @@ class Comment implements IComparable * * @return string */ - public function getMarginLeft() + public function getMarginLeft(): string { return $this->marginLeft; } @@ -188,13 +192,13 @@ class Comment implements IComparable /** * Set left margin (CSS style, i.e. XXpx or YYpt). * - * @param string $value + * @param string $margin including units (px or pt) * * @return $this */ - public function setMarginLeft($value) + public function setMarginLeft(string $margin) { - $this->marginLeft = $value; + $this->marginLeft = $margin; return $this; } @@ -204,7 +208,7 @@ class Comment implements IComparable * * @return string */ - public function getMarginTop() + public function getMarginTop(): string { return $this->marginTop; } @@ -212,13 +216,13 @@ class Comment implements IComparable /** * Set top margin (CSS style, i.e. XXpx or YYpt). * - * @param string $value + * @param string $margin including units (px or pt) * * @return $this */ - public function setMarginTop($value) + public function setMarginTop(string $margin) { - $this->marginTop = $value; + $this->marginTop = $margin; return $this; } @@ -228,7 +232,7 @@ class Comment implements IComparable * * @return bool */ - public function getVisible() + public function getVisible(): bool { return $this->visible; } @@ -236,13 +240,27 @@ class Comment implements IComparable /** * Set comment default visibility. * - * @param bool $value + * @param bool $visibility * * @return $this */ - public function setVisible($value) + public function setVisible(bool $visibility) { - $this->visible = $value; + $this->visible = $visibility; + + return $this; + } + + /** + * Set fill color. + * + * @param Color $color + * + * @return $this + */ + public function setFillColor(Color $color) + { + $this->fillColor = $color; return $this; } @@ -250,9 +268,9 @@ class Comment implements IComparable /** * Get fill color. * - * @return Style\Color + * @return Color */ - public function getFillColor() + public function getFillColor(): Color { return $this->fillColor; } @@ -260,11 +278,11 @@ class Comment implements IComparable /** * Set Alignment. * - * @param string $alignment see Style\Alignment::HORIZONTAL_* + * @param string $alignment see Alignment::HORIZONTAL_* * * @return $this */ - public function setAlignment($alignment) + public function setAlignment(string $alignment): string { $this->alignment = $alignment; diff --git a/src/PhpSpreadsheet/IOFactory.php b/src/PhpSpreadsheet/IOFactory.php index ab04e969..f3ce6a30 100644 --- a/src/PhpSpreadsheet/IOFactory.php +++ b/src/PhpSpreadsheet/IOFactory.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheet; use PhpOffice\PhpSpreadsheet\Shared\File; +use PhpOffice\PhpSpreadsheet\Writer\IWriter; /** * Factory to create readers and writers easily. @@ -41,7 +42,7 @@ abstract class IOFactory * * @return Writer\IWriter */ - public static function createWriter(Spreadsheet $spreadsheet, $writerType) + public static function createWriter(Spreadsheet $spreadsheet, string $writerType): Writer\IWriter { if (!isset(self::$writers[$writerType])) { throw new Writer\Exception("No writer found for type $writerType"); @@ -60,7 +61,7 @@ abstract class IOFactory * * @return Reader\IReader */ - public static function createReader($readerType) + public static function createReader(string $readerType): Reader\IReader { if (!isset(self::$readers[$readerType])) { throw new Reader\Exception("No reader found for type $readerType"); @@ -79,7 +80,7 @@ abstract class IOFactory * * @return Spreadsheet */ - public static function load($pFilename) + public static function load(string $pFilename): Spreadsheet { $reader = self::createReaderForFile($pFilename); @@ -93,7 +94,7 @@ abstract class IOFactory * * @return string */ - public static function identify($pFilename) + public static function identify(string $pFilename): string { $reader = self::createReaderForFile($pFilename); $className = get_class($reader); @@ -110,7 +111,7 @@ abstract class IOFactory * * @return Reader\IReader */ - public static function createReaderForFile($filename) + public static function createReaderForFile(string $filename): Reader\IReader { File::assertFile($filename); @@ -147,7 +148,7 @@ abstract class IOFactory * * @return null|string */ - private static function getReaderTypeFromExtension($filename) + private static function getReaderTypeFromExtension(string $filename): ?string { $pathinfo = pathinfo($filename); if (!isset($pathinfo['extension'])) { @@ -191,7 +192,7 @@ abstract class IOFactory * @param string $writerType * @param string $writerClass */ - public static function registerWriter($writerType, $writerClass): void + public static function registerWriter(string $writerType, string $writerClass): void { if (!is_a($writerClass, Writer\IWriter::class, true)) { throw new Writer\Exception('Registered writers must implement ' . Writer\IWriter::class); @@ -206,7 +207,7 @@ abstract class IOFactory * @param string $readerType * @param string $readerClass */ - public static function registerReader($readerType, $readerClass): void + public static function registerReader(string $readerType, string $readerClass): void { if (!is_a($readerClass, Reader\IReader::class, true)) { throw new Reader\Exception('Registered readers must implement ' . Reader\IReader::class); diff --git a/src/PhpSpreadsheet/Settings.php b/src/PhpSpreadsheet/Settings.php index cfa50573..1b760578 100644 --- a/src/PhpSpreadsheet/Settings.php +++ b/src/PhpSpreadsheet/Settings.php @@ -63,7 +63,7 @@ class Settings * * @return bool Success or failure */ - public static function setLocale($locale) + public static function setLocale(string $locale) { return Calculation::getInstance()->setLocale($locale); } @@ -74,7 +74,7 @@ class Settings * @param string $rendererClass Class name of the chart renderer * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph */ - public static function setChartRenderer($rendererClass): void + public static function setChartRenderer(string $rendererClass): void { if (!is_a($rendererClass, IRenderer::class, true)) { throw new Exception('Chart renderer must implement ' . IRenderer::class); @@ -89,7 +89,7 @@ class Settings * @return null|string Class name of the chart renderer * eg: PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph */ - public static function getChartRenderer() + public static function getChartRenderer(): string { return self::$chartRenderer; } @@ -113,7 +113,7 @@ class Settings * * @return int Default options for libxml loader */ - public static function getLibXmlLoaderOptions() + public static function getLibXmlLoaderOptions(): int { if (self::$libXmlLoaderOptions === null && defined('LIBXML_DTDLOAD')) { self::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR); @@ -144,7 +144,7 @@ class Settings * * @return bool $state */ - public static function getLibXmlDisableEntityLoader() + public static function getLibXmlDisableEntityLoader(): bool { return self::$libXmlDisableEntityLoader; } @@ -162,7 +162,7 @@ class Settings * * @return CacheInterface */ - public static function getCache() + public static function getCache(): CacheInterface { if (!self::$cache) { self::$cache = new Memory(); diff --git a/src/PhpSpreadsheet/Style/Color.php b/src/PhpSpreadsheet/Style/Color.php index d8ba08b2..78c860fb 100644 --- a/src/PhpSpreadsheet/Style/Color.php +++ b/src/PhpSpreadsheet/Style/Color.php @@ -27,6 +27,9 @@ class Color extends Supervisor const COLOR_YELLOW = 'FFFFFF00'; const COLOR_DARKYELLOW = 'FF808000'; + const VALIDATE_ARGB = '/^[A-F0-9]{8}$/i'; + const VALIDATE_RGB = '/^[A-F0-9]{6}$/i'; + /** * Indexed colors array. * @@ -44,7 +47,7 @@ class Color extends Supervisor /** * Create a new Color. * - * @param string $pARGB ARGB value for the colour + * @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 @@ -52,14 +55,14 @@ class Color extends Supervisor * Leave this value at default unless you understand exactly what * its ramifications are */ - public function __construct($pARGB = self::COLOR_BLACK, $isSupervisor = false, $isConditional = false) + public function __construct($colorValue = self::COLOR_BLACK, $isSupervisor = false, $isConditional = false) { // Supervisor? parent::__construct($isSupervisor); // Initialise values if (!$isConditional) { - $this->argb = $pARGB; + $this->argb = $this->setARGB($colorValue); } } @@ -125,7 +128,7 @@ class Color extends Supervisor * * @return string */ - public function getARGB() + public function getARGB(): ?string { if ($this->isSupervisor) { return $this->getSharedComponent()->getARGB(); @@ -134,23 +137,32 @@ class Color extends Supervisor return $this->argb; } + private function validateARGB(string $colorValue): bool + { + return in_array(ucfirst($colorValue), self::NAMED_COLORS) || + preg_match(self::VALIDATE_ARGB, $colorValue); + } + /** * Set ARGB. * - * @param string $pValue see self::COLOR_* + * @param string $colorValue ARGB value, or a named color * * @return $this */ - public function setARGB($pValue) + public function setARGB(string $colorValue) { - if ($pValue == '') { - $pValue = self::COLOR_BLACK; + if ($colorValue == '') { + $colorValue = self::COLOR_BLACK; + } elseif (!$this->validateARGB($colorValue)) { + return $this; } + if ($this->isSupervisor) { - $styleArray = $this->getStyleArray(['argb' => $pValue]); + $styleArray = $this->getStyleArray(['argb' => $colorValue]); $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); } else { - $this->argb = $pValue; + $this->argb = $colorValue; } return $this; @@ -161,7 +173,7 @@ class Color extends Supervisor * * @return string */ - public function getRGB() + public function getRGB(): ?string { if ($this->isSupervisor) { return $this->getSharedComponent()->getRGB(); @@ -170,23 +182,32 @@ class Color extends Supervisor return substr($this->argb, 2); } + private function validateRGB(string $colorValue): bool + { + return in_array(ucfirst($colorValue), self::NAMED_COLORS) || + preg_match(self::VALIDATE_RGB, $colorValue); + } + /** * Set RGB. * - * @param string $pValue RGB value + * @param string $colorValue RGB value, or a named color * * @return $this */ - public function setRGB($pValue) + public function setRGB(string $colorValue) { - if ($pValue == '') { - $pValue = '000000'; + if ($colorValue == '') { + $colorValue = '000000'; + } elseif (!$this->validateRGB($colorValue)) { + return $this; } + if ($this->isSupervisor) { - $styleArray = $this->getStyleArray(['argb' => 'FF' . $pValue]); + $styleArray = $this->getStyleArray(['argb' => 'FF' . $colorValue]); $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); } else { - $this->argb = 'FF' . $pValue; + $this->argb = 'FF' . $colorValue; } return $this; diff --git a/src/PhpSpreadsheet/Style/Style.php b/src/PhpSpreadsheet/Style/Style.php index 533a7c38..012677f4 100644 --- a/src/PhpSpreadsheet/Style/Style.php +++ b/src/PhpSpreadsheet/Style/Style.php @@ -110,7 +110,7 @@ class Style extends Supervisor * * @return Style */ - public function getSharedComponent() + public function getSharedComponent(): Style { $activeSheet = $this->getActiveSheet(); $selectedCell = $this->getActiveCell(); // e.g. 'A1'