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