Skip to content

Commit 9faab8e

Browse files
committed
options_merge function
1 parent 07ca782 commit 9faab8e

File tree

5 files changed

+233
-16
lines changed

5 files changed

+233
-16
lines changed

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"name": "api-clients/foundation",
33
"license": "MIT",
4+
"minimum-stability": "dev",
5+
"prefer-stable": true,
46
"authors": [
57
{
68
"name": "Cees-Jan Kiewiet",
@@ -27,7 +29,10 @@
2729
"autoload": {
2830
"psr-4": {
2931
"ApiClients\\Foundation\\": "src/"
30-
}
32+
},
33+
"files": [
34+
"src/functions_include.php"
35+
]
3136
},
3237
"autoload-dev": {
3338
"psr-4": {

composer.lock

Lines changed: 61 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/functions.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Foundation;
4+
5+
/**
6+
* @param array $base
7+
* @param array $options
8+
* @return array
9+
*/
10+
function options_merge(array $base, array $options): array
11+
{
12+
$merge = true;
13+
foreach ($base as $key => $value) {
14+
if (is_numeric($key)) {
15+
$merge = false;
16+
}
17+
}
18+
foreach ($options as $name => $option) {
19+
if (is_numeric($name)) {
20+
$merge = false;
21+
}
22+
}
23+
24+
if ($merge === false) {
25+
$new = [];
26+
27+
foreach ($base as $key => $value) {
28+
if (in_array($value, $new)) {
29+
continue;
30+
}
31+
$new[] = $value;
32+
}
33+
foreach ($options as $name => $option) {
34+
if (in_array($option, $new)) {
35+
continue;
36+
}
37+
$new[] = $option;
38+
}
39+
40+
return $new;
41+
}
42+
43+
foreach ($base as $key => $value) {
44+
if (!isset($options[$key])) {
45+
continue;
46+
}
47+
48+
$option = $options[$key];
49+
unset($options[$key]);
50+
51+
if (is_array($value) && is_array($option)) {
52+
$base[$key] = options_merge($value, $option);
53+
continue;
54+
}
55+
56+
$base[$key] = $option;
57+
}
58+
59+
foreach ($options as $name => $option) {
60+
$base[$name] = $option;
61+
}
62+
63+
return $base;
64+
}

src/functions_include.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Foundation;
4+
5+
// @codeCoverageIgnoreStart
6+
if (!function_exists('ApiClients\Foundation\options_merge')) {
7+
require __DIR__ . '/functions.php';
8+
}
9+
// @codeCoverageIgnoreEnd

tests/FunctionsTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tests\Foundation;
4+
5+
use ApiClients\Tools\TestUtilities\TestCase;
6+
use function ApiClients\Foundation\options_merge;
7+
8+
final class FunctionsTest extends TestCase
9+
{
10+
public function optionsProvider()
11+
{
12+
yield [
13+
[],
14+
[],
15+
[],
16+
];
17+
18+
yield [
19+
[],
20+
['foo' => 'bar'],
21+
['foo' => 'bar'],
22+
];
23+
24+
yield [
25+
[],
26+
[
27+
'foo' => 'bar',
28+
'middleware' => [],
29+
],
30+
[
31+
'foo' => 'bar',
32+
'middleware' => [],
33+
],
34+
];
35+
36+
yield [
37+
[
38+
'middleware' => [
39+
'foo',
40+
],
41+
],
42+
[
43+
'foo' => 'bar',
44+
'middleware' => [
45+
'bar',
46+
],
47+
],
48+
[
49+
'middleware' => [
50+
'foo',
51+
'bar',
52+
],
53+
'foo' => 'bar',
54+
],
55+
];
56+
57+
yield [
58+
[
59+
'middleware' => [
60+
'foo',
61+
'bar',
62+
],
63+
],
64+
[
65+
'foo' => 'bar',
66+
'middleware' => [
67+
'bar',
68+
],
69+
],
70+
[
71+
'middleware' => [
72+
'foo',
73+
'bar',
74+
],
75+
'foo' => 'bar',
76+
],
77+
];
78+
}
79+
80+
/**
81+
* @dataProvider optionsProvider
82+
*/
83+
public function testOptionsMerge(array $base, array $options, array $expected)
84+
{
85+
self::assertSame(
86+
$expected,
87+
options_merge(
88+
$base,
89+
$options
90+
)
91+
);
92+
}
93+
}

0 commit comments

Comments
 (0)