generated from yiisoft/package-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ArrayDefinitionHelper::merge()
(#67)
- Loading branch information
Showing
6 changed files
with
280 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Definitions\Helpers; | ||
|
||
use Yiisoft\Definitions\ArrayDefinition; | ||
|
||
use Yiisoft\Definitions\Exception\InvalidConfigException; | ||
|
||
use function is_array; | ||
|
||
final class ArrayDefinitionHelper | ||
{ | ||
/** | ||
* @throws InvalidConfigException | ||
*/ | ||
public static function merge(array ...$configs): array | ||
{ | ||
$result = array_shift($configs) ?: []; | ||
while (!empty($configs)) { | ||
/** @var mixed $value */ | ||
foreach (array_shift($configs) as $key => $value) { | ||
if (!is_string($key)) { | ||
throw ExceptionHelper::invalidArrayDefinitionKey($key); | ||
} | ||
|
||
if (!isset($result[$key])) { | ||
/** @var mixed */ | ||
$result[$key] = $value; | ||
continue; | ||
} | ||
|
||
if ($key === ArrayDefinition::CONSTRUCTOR) { | ||
if (!is_array($value)) { | ||
throw ExceptionHelper::incorrectArrayDefinitionConstructorArguments($value); | ||
} | ||
if (!is_array($result[$key])) { | ||
throw ExceptionHelper::incorrectArrayDefinitionConstructorArguments($result[$key]); | ||
} | ||
$result[$key] = self::mergeArguments($result[$key], $value); | ||
continue; | ||
} | ||
|
||
if (str_ends_with($key, '()')) { | ||
if (!is_array($value)) { | ||
throw ExceptionHelper::incorrectArrayDefinitionMethodArguments($key, $value); | ||
} | ||
if (!is_array($result[$key])) { | ||
throw ExceptionHelper::incorrectArrayDefinitionMethodArguments($key, $result[$key]); | ||
} | ||
/** @var mixed */ | ||
$result[$key] = self::mergeArguments($result[$key], $value); | ||
continue; | ||
} | ||
|
||
/** @var mixed */ | ||
$result[$key] = $value; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public static function mergeArguments(array $argumentsA, array $argumentsB): array | ||
{ | ||
/** @var mixed $argument */ | ||
foreach ($argumentsB as $name => $argument) { | ||
/** @var mixed */ | ||
$argumentsA[$name] = $argument; | ||
} | ||
|
||
return $argumentsA; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Definitions\Helpers; | ||
|
||
use Yiisoft\Definitions\Exception\InvalidConfigException; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ExceptionHelper | ||
{ | ||
public static function invalidArrayDefinitionKey(int|string $key): InvalidConfigException | ||
{ | ||
return new InvalidConfigException( | ||
sprintf( | ||
'Invalid definition: invalid key in array definition. Only string keys are allowed, got %d.', | ||
$key, | ||
), | ||
); | ||
} | ||
|
||
public static function incorrectArrayDefinitionConstructorArguments(mixed $value): InvalidConfigException | ||
{ | ||
return new InvalidConfigException( | ||
sprintf( | ||
'Invalid definition: incorrect constructor arguments. Expected array, got %s.', | ||
get_debug_type($value) | ||
) | ||
); | ||
} | ||
|
||
public static function incorrectArrayDefinitionMethodArguments(string $key, mixed $value): InvalidConfigException | ||
{ | ||
return new InvalidConfigException( | ||
sprintf( | ||
'Invalid definition: incorrect method "%s" arguments. Expected array, got "%s". ' . | ||
'Probably you should wrap them into square brackets.', | ||
$key, | ||
get_debug_type($value), | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Helpers; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Definitions\Exception\InvalidConfigException; | ||
use Yiisoft\Definitions\Helpers\ArrayDefinitionHelper; | ||
|
||
final class ArrayDefinitionHelperTest extends TestCase | ||
{ | ||
public function dataBase(): array | ||
{ | ||
return [ | ||
'without configs' => [ | ||
[], | ||
[], | ||
], | ||
'one config' => [ | ||
['$value' => 42], | ||
[ | ||
['$value' => 42], | ||
], | ||
], | ||
'indexed constructor arguments' => [ | ||
['__construct()' => ['y', 42]], | ||
[ | ||
['__construct()' => ['x']], | ||
['__construct()' => ['y', 42]], | ||
], | ||
], | ||
'named constructor arguments' => [ | ||
['__construct()' => ['number' => 42, 'color' => 'red']], | ||
[ | ||
['__construct()' => ['number' => 42]], | ||
['__construct()' => ['color' => 'green']], | ||
['__construct()' => ['color' => 'red']], | ||
], | ||
], | ||
'indexed method arguments' => [ | ||
['run()' => ['y', 42]], | ||
[ | ||
['run()' => ['x']], | ||
['run()' => ['y', 42]], | ||
], | ||
], | ||
'named method arguments' => [ | ||
['run()' => ['number' => 42, 'color' => 'red']], | ||
[ | ||
['run()' => ['number' => 42]], | ||
['run()' => ['color' => 'green']], | ||
['run()' => ['color' => 'red']], | ||
], | ||
], | ||
'extra keys' => [ | ||
['$value' => 7, 'meta' => [1, 2]], | ||
[ | ||
['$value' => 7, 'meta' => 42], | ||
['meta' => [1, 2]], | ||
], | ||
], | ||
'complex test' => [ | ||
[ | ||
'__construct()' => ['number' => 42, 'color' => 'green'], | ||
'run()' => [15, 23], | ||
'$value' => 7, | ||
'$count' => 7, | ||
'support' => ['name' => 'background'], | ||
'do()' => [1], | ||
], | ||
[ | ||
[ | ||
'__construct()' => ['number' => 42, 'color' => 'red'], | ||
'run()' => [3], | ||
], | ||
[ | ||
'$value' => 7, | ||
'run()' => [15, 23], | ||
'$count' => 7, | ||
'support' => ['name' => 'background'], | ||
], | ||
[ | ||
'__construct()' => ['color' => 'green'], | ||
'do()' => [1], | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataBase | ||
*/ | ||
public function testBase(array $expected, array $configs): void | ||
{ | ||
$result = ArrayDefinitionHelper::merge(...$configs); | ||
$this->assertSame($expected, $result); | ||
} | ||
|
||
public function dataInvalidConfigException(): array | ||
{ | ||
return [ | ||
'non-string key' => [ | ||
'Invalid definition: invalid key in array definition. Only string keys are allowed, got 0.', | ||
[ | ||
[], | ||
['run'], | ||
], | ||
], | ||
'non-array constructor arguments 1' => [ | ||
'Invalid definition: incorrect constructor arguments. Expected array, got string.', | ||
[ | ||
['__construct()' => ['finish']], | ||
['__construct()' => 'start'], | ||
], | ||
], | ||
'non-array constructor arguments 2' => [ | ||
'Invalid definition: incorrect constructor arguments. Expected array, got string.', | ||
[ | ||
['__construct()' => 'start'], | ||
['__construct()' => ['finish']], | ||
], | ||
], | ||
'non-array method arguments 1' => [ | ||
'Invalid definition: incorrect method "run()" arguments. Expected array, got "string". Probably you should wrap them into square brackets.', | ||
[ | ||
['run()' => ['finish']], | ||
['run()' => 'start'], | ||
], | ||
], | ||
'non-array method arguments 2' => [ | ||
'Invalid definition: incorrect method "run()" arguments. Expected array, got "string". Probably you should wrap them into square brackets.', | ||
[ | ||
['run()' => 'start'], | ||
['run()' => ['finish']], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataInvalidConfigException | ||
*/ | ||
public function testInvalidConfigException(string $expectedMessage, array $configs): void | ||
{ | ||
$this->expectException(InvalidConfigException::class); | ||
$this->expectExceptionMessage($expectedMessage); | ||
ArrayDefinitionHelper::merge(...$configs); | ||
} | ||
} |