Completed
Push — master ( 8e8a04...31fb68 )
by Marco
38:43 queued 23:45
created

RpcRequest::setId()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 2
nop 1
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
    /**
34
     * Request's id
35
     *
36
     * @var boolean|integer
37
     */
38
    private $id = true;
39
40
    private $uid;
41
42
    public function __construct() {
43
44
        $this->uid = md5(uniqid(rand(), true));
45
46
    }
47
48
    public function setMethod($method) {
49
50
        if ( empty($method) || !is_string($method) ) throw new Exception("Invalid RPC method");
51
52
        $this->method = $method;
53
54
        return $this;
55
56
    }
57
58
    public function setParameters($params) {
59
60
        if ( !is_array($params) ) throw new Exception("Invalid RPC parameters");
61
62
        if ( !empty($params) ) $this->parameters = $params;
63
64
        return $this;
65
66
    }
67
68
    public function setSpecialType(&$value, $type) {
69
70
        $type = strtolower($type);
71
72
        if ( empty($value) || !in_array($type, array("base64", "datetime", "cdata")) ) {
73
74
            throw new Exception("Invalid value type");
75
76
        }
77
78
        $this->special_types[$value] = $type;
79
80
        return $this;
81
82
    }
83
84
    public function setId($id = null) {
85
86
        if ( is_null($id) || is_int($id) || is_bool($id) ) {
87
88
            $this->id = $id;
89
90
        } else {
91
92
            throw new Exception("Invalid RPC id");
93
94
        }
95
96
        return $this;
97
98
    }
99
100
    public function getMethod() {
101
102
        return $this->method;
103
104
    }
105
106
    public function getParameters() {
107
108
        return $this->parameters;
109
110
    }
111
112
    public function getSpecialTypes() {
113
114
        return $this->special_types;
115
116
    }
117
118
    public function getId() {
119
120
        return $this->id;
121
122
    }
123
124
    public function getUniqueId() {
125
126
        return $this->uid;
127
128
    }
129
130
    public function toArray() {
131
132
        return array(
133
            'uid' => $this->uid,
134
            'method' => $this->method,
135
            'parameters' => $this->parameters,
136
            'special_types' => $this->special_types,
137
            'id' => $this->id
138
        );
139
140
    }
141
142
    public static function create($method, $parameters = array(), $id = true) {
143
144
        $request = new RpcRequest();
145
146
        try {
147
148
            $request->setMethod($method)
149
                ->setParameters($parameters)
150
                ->setId($id);
151
152
        } catch (Exception $e) {
153
154
            throw $e;
155
156
        }
157
158
        return $request;
159
160
    }
161
162
}
163