1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace POData\Providers\Metadata; |
4
|
|
|
|
5
|
|
|
use AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\FunctionImportAnonymousType; |
6
|
|
|
|
7
|
|
|
class ResourceFunctionType |
8
|
|
|
{ |
9
|
|
|
private $blacklist = ['exec', 'system', 'eval']; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @property string |
13
|
|
|
*/ |
14
|
|
|
private $functionName = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @property \AlgoWeb\ODataMetadata\MetadataV3\edm\EntityContainer\FunctionImportAnonymousType $baseType |
18
|
|
|
*/ |
19
|
|
|
private $baseType = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ResourceFunctionType constructor. |
23
|
|
|
* @param string|array $functionName |
24
|
|
|
* @param FunctionImportAnonymousType $type |
25
|
|
|
*/ |
26
|
|
|
public function __construct($functionName, FunctionImportAnonymousType $type) |
27
|
|
|
{ |
28
|
|
|
if (null === $functionName) { |
29
|
|
|
$msg = "FunctionName must not be null"; |
30
|
|
|
throw new \InvalidArgumentException($msg); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!is_string($functionName) && !is_array($functionName)) { |
34
|
|
|
$msg = "Function name must be string or array"; |
35
|
|
|
throw new \InvalidArgumentException($msg); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$isArray = is_array($functionName); |
39
|
|
|
if ($isArray && 1 == count($functionName)) { |
40
|
|
|
$functionName = $functionName[0]; |
41
|
|
|
$isArray = false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($isArray) { |
45
|
|
|
if (2 < count($functionName)) { |
46
|
|
|
$msg = "FunctionName must have no more than 2 elements"; |
47
|
|
|
throw new \InvalidArgumentException($msg); |
48
|
|
|
} |
49
|
|
|
if (0 == count($functionName)) { |
50
|
|
|
$msg = "FunctionName must have 1 or 2 elements"; |
51
|
|
|
throw new \InvalidArgumentException($msg); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!is_object($functionName[0]) && !is_string($functionName[0])) { |
55
|
|
|
$msg = "First element of FunctionName must be either object or string"; |
56
|
|
|
throw new \InvalidArgumentException($msg); |
57
|
|
|
} |
58
|
|
|
if (!is_string($functionName[1])) { |
59
|
|
|
$msg = "Second element of FunctionName must be string"; |
60
|
|
|
throw new \InvalidArgumentException($msg); |
61
|
|
|
} |
62
|
|
|
if (is_string($functionName[0])) { |
63
|
|
|
$functionName[0] = trim($functionName[0]); |
64
|
|
|
$func = $functionName[0]; |
65
|
|
|
if ('' == $func) { |
66
|
|
|
$msg = "First element of FunctionName must not be empty"; |
67
|
|
|
throw new \InvalidArgumentException($msg); |
68
|
|
|
} |
69
|
|
|
$this->checkBlacklist($func, true); |
70
|
|
|
} |
71
|
|
|
} else { |
72
|
|
|
if (!is_string($functionName) || empty(trim($functionName))) { |
73
|
|
|
$msg = "FunctionName must be a non-empty string"; |
74
|
|
|
throw new \InvalidArgumentException($msg); |
75
|
|
|
} |
76
|
|
|
$functionName = trim($functionName); |
77
|
|
|
|
78
|
|
|
$this->checkBlacklist($functionName, false); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!$type->isOK($msg)) { |
82
|
|
|
throw new \InvalidArgumentException($msg); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->functionName = $functionName; |
86
|
|
|
$this->baseType = $type; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get endpoint name |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getName() |
95
|
|
|
{ |
96
|
|
|
return $this->baseType->getName(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get underlying function name |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getFunctionName() |
105
|
|
|
{ |
106
|
|
|
return $this->functionName; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Required parameter list |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function getParms() |
115
|
|
|
{ |
116
|
|
|
return $this->baseType->getParameter(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function get(array $parms = []) |
120
|
|
|
{ |
121
|
|
|
// check inputs |
122
|
|
|
$baseParms = $this->getParms(); |
123
|
|
|
$expectedParms = count($baseParms); |
124
|
|
|
$actualParms = count($parms); |
125
|
|
|
if ($expectedParms != $actualParms) { |
126
|
|
|
$msg = "Was expecting ". $expectedParms. " arguments, received ".$actualParms." instead"; |
127
|
|
|
throw new \InvalidArgumentException($msg); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// commence primary ignition |
131
|
|
|
return call_user_func_array($this->functionName, $parms); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $func |
136
|
|
|
*/ |
137
|
|
|
private function checkBlacklist($func, $fromArray = false) |
138
|
|
|
{ |
139
|
|
|
if (in_array($func, $this->blacklist) || in_array(strtolower($func), $this->blacklist)) { |
140
|
|
|
$msg = (true === $fromArray ? "First element of " : "")."FunctionName blacklisted"; |
141
|
|
|
throw new \InvalidArgumentException($msg); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|