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