From 633e4a284e70d3635241b85acd6e7ddcd9b6b457 Mon Sep 17 00:00:00 2001 From: Brabli <67018167+Brabli@users.noreply.github.com> Date: Tue, 30 May 2023 17:18:33 +0100 Subject: [PATCH] Replace 'palletes' with 'colours' which is a bit more intuitive and is also spelt correctly. --- README.md | 12 ++--- config/packages/pcm_icon.yaml | 8 ++-- src/DependencyInjection/Configuration.php | 2 +- src/DependencyInjection/PcmIconExtension.php | 2 +- src/Twig/Functions/IconExtension.php | 50 ++++++++++---------- tests/Config/ConfigurationTest.php | 2 +- tests/Twig/Functions/IconExtensionTest.php | 26 +++++----- 7 files changed, 50 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 091f983..737a9ec 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Example config: pcm_icon: directories: - '%kernel.project_dir%/public/icons' - palletes: + colours: primary: fill: 'fill-primary' stroke: 'stroke-primary' @@ -33,10 +33,10 @@ pcm_icon: `directories` - Which directories to look in for icons. -`palletes` - Custom colour palletes to use when colouring icons. Because this extension relies on Tailwind classes for colouring we must specify all the classes. This is annoyingly verbose, but you can just copy this template: +`colours` - Custom colours to use when colouring icons. Because this extension relies on Tailwind classes for colouring we must specify all the classes. This is a bit awkward, but you can just copy this template replacing COLOUR as appropriate: ```yaml -PALLETE_NAME: +COLOUR: fill: 'fill-COLOUR' stroke: 'stroke-COLOUR' fill-hover: 'hover:fill-COLOUR' @@ -53,7 +53,7 @@ PALLETE_NAME: `size (int)` Size of the icon in pixels -`colour (string)` Name of the main colour pallete to use. Defaults to `"primary"`, which is a pallete that uses the project's primary colour. If however you are not using `primary` to set a Tailwind primary colour in your project, you can instead set the default colour of the icon by changing what colour classes the primary pallete uses. EG: +`colour (string)` Name of the colour to use. Defaults to `primary` as it assumes you have a Tailwind colour set up called `primary`. If however you are not using `primary` to set a Tailwind primary colour in your project, you can instead set the default colour of the icon by changing what colour classes the primary colour uses. EG: ```yaml primary: fill: 'fill-purple-700' @@ -64,9 +64,9 @@ primary: stroke-group-hover: 'group-hover:stroke-purple-700' ``` -`hover (string)` Name of the colour pallete to use when the icon is hovered over +`hover (string)` Name of the colour to use when the icon is hovered over -`classes (string[])` Additional classes to add to the icon. This can cause Tailwind class collisions, so use with caution if at all. +`classes (string[])` Additional classes to add to the icon. This can cause Tailwind class collisions, so only use it as a last resort. ## Reminders Remember to add `./config/packages/*.yaml` as a value in your Tailwind config content array if it does not already exist. This will ensure Tailwind classes inside of the config file get compiled. diff --git a/config/packages/pcm_icon.yaml b/config/packages/pcm_icon.yaml index d610470..2c4f5e3 100644 --- a/config/packages/pcm_icon.yaml +++ b/config/packages/pcm_icon.yaml @@ -2,18 +2,16 @@ when@dev: pcm_icon: directories: - '%kernel.project_dir%/public/icons' - palletes: [] + colours: [] when@prod: pcm_icon: directories: - '%kernel.project_dir%/public/icons' - palletes: [] + colours: [] when@test: pcm_icon: directories: - '%kernel.project_dir%/tests/icons' - palletes: - - [] - + colours: [] diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 8bd3135..b6ccc3d 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -19,7 +19,7 @@ class Configuration implements ConfigurationInterface ->arrayNode('directories') ->scalarPrototype()->end() ->end() - ->arrayNode('palletes') // Akshewally it is defined + ->arrayNode('colours') // Akshewally it is defined ->variablePrototype()->end() ->end() ->end() diff --git a/src/DependencyInjection/PcmIconExtension.php b/src/DependencyInjection/PcmIconExtension.php index 64a8e3e..95ea0fa 100644 --- a/src/DependencyInjection/PcmIconExtension.php +++ b/src/DependencyInjection/PcmIconExtension.php @@ -26,6 +26,6 @@ class PcmIconExtension extends Extension $definition = $container->getDefinition('pcm_icon.icon_extension'); $definition->addArgument($config['directories']); - $definition->addArgument($config['palletes']); + $definition->addArgument($config['colours']); } } diff --git a/src/Twig/Functions/IconExtension.php b/src/Twig/Functions/IconExtension.php index 51e6d02..380b9e1 100644 --- a/src/Twig/Functions/IconExtension.php +++ b/src/Twig/Functions/IconExtension.php @@ -20,29 +20,29 @@ final class IconExtension extends AbstractExtension 'classes' => [], ]; - public function __construct(private array $directories, private array $palletes) + public function __construct(private array $directories, private array $colours) { - if (empty($this->palletes)) { - throw new \InvalidArgumentException('Palletes array must contain at least one pallet!'); + if (empty($this->colours)) { + throw new \InvalidArgumentException('Colours array must contain at least one colour!'); } - $palletesContainNonarray = array_reduce($this->palletes, + $coloursContainsNonArray = array_reduce($this->colours, fn($notArray, $path) => $notArray || !is_array($path)); - if ($palletesContainNonarray) { - throw new \TypeError('Palletes array must only contain arrays!'); + if ($coloursContainsNonArray) { + throw new \TypeError('Colours array must only contain arrays!'); } - foreach ($this->palletes as $pallete) { + foreach ($this->colours as $colour) { if (!( - array_key_exists('stroke', $pallete) && - array_key_exists('fill', $pallete) && - array_key_exists('fill-hover', $pallete) && - array_key_exists('stroke-hover', $pallete) && - array_key_exists('fill-group-hover', $pallete) && - array_key_exists('stroke-group-hover', $pallete)) + array_key_exists('stroke', $colour) && + array_key_exists('fill', $colour) && + array_key_exists('fill-hover', $colour) && + array_key_exists('stroke-hover', $colour) && + array_key_exists('fill-group-hover', $colour) && + array_key_exists('stroke-group-hover', $colour)) ) { - throw new \Exception('Palletes must contain a "stroke" and "fill" key!'); + throw new \Exception('Colours must contain a "stroke" and "fill" key!'); } } } @@ -66,8 +66,8 @@ final class IconExtension extends AbstractExtension * 'icon' => (string) REQUIRED Which icon to use * 'title' => (?string) Text to appear on mouse hover * 'size' => (int) Height and width in px - * 'colour' => (string) Main colour pallete - * 'hover' => (?string) Hover colour pallete + * 'colour' => (string) Main colour + * 'hover' => (?string) Hover colour * 'classes' => (array) Additional classes to add to the icon. * Use with caution as this can potentially * cause Tailwind class conflicts! @@ -159,24 +159,24 @@ final class IconExtension extends AbstractExtension private function getColourClasses(string $primaryColour, ?string $hoverColour): string { - $mainPallete = $this->getPallete($primaryColour); - $colourClasses = "{$mainPallete['stroke']} {$mainPallete['fill']}"; + $mainColour = $this->getColour($primaryColour); + $colourClasses = "{$mainColour['stroke']} {$mainColour['fill']}"; if (null !== $hoverColour) { - $hoverPallete = $this->getPallete($hoverColour); - $colourClasses .= " cursor-pointer {$hoverPallete['stroke-hover']} {$hoverPallete['fill-hover']} {$hoverPallete['stroke-group-hover']} {$hoverPallete['fill-group-hover']}"; + $hoverColour = $this->getColour($hoverColour); + $colourClasses .= " cursor-pointer {$hoverColour['stroke-hover']} {$hoverColour['fill-hover']} {$hoverColour['stroke-group-hover']} {$hoverColour['fill-group-hover']}"; } return $colourClasses; } - private function getPallete(string $palleteName): array + private function getColour(string $colourName): array { - if (array_key_exists($palleteName, $this->palletes)) { - return $this->palletes[$palleteName]; + if (array_key_exists($colourName, $this->colours)) { + return $this->colours[$colourName]; } - throw new PalleteNotFound("The pallete '$palleteName' was not found!"); + throw new ColourNotFound("The colour \"$colourName\" was not found!"); } private function addClassesToSvg(string $svg, string $classes): string @@ -239,4 +239,4 @@ final class IconExtension extends AbstractExtension class IconNotFound extends \Exception {}; -class PalleteNotFound extends \Exception {}; +class ColourNotFound extends \Exception {}; diff --git a/tests/Config/ConfigurationTest.php b/tests/Config/ConfigurationTest.php index 72b1738..0cb8c16 100644 --- a/tests/Config/ConfigurationTest.php +++ b/tests/Config/ConfigurationTest.php @@ -48,7 +48,7 @@ class ConfigurationTest extends TestCase './', ], - 'palletes' => [ + 'colours' => [ 'primary' => [ 'fill' => 'fill-primary', 'stroke' => 'stroke-primary', diff --git a/tests/Twig/Functions/IconExtensionTest.php b/tests/Twig/Functions/IconExtensionTest.php index c0385f9..bcee566 100644 --- a/tests/Twig/Functions/IconExtensionTest.php +++ b/tests/Twig/Functions/IconExtensionTest.php @@ -6,14 +6,14 @@ namespace Pcm\IconBundle\Tests\Twig\Functions; use Pcm\IconBundle\Twig\Functions\IconExtension; use Pcm\IconBundle\Twig\Functions\IconNotFound; -use Pcm\IconBundle\Twig\Functions\PalleteNotFound; +use Pcm\IconBundle\Twig\Functions\ColourNotFound; use PHPUnit\Framework\TestCase; class IconExtensionTest extends TestCase { private const ICON = 'test'; - private const PALLETES = [ + private const COLOURS = [ 'primary' => [ 'fill' => 'fill-primary', 'stroke' => 'stroke-primary', @@ -39,7 +39,7 @@ class IconExtensionTest extends TestCase protected function setUp(): void { - $this->icon = new IconExtension(['tests/icons'], self::PALLETES); + $this->icon = new IconExtension(['tests/icons'], self::COLOURS); } public function testInstanceOf(): void @@ -151,19 +151,19 @@ class IconExtensionTest extends TestCase $this->assertSame(1, $timesMatched); } - public function testThrowsIfPalletsIsEmpty(): void + public function testThrowsIfColoursIsEmpty(): void { $this->expectException(\InvalidArgumentException::class); new IconExtension(['/'], []); } - public function testThrowsIfPalletesContainsNonArray(): void + public function testThrowsIfColoursContainsNonArray(): void { $this->expectException(\TypeError::class); new IconExtension(['/'], [99]); } - public function testThrowsIfPalletesContainsNonArrayInbetweenArrays(): void + public function testThrowsIfColoursContainsNonArrayInbetweenArrays(): void { $this->expectException(\TypeError::class); new IconExtension(['/'], [[], 99, []]); @@ -241,26 +241,26 @@ class IconExtensionTest extends TestCase $this->assertDoesNotMatchRegularExpression('/fill:\s*rgb\(0,\s*0,\s*0\)\s*/', $content); } - public function testThrowsIfColourPalleteIsNotFound(): void + public function testThrowsIfColourIsNotFound(): void { - $this->expectException(PalleteNotFound::class); + $this->expectException(ColourNotFound::class); $this->icon->renderIcon(['icon' => self::ICON, 'colour' => 'red']); } - public function testSvgClassContainsPalleteClasses(): void + public function testSvgClassContainsColourClasses(): void { $contents = $this->icon->renderIcon(['icon' => self::ICON, 'colour' => 'white']); $this->assertMatchesRegularExpression('//', $contents); $this->assertMatchesRegularExpression('//', $contents); } - public function testThrowsIfHoverPalleteIsNotFound(): void + public function testThrowsIfHoverColourIsNotFound(): void { - $this->expectException(PalleteNotFound::class); + $this->expectException(ColourNotFound::class); $this->icon->renderIcon(['icon' => self::ICON, 'hover' => 'red']); } - public function testSvgClassContainsHoverPalleteClasses(): void + public function testSvgClassContainsHoverColourClasses(): void { $contents = $this->icon->renderIcon(['icon' => self::ICON, 'hover' => 'white']); $this->assertMatchesRegularExpression('//', $contents); @@ -270,7 +270,7 @@ class IconExtensionTest extends TestCase $this->assertMatchesRegularExpression('//', $contents); } - public function testSvgClassContainsHoverAndColourPalleteClasses(): void + public function testSvgClassContainsHoverAndColourClasses(): void { $contents = $this->icon->renderIcon(['icon' => self::ICON, 'hover' => 'white', 'colour' => 'primary']); $this->assertMatchesRegularExpression('//', $contents);