-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigResolver.php
More file actions
150 lines (127 loc) · 3.8 KB
/
ConfigResolver.php
File metadata and controls
150 lines (127 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare (strict_types=1);
namespace Coshi\Variator;
use Coshi\Variator\Exception\InvalidConfigurationException;
use Coshi\Variator\Variation\VariationInterface;
class ConfigResolver
{
/**
* @var callable[]
*/
protected $contextCallbacks;
/**
* @param array $arguments
* @param string $name
*
* @return bool
*/
public function containsArgumentsPattern(array $arguments, string $name) : bool
{
foreach ($arguments as $key => $argument) {
if (is_string($argument) && '@'.$name === $argument) {
return true;
}
}
return false;
}
/**
* @param array $requiredArgs
* @param array $parameters
* @param string $type
*
* @return bool
*
* @throws InvalidConfigurationException
*/
public function requireArguments(array $requiredArgs, array $parameters, string $type) : bool
{
foreach ($requiredArgs as $parameter) {
if (!array_key_exists($parameter, $parameters)) {
throw new InvalidConfigurationException(sprintf('Parameter "%s" for type "%s" is required', $parameter, $type));
}
}
return true;
}
/**
* @param array $config
*
* @return array
*
* @throws \InvalidArgumentException
*/
public function resolveCallback(array $config)
{
if (count($config) < 1) {
throw new \InvalidArgumentException('Cannot resolve empty callback');
}
if (!isset($config[1]) || is_array($config[1])) {
$callable = $config[0];
$arguments = $config[1] ?? [];
} else {
$callable = [$this->resolveInstance($config), $config[1]];
$arguments = $config[2] ?? [];
}
return [
'callback' => $callable,
'arguments' => $arguments,
];
}
/**
* @param array $callback - callback config returned from ConfigResolver::resolveCallback() method
*
* @return mixed
*/
public function call(array $callback)
{
return call_user_func_array($callback['callback'], $this->processArguments($callback['arguments']));
}
/**
* @param array $arguments
*
* @return array
*/
public function processArguments(array $arguments) : array
{
foreach ($arguments as $key => $value) {
if (is_string($value) && 0 === strpos($value, '@')) {
$name = substr($value, 1);
if (isset($this->contextCallbacks[$name])) {
$arguments[$key] = call_user_func($this->contextCallbacks[$name]);
}
}
}
return $arguments;
}
/**
* @param VariationInterface $variation
*
* @return bool
*/
public function addCallback(VariationInterface $variation)
{
$closure = (function () {
if (!$this->isValid()) {
$this->doRewind();
}
return $this->getCurrent();
})->bindTo($variation, $variation);
$this->contextCallbacks[$variation->getName()] = $closure;
return true;
}
protected function resolveInstance(array $config)
{
$instance = $config[0];
if (!is_string($instance)) {
throw new InvalidConfigurationException(sprintf('Expected type "string" for callback class, found "%s"', gettype($instance)));
}
if (!class_exists($instance)) {
throw new \InvalidArgumentException(sprintf('Class %s does not exist', $instance));
}
$reflection = new \ReflectionClass($instance);
$method = $reflection->getMethod($config[1]);
if (!$method->isStatic()) {
$instance = new $instance();
}
return $instance;
}
}