1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Marwan Al-Soltany <[email protected]> |
5
|
|
|
* @copyright Marwan Al-Soltany 2020 |
6
|
|
|
* For the full copyright and license information, please view |
7
|
|
|
* the LICENSE file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace MAKS\AmqpAgent\Config; |
13
|
|
|
|
14
|
|
|
use MAKS\AmqpAgent\Exception\ConstantDoesNotExistException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* An abstract class that exposes a simple API to work with parameters. |
18
|
|
|
* @since 1.2.0 |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractParameters |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Patches the passed array with a class constant. |
24
|
|
|
* @param array $options The partial array. |
25
|
|
|
* @param string $const The constant name. |
26
|
|
|
* @param bool $values Whether to return values only or an associative array. |
27
|
|
|
* @return array The final patched array. |
28
|
|
|
* @throws ConstantDoesNotExistException |
29
|
|
|
*/ |
30
|
71 |
|
final public static function patch(array $options, string $const, bool $values = false): array |
31
|
|
|
{ |
32
|
71 |
|
$final = null; |
33
|
71 |
|
$const = static::class . '::' . $const; |
34
|
|
|
|
35
|
71 |
|
if (defined($const)) { |
36
|
70 |
|
$const = constant($const); |
37
|
|
|
|
38
|
70 |
|
$final = is_array($const) ? self::patchWith($options, $const, $values) : $final; |
39
|
|
|
} |
40
|
|
|
|
41
|
71 |
|
if (null !== $final) { |
42
|
70 |
|
return $final; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
throw new ConstantDoesNotExistException( |
46
|
1 |
|
sprintf( |
47
|
1 |
|
'Could not find a constant with the name "%s", or the constant is not of type array!', |
48
|
1 |
|
$const |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Patches the passed array with another array. |
55
|
|
|
* @param array $partialArray The partial array. |
56
|
|
|
* @param array $fullArray The full array. |
57
|
|
|
* @param bool $values Whether to return values only or an associative array. |
58
|
|
|
* @return array The final patched array. |
59
|
|
|
*/ |
60
|
70 |
|
final public static function patchWith(array $partialArray, array $fullArray, bool $values = false): array |
61
|
|
|
{ |
62
|
|
|
$final = ( |
63
|
70 |
|
array_merge( |
64
|
70 |
|
$fullArray, |
65
|
70 |
|
array_intersect_key( |
66
|
70 |
|
$partialArray, |
67
|
70 |
|
$fullArray |
68
|
|
|
) |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
|
72
|
70 |
|
return !$values ? $final : array_values($final); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|