1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service\Action; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Result |
18
|
|
|
* |
19
|
|
|
* @package CakeDC\Api\Service\Action |
20
|
|
|
*/ |
21
|
|
|
class Result |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Response code |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $_code = 200; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Response data |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $_data = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Response payload |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $_payload = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Exception structure |
47
|
|
|
* |
48
|
|
|
* @var Exception |
49
|
|
|
*/ |
50
|
|
|
protected $_exception = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Result constructor. |
54
|
|
|
* |
55
|
|
|
* @param array $data data to be delivered for the api |
56
|
|
|
* @param int $code code of the api request |
57
|
|
|
*/ |
58
|
60 |
|
public function __construct($data = null, $code = null) |
59
|
|
|
{ |
60
|
60 |
|
if ($data !== null) { |
61
|
|
|
$this->data($data); |
62
|
|
|
} |
63
|
60 |
|
if ($code !== null) { |
64
|
|
|
$this->code($code); |
65
|
|
|
} |
66
|
60 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Data api method. |
70
|
|
|
* |
71
|
|
|
* @param array $value data to be delivered for the api |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
56 |
|
public function data($value = null) |
75
|
|
|
{ |
76
|
56 |
|
if ($value === null) { |
77
|
56 |
|
return $this->_data; |
78
|
|
|
} |
79
|
56 |
|
$this->_data = $value; |
80
|
|
|
|
81
|
56 |
|
return $this->_data; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Code api method. |
86
|
|
|
* |
87
|
|
|
* @param int $value code of the api request |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
60 |
|
public function code($value = null) |
91
|
|
|
{ |
92
|
60 |
|
if ($value === null) { |
93
|
60 |
|
return $this->_code; |
94
|
|
|
} |
95
|
58 |
|
$this->_code = $value; |
96
|
|
|
|
97
|
58 |
|
return $this->_code; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Exception api. |
102
|
|
|
* |
103
|
|
|
* @param Exception $value set the excepltion value |
104
|
|
|
* @return Exception |
105
|
|
|
*/ |
106
|
56 |
|
public function exception($value = null) |
107
|
|
|
{ |
108
|
56 |
|
if ($value === null) { |
109
|
56 |
|
return $this->_exception; |
110
|
|
|
} |
111
|
8 |
|
$this->_exception = $value; |
112
|
|
|
|
113
|
8 |
|
return $this->_exception; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Payload setter. |
118
|
|
|
* |
119
|
|
|
* @param string $key the key to be setted in the payload |
120
|
|
|
* @param mixed $value value to be setted as payload |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
30 |
|
public function setPayload($key, $value) |
124
|
|
|
{ |
125
|
30 |
|
$this->_payload[$key] = $value; |
126
|
30 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Payload api method |
130
|
|
|
* |
131
|
|
|
* @param string $key the key to get the payload |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
54 |
|
public function payload($key = null) |
135
|
|
|
{ |
136
|
54 |
|
if ($key === null) { |
137
|
54 |
|
return $this->_payload; |
138
|
|
|
} |
139
|
|
|
if (isset($this->_payload[$key])) { |
140
|
|
|
return $this->_payload[$key]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function toArray() |
147
|
|
|
{ |
148
|
|
|
$info = [ |
149
|
|
|
'code' => $this->_code, |
150
|
|
|
'data' => $this->_data, |
151
|
|
|
'payload' => $this->_payload, |
152
|
|
|
]; |
153
|
|
|
if ($this->_exception !== null) { |
154
|
|
|
$info['exception'] = $this->_exception->getMessage(); |
155
|
|
|
} |
156
|
|
|
return $info; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns an array that can be used to describe the internal state of this |
161
|
|
|
* object. |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
public function __debugInfo() |
166
|
|
|
{ |
167
|
|
|
return $this->toArray(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|