diff --git a/src/Config.php b/src/Config.php index 4954fdff..4b69209e 100644 --- a/src/Config.php +++ b/src/Config.php @@ -32,7 +32,7 @@ class Config */ public function setOptions(mixed ...$options): self { - foreach ($options as $name => $value) { + foreach ($this->prepareOptions($options) as $name => $value) { if (!property_exists($this, $name)) { throw new InputException('Property ' . $name . ' does not exists for ' . $this::class . '.'); } @@ -42,4 +42,32 @@ class Config return $this; } + + /** + * This method makes it possible to call self::setOptions() with a single + * array instead of named parameters + * + * @param array $options + * @return array + */ + private function prepareOptions(array $options): array + { + if (count($options) === 0) { + return $options; + } + + if (count($options) > 1) { + return $options; + } + + if (!array_key_exists(0, $options)) { + return $options; + } + + if (!is_array($options[0])) { + return $options; + } + + return $options[0]; + } } diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index 6f09cdff..6a1683bd 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -63,4 +63,21 @@ final class ConfigTest extends BaseTestCase $this->assertFalse($result->decodeAnimation); $this->assertEquals('000', $result->blendingColor); } + + public function testSetOptionsWithArray(): void + { + $config = new Config(); + $result = $config->setOptions([ + 'autoOrientation' => false, + 'decodeAnimation' => false, + 'blendingColor' => 'f00', + ]); + + $this->assertFalse($config->autoOrientation); + $this->assertFalse($config->decodeAnimation); + $this->assertEquals('f00', $config->blendingColor); + $this->assertFalse($result->autoOrientation); + $this->assertFalse($result->decodeAnimation); + $this->assertEquals('f00', $result->blendingColor); + } }