Skip to content

Commit 5b2f9b2

Browse files
committed
Add ThemeColor
1 parent f53561d commit 5b2f9b2

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This release marked the addition of return type declarations (PHP 7+).
1212
- Set complex type in template @troosan #1565
1313
- Add support for page vertical alignment. @troosan #672 #1569
1414
- Length validation with `PhpOffice\PhpWord\Style\Lengths\{Absolute, Auto, Percent}` @0b10011 #1669
15-
- Color validation with `PhpOffice\PhpWord\Style\Colors\{HighlightColor, Hex, Rgb, SystemColor}` @0b10011 #1669
15+
- Color validation with `PhpOffice\PhpWord\Style\Colors\{HighlightColor, Hex, Rgb, SystemColor, ThemeColor}` @0b10011 #1669
1616
- BorderStyle validation with `PhpOffice\PhpWord\Style\BorderStyle` @0b10011 #1669
1717

1818
### Fixed

src/PhpWord/Style/Colors/Color.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ public static function fromMixed($value = null): AbstractColor
2121
} elseif (is_string($value)) {
2222
if (Hex::isValid($value)) {
2323
return new Hex($value);
24-
} elseif (SystemColor::isValid($value)) {
25-
return new SystemColor($value);
26-
} elseif (HighlightColor::isValid($value)) {
27-
return new HighlightColor($value);
24+
} elseif (ThemeColor::isValid($value)) {
25+
return new ThemeColor($value);
2826
}
2927
}
3028

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PhpOffice\PhpWord\Style\Colors;
5+
6+
use PhpOffice\PhpWord\Exception\Exception;
7+
8+
final class ThemeColor extends AbstractColor implements NamedColorInterface
9+
{
10+
/**
11+
* Taken from https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.drawing.colorscheme?view=openxml-2.8.1
12+
* @todo Check if 'extLst' (Extension List) can add additional colors to this list.
13+
* @var array
14+
*/
15+
private static $allowedColors = array(
16+
'dk1' => true,
17+
'lt1' => true,
18+
'dk2' => true,
19+
'lt2' => true,
20+
'accent1' => true,
21+
'accent2' => true,
22+
'accent3' => true,
23+
'accent4' => true,
24+
'accent5' => true,
25+
'accent6' => true,
26+
'hlink' => true,
27+
'folHlink' => true,
28+
);
29+
30+
private $name;
31+
32+
public function __construct(string $name)
33+
{
34+
if (!static::isValid($name)) {
35+
throw new Exception(sprintf("Provided color must be a valid theme color. '%s' provided. Allowed: %s", $name, implode(', ', array_keys(self::$allowedColors))));
36+
}
37+
38+
$this->name = $name;
39+
}
40+
41+
public function getName(): string
42+
{
43+
return $this->name;
44+
}
45+
46+
public static function isValid(string $color): bool
47+
{
48+
return array_key_exists($color, self::$allowedColors);
49+
}
50+
}

tests/PhpWord/Style/Colors/ColorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public function testConversions()
2727
{
2828
// Prepare test values [ original, expected ]
2929
$values = array(
30-
array('yellow', HighlightColor::class, 'yellow'),
31-
array('window', SystemColor::class, 'window'),
30+
array('lt1', ThemeColor::class, 'lt1'),
3231
array('a0b', Hex::class, 'AA00BB'),
3332
array('aB01cD', Hex::class, 'AB01CD'),
3433
array('', AbstractColor::class, null),

0 commit comments

Comments
 (0)