Skip to content

Commit

Permalink
Adjust colour validation calls
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBaker committed Nov 1, 2020
1 parent 212fc75 commit 613a0b9
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/PhpSpreadsheet/Style/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +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';
const VALIDATE_ARGB_SIZE = 8;
const VALIDATE_RGB_SIZE = 6;
const VALIDATE_COLOR_VALUE = '/^[A-F0-9]{%d}$/i';

/**
* Indexed colors array.
Expand Down Expand Up @@ -62,7 +63,7 @@ public function __construct($colorValue = self::COLOR_BLACK, $isSupervisor = fal

// Initialise values
if (!$isConditional) {
$this->argb = $this->validateARGB($colorValue) ? $colorValue : self::COLOR_BLACK;
$this->argb = $this->validateColour($colorValue) ? $colorValue : self::COLOR_BLACK;
}
}

Expand Down Expand Up @@ -137,10 +138,10 @@ public function getARGB(): ?string
return $this->argb;
}

private function validateARGB(string $colorValue): bool
private function validateColour(string $colorValue, int $size): bool
{
return in_array(ucfirst($colorValue), self::NAMED_COLORS) ||
preg_match(self::VALIDATE_ARGB, $colorValue);
preg_match(sprintf(self::VALIDATE_COLOR_VALUE, $size), $colorValue);
}

/**
Expand All @@ -154,7 +155,7 @@ public function setARGB(?string $colorValue)
{
if ($colorValue === '' || $colorValue === null) {
$colorValue = self::COLOR_BLACK;
} elseif (!$this->validateARGB($colorValue)) {
} elseif (!$this->validateColour($colorValue, self::VALIDATE_ARGB_SIZE)) {
return $this;
}

Expand Down Expand Up @@ -182,12 +183,6 @@ public function getRGB(): ?string
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.
*
Expand All @@ -199,7 +194,7 @@ public function setRGB(?string $colorValue)
{
if ($colorValue === '' || $colorValue === null) {
$colorValue = '000000';
} elseif (!$this->validateRGB($colorValue)) {
} elseif (!$this->validateColour($colorValue, self::VALIDATE_RGB_SIZE)) {
return $this;
}

Expand Down

0 comments on commit 613a0b9

Please sign in to comment.