1: <?php
2:
3: 4: 5: 6: 7:
8:
9:
10:
11: 12: 13: 14: 15: 16:
17: class NDIHelpers
18: {
19:
20: 21: 22: 23: 24: 25: 26: 27:
28: public static function expand($var, array $params, $recursive = FALSE)
29: {
30: if (is_array($var)) {
31: $res = array();
32: foreach ($var as $key => $val) {
33: $res[$key] = self::expand($val, $params, $recursive);
34: }
35: return $res;
36:
37: } elseif ($var instanceof NDIStatement) {
38: return new NDIStatement(self::expand($var->entity, $params, $recursive), self::expand($var->arguments, $params, $recursive));
39:
40: } elseif (!is_string($var)) {
41: return $var;
42: }
43:
44: $parts = preg_split('#%([\w.-]*)%#i', $var, -1, PREG_SPLIT_DELIM_CAPTURE);
45: $res = '';
46: foreach ($parts as $n => $part) {
47: if ($n % 2 === 0) {
48: $res .= $part;
49:
50: } elseif ($part === '') {
51: $res .= '%';
52:
53: } elseif (isset($recursive[$part])) {
54: throw new InvalidArgumentException('Circular reference detected for variables: ' . implode(', ', array_keys($recursive)) . '.');
55:
56: } else {
57: $val = NArrays::get($params, explode('.', $part));
58: if ($recursive) {
59: $val = self::expand($val, $params, (is_array($recursive) ? $recursive : array()) + array($part => 1));
60: }
61: if (strlen($part) + 2 === strlen($var)) {
62: return $val;
63: }
64: if (!is_scalar($val)) {
65: throw new InvalidArgumentException("Unable to concatenate non-scalar parameter '$part' into '$var'.");
66: }
67: $res .= $val;
68: }
69: }
70: return $res;
71: }
72:
73:
74: 75: 76: 77: 78:
79: public static function escape($value)
80: {
81: if (is_array($value)) {
82: array_walk_recursive($value, create_function('&$val', '
83: $val = is_string($val) ? str_replace(\'%\', \'%%\', $val) : $val;
84: '));
85: } elseif (is_string($value)) {
86: $value = str_replace('%', '%%', $value);
87: }
88: return $value;
89: }
90:
91:
92: 93: 94: 95: 96:
97: public static function autowireArguments(ReflectionFunctionAbstract $method, array $arguments, $container)
98: {
99: $optCount = 0;
100: $num = -1;
101: $res = array();
102:
103: foreach ($method->getParameters() as $num => $parameter) {
104: if (array_key_exists($num, $arguments)) {
105: $res[$num] = $arguments[$num];
106: unset($arguments[$num]);
107: $optCount = 0;
108:
109: } elseif (array_key_exists($parameter->getName(), $arguments)) {
110: $res[$num] = $arguments[$parameter->getName()];
111: unset($arguments[$parameter->getName()]);
112: $optCount = 0;
113:
114: } elseif ($class = $parameter->getClassName()) {
115: $res[$num] = $container->getByType($class, FALSE);
116: if ($res[$num] === NULL) {
117: if ($parameter->allowsNull()) {
118: $optCount++;
119: } else {
120: throw new NServiceCreationException("No service of type {$class} found. Make sure the type hint in $method is written correctly and service of this type is registered.");
121: }
122: } else {
123: if ($container instanceof NDIContainerBuilder) {
124: $res[$num] = '@' . $res[$num];
125: }
126: $optCount = 0;
127: }
128:
129: } elseif ($parameter->isOptional()) {
130:
131: $res[$num] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
132: $optCount++;
133:
134: } else {
135: throw new NServiceCreationException("$parameter has no type hint, so its value must be specified.");
136: }
137: }
138:
139:
140: while (array_key_exists(++$num, $arguments)) {
141: $res[$num] = $arguments[$num];
142: unset($arguments[$num]);
143: $optCount = 0;
144: }
145: if ($arguments) {
146: throw new NServiceCreationException("Unable to pass specified arguments to $method.");
147: }
148:
149: return $optCount ? array_slice($res, 0, -$optCount) : $res;
150: }
151:
152: }
153: