mirror of
https://github.com/Intervention/image.git
synced 2025-08-14 09:54:13 +02:00
Add ability to call Config::setOptions() with single array
This commit is contained in:
@@ -32,7 +32,7 @@ class Config
|
|||||||
*/
|
*/
|
||||||
public function setOptions(mixed ...$options): self
|
public function setOptions(mixed ...$options): self
|
||||||
{
|
{
|
||||||
foreach ($options as $name => $value) {
|
foreach ($this->prepareOptions($options) as $name => $value) {
|
||||||
if (!property_exists($this, $name)) {
|
if (!property_exists($this, $name)) {
|
||||||
throw new InputException('Property ' . $name . ' does not exists for ' . $this::class . '.');
|
throw new InputException('Property ' . $name . ' does not exists for ' . $this::class . '.');
|
||||||
}
|
}
|
||||||
@@ -42,4 +42,32 @@ class Config
|
|||||||
|
|
||||||
return $this;
|
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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -63,4 +63,21 @@ final class ConfigTest extends BaseTestCase
|
|||||||
$this->assertFalse($result->decodeAnimation);
|
$this->assertFalse($result->decodeAnimation);
|
||||||
$this->assertEquals('000', $result->blendingColor);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user