1
|
|
|
<?php namespace Comodojo\RpcClient; |
2
|
|
|
|
3
|
|
|
use \Exception; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Comodojo RPC client. It's able to talk in XML and JSON (2.0). |
7
|
|
|
* |
8
|
|
|
* It optionally supports a not standard encrypted transport |
9
|
|
|
* |
10
|
|
|
* @package Comodojo Spare Parts |
11
|
|
|
* @author Marco Giovinazzi <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
* |
14
|
|
|
* LICENSE: |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
22
|
|
|
* THE SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
class RpcRequest { |
26
|
|
|
|
27
|
|
|
private $method; |
28
|
|
|
|
29
|
|
|
private $parameters = array(); |
30
|
|
|
|
31
|
|
|
private $special_types = array(); |
32
|
|
|
|
33
|
|
|
private $id = true; |
34
|
|
|
|
35
|
|
|
private $uid; |
36
|
|
|
|
37
|
|
|
public function __construct() { |
38
|
|
|
|
39
|
|
|
$this->uid = md5(uniqid(rand(), true)); |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setMethod($method) { |
44
|
|
|
|
45
|
|
|
if ( empty($method) || !is_string($method) ) throw new Exception("Invalid RPC method"); |
46
|
|
|
|
47
|
|
|
$this->method = $method; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setParameters($params) { |
54
|
|
|
|
55
|
|
|
if ( !is_array($params) ) throw new Exception("Invalid RPC parameters"); |
56
|
|
|
|
57
|
|
|
if ( !empty($params) ) $this->parameters = $params; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setSpecialType(&$value, $type) { |
64
|
|
|
|
65
|
|
|
$type = strtolower($type); |
66
|
|
|
|
67
|
|
|
if ( empty($value) OR !in_array($type, array("base64", "datetime", "cdata")) ) { |
|
|
|
|
68
|
|
|
|
69
|
|
|
throw new Exception("Invalid value type"); |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->special_types[$value] = $type; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function setId($id = null) { |
80
|
|
|
|
81
|
|
|
if ( is_null($id) || is_int($id) || is_bool($id) ) { |
82
|
|
|
|
83
|
|
|
$this->id = $id; |
|
|
|
|
84
|
|
|
|
85
|
|
|
} else { |
86
|
|
|
|
87
|
|
|
throw new Exception("Invalid RPC id"); |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getMethod() { |
96
|
|
|
|
97
|
|
|
return $this->method; |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getParameters() { |
102
|
|
|
|
103
|
|
|
return $this->parameters; |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getSpecialTypes() { |
108
|
|
|
|
109
|
|
|
return $this->special_types; |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getId() { |
114
|
|
|
|
115
|
|
|
return $this->id; |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getUniqueId() { |
120
|
|
|
|
121
|
|
|
return $this->uid; |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function toArray() { |
126
|
|
|
|
127
|
|
|
return array( |
128
|
|
|
'uid' => $this->uid, |
129
|
|
|
'method' => $this->method, |
130
|
|
|
'parameters' => $this->parameters, |
131
|
|
|
'special_types' => $this->special_types, |
132
|
|
|
'id' => $this->id |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function create($method, $parameters = array(), $id = true) { |
138
|
|
|
|
139
|
|
|
$request = new RpcRequest(); |
140
|
|
|
|
141
|
|
|
try { |
142
|
|
|
|
143
|
|
|
$request->setMethod($method) |
144
|
|
|
->setParameters($parameters) |
145
|
|
|
->setId($id); |
146
|
|
|
|
147
|
|
|
} catch (Exception $e) { |
148
|
|
|
|
149
|
|
|
throw $e; |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $request; |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.