1
|
|
|
<?php |
2
|
|
|
namespace EZAMA{ |
3
|
|
|
|
4
|
|
|
class namedArgs extends namedArgsHelper |
5
|
|
|
{ |
6
|
|
|
protected $parameters=array(); |
7
|
|
|
|
8
|
|
|
public function __construct($mandatory) |
9
|
|
|
{ |
10
|
|
|
if (!\is_array($mandatory)) { |
11
|
|
|
throw new \InvalidArgumentException(\sprintf('parameter type must be array, %s given', \gettype($mandatory))); |
12
|
|
|
} |
13
|
|
|
$this->parameters=$mandatory; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
protected static function &ProcessParams(&$argument, $required, $default) |
17
|
|
|
{ |
18
|
|
|
$missing=array(); |
19
|
|
|
if (!\is_array($argument)) { |
20
|
|
|
return \false; |
21
|
|
|
} |
22
|
|
|
$argument=array_intersect_key($argument, $default);//keep only predefined names |
23
|
|
|
//check for missing required parameters |
24
|
|
|
foreach ($required as $k=>$v) { |
25
|
|
|
if (!array_key_exists($v, $argument)) { |
26
|
|
|
$missing[]=$v; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (!empty($missing)) { |
31
|
|
|
$function=\debug_backtrace(); |
32
|
|
|
$function=\end($function); |
33
|
|
|
$function=$function['function']; |
34
|
|
|
$cm=\count($missing); |
35
|
|
|
$error=\call_user_func_array('sprintf', array('Function %s\'s Required '.($cm>1?'parameters %s are':'parameter %s is').' missing',$function,NamedArgs::format($missing))); |
36
|
|
|
self::throwError($error); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
self::merge($default, $argument);//assign given values to parameters while keeping references |
41
|
|
|
return $default; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function &getParams($required, $default) |
45
|
|
|
{ |
46
|
|
|
if (self::is_valid_associative($this->parameters)) { |
47
|
|
|
return self::ProcessParams($this->parameters, $required, $default); |
48
|
|
|
} else { |
49
|
|
|
$cp=\count($this->parameters); |
50
|
|
|
if ($cp>=\count($required)) { |
51
|
|
|
foreach (array_keys($default) as $k=>$v) { |
52
|
|
|
if ($k===$cp) { |
53
|
|
|
break; |
54
|
|
|
} |
55
|
|
|
$default[$v]=&$this->parameters[$k]; |
56
|
|
|
} |
57
|
|
|
return self::ProcessParams($default, $required, $default); |
58
|
|
|
} else { |
59
|
|
|
$function=\debug_backtrace(); |
60
|
|
|
$function=\end($function); |
61
|
|
|
$function=$function['function']; |
62
|
|
|
|
63
|
|
|
self::throwError(\sprintf('Function %s : Two few parameters supplied', $function)); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
public static function __callStatic($name, $mandatory) |
71
|
|
|
{ |
72
|
|
|
if (empty($mandatory)) { |
73
|
|
|
$mandatory[0]=[]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($mandatory[0] instanceof NamedArgs) { |
77
|
|
|
return self::func($name, $mandatory[0]); |
78
|
|
|
} elseif (\is_array($mandatory[0])) { |
79
|
|
|
return self::func($name, new self($mandatory[0])); |
80
|
|
|
} |
81
|
|
|
return \false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
protected static function func($func, NamedArgs $mandatory) |
86
|
|
|
{ |
87
|
|
|
$args=&$mandatory->parameters; |
88
|
|
|
return self::processParamsAndArgs($func, $args); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
protected static function getValues(&$func, &$params, $paramsArgs, &$args, $associative) |
94
|
|
|
{ |
95
|
|
|
foreach ((array)$params as $k=> $param) { |
96
|
|
|
$key=$associative?$param->name:$k; |
97
|
|
|
if (array_key_exists($key, $args)) { |
98
|
|
|
$paramsArgs[]=&$args[$key]; |
99
|
|
|
} else { |
100
|
|
|
self::elseifGetValues($func, $param, $paramsArgs); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
return $paramsArgs; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected static function handleOptional($notOptional, $func, $param) |
107
|
|
|
{ |
108
|
|
|
if ($notOptional) { |
109
|
|
|
self::throwError(\sprintf('Function %s\'s required parameter %s is missing', $func, $param)); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected static function getParamDefaultValue(\reflectionParameter $param) |
114
|
|
|
{ |
115
|
|
|
return $param->getDefaultValueConstantName()?\constant($param->getDefaultValueConstantName()):$param->getDefaultValue(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected static function canGetParamDefaultValue(\reflectionFunction $func, \reflectionParameter $param) |
119
|
|
|
{ |
120
|
|
|
return !$func->isInternal()&&($param->isDefaultValueAvailable()||$param->isDefaultValueConstant()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
protected static function elseifGetValues(\reflectionFunction $func, \reflectionParameter $param, &$paramsArgs) |
125
|
|
|
{ |
126
|
|
|
if (self::canGetParamDefaultValue($func, $param)) { |
127
|
|
|
$paramsArgs[] = self::getParamDefaultValue($param); |
128
|
|
|
} elseif ($param->allowsNull()) { |
129
|
|
|
$paramsArgs[]=null; |
130
|
|
|
} else { |
131
|
|
|
self::handleOptional(!$param->isOptional(), (string)$func->name, (string)$param->name); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected static function processParamsAndArgs($func, $args) |
136
|
|
|
{ |
137
|
|
|
self::getReflection($func); |
138
|
|
|
$paramsArgs = array(); |
139
|
|
|
$params =$func->getParameters(); |
140
|
|
|
return $func->invokeArgs(self::getValues($func, $params, $paramsArgs, $args, self::is_valid_associative($args))); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|