|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Utils\ConfigurationUtil |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Utils; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Utility class containing the configuration keys. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Tim Wagner <[email protected]> |
|
27
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
29
|
|
|
* @link https://github.com/techdivision/import |
|
30
|
|
|
* @link http://www.techdivision.com |
|
31
|
|
|
*/ |
|
32
|
|
|
class ConfigurationUtil |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* This is a utility class, so protect it against direct |
|
37
|
|
|
* instantiation. |
|
38
|
|
|
*/ |
|
39
|
|
|
private function __construct() |
|
40
|
|
|
{ |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* This is a utility class, so protect it against cloning. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
private function __clone() |
|
49
|
|
|
{ |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Prepare's the arguments for the passed reflection class by applying the values from the passed configuration array |
|
54
|
|
|
* to the apropriate arguments. If no value is found in the configuration, the constructor argument's default value is |
|
55
|
|
|
* used. |
|
56
|
|
|
* |
|
57
|
|
|
* @param \ReflectionClass $reflectionClass The reflection class to prepare the arguments for |
|
58
|
|
|
* @param array $params The constructor arguments from the configuration |
|
59
|
|
|
* |
|
60
|
|
|
* @return array The array with the constructor arguements |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function prepareConstructorArgs(\ReflectionClass $reflectionClass, array $params) |
|
63
|
|
|
{ |
|
64
|
|
|
|
|
65
|
|
|
// prepare the array for the initialized arguments |
|
66
|
|
|
$initializedParams = array(); |
|
67
|
|
|
|
|
68
|
|
|
// prepare the array for the arguments in camel case (in configuration we use a '-' notation) |
|
69
|
|
|
$paramsInCamelCase = array(); |
|
70
|
|
|
|
|
71
|
|
|
// convert the configuration keys to camelcase |
|
72
|
|
|
foreach ($params as $key => $value) { |
|
73
|
|
|
$paramsInCamelCase[lcfirst(str_replace('-', '', ucwords($key, '-')))] = $value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// prepare the arguments by applying the values from the configuration |
|
77
|
|
|
/** @var \ReflectionParameter $reflectionParameter */ |
|
78
|
|
|
foreach ($reflectionClass->getConstructor()->getParameters() as $reflectionParameter) { |
|
79
|
|
|
if (isset($paramsInCamelCase[$paramName = $reflectionParameter->getName()])) { |
|
80
|
|
|
$initializedParams[$paramName] = $paramsInCamelCase[$paramName]; |
|
81
|
|
|
} elseif ($reflectionParameter->isOptional()) { |
|
82
|
|
|
$initializedParams[$paramName] = $reflectionParameter->getDefaultValue(); |
|
83
|
|
|
} else { |
|
84
|
|
|
$initializedParams[$paramName] = null; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// return the array with the prepared arguments |
|
89
|
|
|
return $initializedParams; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|