1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2014-05-26 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\Locator\Configuration\Assembler; |
8
|
|
|
|
9
|
|
|
use Net\Bazzline\Component\Locator\Configuration\Configuration; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AbstractAssembler |
13
|
|
|
* @package Net\Bazzline\Component\Locator\Configuration\Assembler |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractAssembler implements AssemblerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param mixed $data |
19
|
|
|
* @param Configuration $configuration |
20
|
|
|
* @throws InvalidArgumentException |
21
|
|
|
* @throws RuntimeException |
22
|
|
|
* @return Configuration |
23
|
|
|
*/ |
24
|
|
|
final public function assemble($data, Configuration $configuration) |
25
|
|
|
{ |
26
|
|
|
$this->validateData($data); |
27
|
|
|
$configuration = $this->map($data, $configuration); |
28
|
|
|
|
29
|
|
|
return $configuration; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param mixed $data |
34
|
|
|
* @param Configuration $configuration |
35
|
|
|
* @return Configuration |
36
|
|
|
* @throws RuntimeException |
37
|
|
|
*/ |
38
|
|
|
abstract protected function map($data, Configuration $configuration); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed $data |
42
|
|
|
* @throws InvalidArgumentException |
43
|
|
|
*/ |
44
|
|
|
abstract protected function validateData($data); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $data |
48
|
|
|
* @param array $keysToExpectedValueType |
49
|
|
|
* @throws InvalidArgumentException |
50
|
|
|
*/ |
51
|
|
|
final protected function validateDataWithMandatoryKeysAndExpectedValueType(array $data, array $keysToExpectedValueType) |
52
|
|
|
{ |
53
|
|
|
foreach ($keysToExpectedValueType as $key => $expectedType) { |
54
|
|
|
if (!isset($data[$key])) { |
55
|
|
|
throw new InvalidArgumentException( |
56
|
|
|
'data array must contain content for key "' . $key . '"' |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->validateExpectedDataKeyType($data, $key, $expectedType); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $data |
66
|
|
|
* @param array $keysToExpectedValueType |
67
|
|
|
* @throws InvalidArgumentException |
68
|
|
|
*/ |
69
|
|
|
final protected function validateDataWithOptionalKeysAndExpectedValueTypeOrSetExpectedValueAsDefault(array $data, array $keysToExpectedValueType) |
70
|
|
|
{ |
71
|
|
|
foreach ($keysToExpectedValueType as $key => $expectedType) { |
72
|
|
|
if (isset($data[$key])) { |
73
|
|
|
$this->validateExpectedDataKeyType($data, $key, $expectedType); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $data |
80
|
|
|
* @param Configuration $configuration |
81
|
|
|
* @return Configuration |
82
|
|
|
*/ |
83
|
|
|
protected function mapBooleanProperties(array $data, Configuration $configuration) |
84
|
|
|
{ |
85
|
|
|
if (isset($data['create_interface'])) { |
86
|
|
|
$configuration->setCreateLocatorGeneratorInterface($data['create_interface']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $configuration; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $data |
94
|
|
|
* @param Configuration $configuration |
95
|
|
|
* @return Configuration |
96
|
|
|
*/ |
97
|
|
|
protected function mapStringProperties(array $data, Configuration $configuration) |
98
|
|
|
{ |
99
|
|
|
$configuration->setClassName($data['class_name'])->setFilePath($data['file_path']); |
100
|
|
|
|
101
|
|
|
if (isset($data['method_prefix'])) { |
102
|
|
|
$configuration->setMethodPrefix($data['method_prefix']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (isset($data['namespace'])) { |
106
|
|
|
$configuration->setNamespace($data['namespace']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (isset($data['extends'])) { |
110
|
|
|
$configuration->setExtends($data['extends']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $configuration; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $data |
118
|
|
|
* @param string $key |
119
|
|
|
* @param string $expectedType |
120
|
|
|
*/ |
121
|
|
|
private function validateExpectedDataKeyType(array $data, $key, $expectedType) |
122
|
|
|
{ |
123
|
|
|
$exceptionMessage = 'value of key "' . $key . '" must be of type "' . $expectedType . '" when set'; |
124
|
|
|
|
125
|
|
|
switch ($expectedType) { |
126
|
|
|
case 'array': |
127
|
|
|
if (!is_array($data[$key])) { |
128
|
|
|
throw new InvalidArgumentException($exceptionMessage); |
129
|
|
|
} |
130
|
|
|
break; |
131
|
|
|
case 'string': |
132
|
|
|
if (!is_string($data[$key])) { |
133
|
|
|
throw new InvalidArgumentException($exceptionMessage); |
134
|
|
|
} |
135
|
|
|
break; |
136
|
|
|
case 'bolean': |
137
|
|
|
if (!is_bool($data[$key])) { |
138
|
|
|
throw new InvalidArgumentException($exceptionMessage); |
139
|
|
|
} |
140
|
|
|
break; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |