PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / api / response.php
vikappointments / site / helpers / libraries / api Last commit date
implementors 1 month ago plugins 1 month ago api.php 1 month ago autoload.php 1 month ago error.php 1 month ago event.php 1 month ago index.html 1 month ago response.php 1 month ago user.php 1 month ago
response.php
272 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * The API response wrapper.
16 *
17 * @since 1.7
18 */
19 class VAPApiResponse
20 {
21 /**
22 * The status of the response. True for success, false on failure.
23 *
24 * @var boolean
25 */
26 private $status = false;
27
28 /**
29 * The text description of the response.
30 *
31 * @var string
32 */
33 private $content = '';
34
35 /**
36 * The initial timestamp in seconds of the creation of this object.
37 *
38 * @var integer
39 */
40 private $startTime = 0;
41
42 /**
43 * Used to allow the events to force a specific content type.
44 * If not specified, the default application/json will be used.
45 *
46 * @var null|string
47 */
48 private $contentType = 'application/json';
49
50 /**
51 * Keeps track of the request payload.
52 *
53 * @var null|string
54 */
55 private $payload = null;
56
57 /**
58 * Class constructor.
59 *
60 * @param boolean $status True for success response, otherwise false.
61 * @param string $content The text description of the response.
62 *
63 * @uses setStatus() Set the status of the response.
64 * @uses setContent() Set the content of the response.
65 */
66 public function __construct($status = false, $content = '')
67 {
68 $this->setStatus($status)->setContent($content);
69
70 $this->startTime = microtime(true);
71 }
72
73 /**
74 * Set the status of the response.
75 *
76 * @param boolean $status True for success response, false otherwise.
77 *
78 * @return self This object to support chaining.
79 */
80 public function setStatus($status)
81 {
82 $this->status = $status;
83
84 return $this;
85 }
86
87 /**
88 * Return true if the status of the response is success, false otherwise.
89 *
90 * @return boolean
91 */
92 public function isVerified()
93 {
94 return $this->status == true;
95 }
96
97 /**
98 * Return true if the status of the response is failure, false otherwise.
99 *
100 * @return boolean
101 */
102 public function isError()
103 {
104 return $this->status == false;
105 }
106
107 /**
108 * Set the text description of the response.
109 *
110 * @param mixed $content The content of the response.
111 *
112 * @return self This object to support chaining.
113 */
114 public function setContent($content)
115 {
116 if (!is_scalar($content))
117 {
118 // stringify non-scalar value
119 $content = print_r($content, true);
120 }
121
122 $this->content = (string) $content;
123
124 return $this;
125 }
126
127 /**
128 * Append some text to the existing description of the response.
129 *
130 * @param string $content The content of the response.
131 *
132 * @return self This object to support chaining.
133 *
134 * @uses setContent()
135 */
136 public function appendContent($content)
137 {
138 // keep current contents
139 $tmp = $this->content;
140
141 // set contents with default method
142 $this->setContent($content);
143
144 // prepend previous contents
145 $this->content = $tmp . $this->content;
146
147 return $this;
148 }
149
150 /**
151 * Prepend some text to the existing description of the response.
152 *
153 * @param string $content The content of the response.
154 *
155 * @return self This object to support chaining.
156 *
157 * @uses setContent()
158 */
159 public function prependContent($content)
160 {
161 // keep current contents
162 $tmp = $this->content;
163
164 // set contents with default method
165 $this->setContent($content);
166
167 // append previous contents
168 $this->content .= $tmp;
169
170 return $this;
171 }
172
173 /**
174 * Clear the text description of the response.
175 *
176 * @return self This object to support chaining.
177 *
178 * @uses setContent()
179 */
180 public function clearContent()
181 {
182 return $this->setContent('');
183 }
184
185 /**
186 * Get the text description of the response.
187 *
188 * @return string The text description of the response.
189 */
190 public function getContent()
191 {
192 return $this->content;
193 }
194
195 /**
196 * Sets the given content type.
197 *
198 * @param string|null $type The content type to set.
199 *
200 * @return self This object to support chaining.
201 */
202 public function setContentType($type = null)
203 {
204 $this->contentType = $type;
205
206 return $this;
207 }
208
209 /**
210 * Returns the currently set content type.
211 *
212 * @return string|null
213 */
214 public function getContentType()
215 {
216 return $this->contentType;
217 }
218
219 /**
220 * Sets the given payload.
221 *
222 * @param mixed $type The payload to set.
223 *
224 * @return self This object to support chaining.
225 */
226 public function setPayload($payload = null)
227 {
228 if ($payload !== null && !is_scalar($payload))
229 {
230 // JSON encode received payload
231 $this->payload = json_encode($payload);
232 }
233 else
234 {
235 $this->payload = $payload;
236 }
237
238 return $this;
239 }
240
241 /**
242 * Returns the currently set payload.
243 *
244 * @return string|null
245 */
246 public function getPayload()
247 {
248 return $this->payload;
249 }
250
251 /**
252 * Get the initial timestamp of the response.
253 * The initial time is recorded during the creation of the response.
254 *
255 * @return integer The initial timestamp in seconds.
256 */
257 public function createdOn()
258 {
259 return $this->startTime;
260 }
261
262 /**
263 * Get the elapsed time between the current time and the initial time.
264 *
265 * @return integer The elapsed time in seconds.
266 */
267 public function getElapsedTime()
268 {
269 return microtime() - $this->startTime;
270 }
271 }
272