diff --git a/src/Converter.php b/src/Converter.php index d7bb5b1..66be879 100644 --- a/src/Converter.php +++ b/src/Converter.php @@ -29,7 +29,7 @@ private function addConverter(Convertible $converter) public function convert(Color $color, string $fqcn): Color { - foreach ($this->getConverterChain(get_class($color), $fqcn) as $converter) { + foreach ($this->getConverterChain([], \get_class($color), $fqcn) as $converter) { $color = $converter->convert($color); } @@ -37,34 +37,26 @@ public function convert(Color $color, string $fqcn): Color } /** - * @param string $fqcnFrom - * @param string $fqcnTo + * @param Convertible[] $converters + * @param string $fromFqcn + * @param string $toFqcn * * @return Convertible[] */ - private function getConverterChain(string $fqcnFrom, string $fqcnTo): array + private function getConverterChain(array $converters, string $fromFqcn, string $toFqcn): array { - /** @var Convertible[] $converters */ - $converters = []; - - do { - $converter = $this->getConverterTo($fqcnTo); - $fqcnTo = $converter::supportsFrom(); + foreach ($this->converters as $converter) { + if ($converter::supportsFrom() === $fromFqcn) { + $converters[] = $converter; - $converters[] = $converter; - } while ($converter::supportsFrom() !== $fqcnFrom); + if ($converter::supportsTo() === $toFqcn) { + return $converters; + } - return array_reverse($converters); - } - - private function getConverterTo(string $fqcn): Convertible - { - foreach ($this->converters as $converter) { - if ($converter::supportsTo() === $fqcn) { - return $converter; + return $this->getConverterChain($converters, $converter::supportsTo(), $toFqcn); } } - throw new \RuntimeException('no converter found getConverterTo'); + return $converters; } } diff --git a/src/Converter/HEXToRGBConverter.php b/src/Converter/HEXToRGBConverter.php new file mode 100644 index 0000000..ed8df48 --- /dev/null +++ b/src/Converter/HEXToRGBConverter.php @@ -0,0 +1,35 @@ +getRed()); + $green = hexdec($color->getGreen()); + $blue = hexdec($color->getBlue()); + + return new RGB($red, $green, $blue); + } + + public static function supportsFrom(): string + { + return HEX::class; + } + + public static function supportsTo(): string + { + return RGB::class; + } +}