1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netdudes\DataSourceryBundle\Extension; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Wraps a definition of a callable defined in an extension, |
7
|
|
|
* available to be used in UQL, etc. |
8
|
|
|
*/ |
9
|
|
|
class UqlFunction implements \JsonSerializable, UqlFunctionInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Instance to which this function/method belongs |
13
|
|
|
* |
14
|
|
|
* @var UqlExtensionInterface |
15
|
|
|
*/ |
16
|
|
|
private $instance; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Name of the method in the instance where its defined |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $method; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Name by which this function will be known inside UQL |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $name; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $name |
34
|
|
|
* @param UqlExtensionInterface $instance |
35
|
|
|
* @param string $method |
36
|
|
|
*/ |
37
|
|
|
public function __construct($name, UqlExtensionInterface $instance, $method) |
38
|
|
|
{ |
39
|
|
|
$this->instance = $instance; |
40
|
|
|
$this->name = $name; |
41
|
|
|
$this->method = $method; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return UqlExtensionInterface |
46
|
|
|
*/ |
47
|
|
|
public function getInstance() |
48
|
|
|
{ |
49
|
|
|
return $this->instance; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function getMethod() |
56
|
|
|
{ |
57
|
|
|
return $this->method; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function getName() |
64
|
|
|
{ |
65
|
|
|
return $this->name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Performs the call to the function defined by this FunctionExtension |
70
|
|
|
* |
71
|
|
|
* @param array $arguments |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function call($arguments) |
76
|
|
|
{ |
77
|
|
|
return call_user_func_array([$this->getInstance(), $this->getMethod()], $arguments); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns a readable string representation of the function, of the standard form: |
82
|
|
|
* |
83
|
|
|
* functionName(argument1, argument2, [optionalArgument1 = defaultValue, [optionalNullableArgument2]]) |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function __toString() |
88
|
|
|
{ |
89
|
|
|
$reflection = new \ReflectionMethod($this->instance, $this->method); |
90
|
|
|
$methodParameters = $reflection |
91
|
|
|
->getParameters(); |
92
|
|
|
|
93
|
|
|
$optionalArgumentCount = 0; |
94
|
|
|
$splitStringRepresentation = []; |
95
|
|
|
foreach ($methodParameters as $parameter) { |
96
|
|
|
$optional = $parameter->isOptional(); |
97
|
|
|
$name = $parameter->getName(); |
|
|
|
|
98
|
|
|
if ($optional) { |
99
|
|
|
$name = '[' . $name; |
100
|
|
|
$defaultValue = $parameter->getDefaultValue(); |
101
|
|
|
if (!is_null($defaultValue)) { |
102
|
|
|
if (is_numeric($defaultValue)) { |
103
|
|
|
$defaultValue = intval($defaultValue); |
104
|
|
|
} |
105
|
|
|
$name .= ' = ' . $defaultValue; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$optionalArgumentCount++; |
109
|
|
|
} |
110
|
|
|
$splitStringRepresentation[] = $name; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->name . '(' . implode(', ', $splitStringRepresentation) . str_repeat(']', $optionalArgumentCount) . ')'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* (PHP 5 >= 5.4.0)<br/> |
118
|
|
|
* Specify data which should be serialized to JSON |
119
|
|
|
* |
120
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
121
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
122
|
|
|
* which is a value of any type other than a resource. |
123
|
|
|
*/ |
124
|
|
|
public function jsonSerialize() |
125
|
|
|
{ |
126
|
|
|
$json = [ |
127
|
|
|
'name' => $this->getName(), |
|
|
|
|
128
|
|
|
'arguments' => [], |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
$reflection = new \ReflectionMethod($this->instance, $this->method); |
132
|
|
|
$methodParameters = $reflection |
133
|
|
|
->getParameters(); |
134
|
|
|
|
135
|
|
|
foreach ($methodParameters as $parameter) { |
136
|
|
|
$isOptional = $parameter->isOptional(); |
137
|
|
|
$argument = [ |
138
|
|
|
'name' => $parameter->getName(), |
|
|
|
|
139
|
|
|
'optional' => $isOptional, |
140
|
|
|
'default' => null, |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
if ($isOptional) { |
144
|
|
|
$defaultValue = $parameter->getDefaultValue(); |
145
|
|
|
if (!is_null($defaultValue)) { |
146
|
|
|
if (is_numeric($defaultValue)) { |
147
|
|
|
$defaultValue = intval($defaultValue); |
148
|
|
|
} |
149
|
|
|
$argument['default'] = $defaultValue; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$json['arguments'][] = $argument; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $json; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|