1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-13 17:34:04 +02:00

Add ability to call Config::setOptions() with single array

This commit is contained in:
Oliver Vogel
2024-05-15 17:24:24 +02:00
parent 62a869ca48
commit a12b646f82
2 changed files with 46 additions and 1 deletions

View File

@@ -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<mixed> $options
* @return array<string, mixed>
*/
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];
}
}

View File

@@ -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);
}
}