1
2 #ifdef HAVE_CONFIG_H
3 #include "../../ext_config.h"
4 #endif
5
6 #include <php.h>
7 #include "../../php_ext.h"
8 #include "../../ext.h"
9
10 #include <Zend/zend_operators.h>
11 #include <Zend/zend_exceptions.h>
12 #include <Zend/zend_interfaces.h>
13
14 #include "kernel/main.h"
15 #include "kernel/object.h"
16 #include "kernel/memory.h"
17 #include "ext/spl/spl_exceptions.h"
18 #include "kernel/exception.h"
19 #include "kernel/operators.h"
20 #include "kernel/array.h"
21
22
23 /**
24 * This file is part of the Phalcon Framework.
25 *
26 * (c) Phalcon Team <team@phalcon.io>
27 *
28 * For the full copyright and license information, please view the LICENSE.txt
29 * file that was distributed with this source code.
30 */
31 /**
32 * Phalcon\Messages\Message
33 *
34 * Stores a message from various components
35 */
ZEPHIR_INIT_CLASS(Phalcon_Messages_Message)36 ZEPHIR_INIT_CLASS(Phalcon_Messages_Message) {
37
38 ZEPHIR_REGISTER_CLASS(Phalcon\\Messages, Message, phalcon, messages_message, phalcon_messages_message_method_entry, 0);
39
40 /**
41 * @var int
42 */
43 zend_declare_property_null(phalcon_messages_message_ce, SL("code"), ZEND_ACC_PROTECTED);
44
45 /**
46 * @var string
47 */
48 zend_declare_property_null(phalcon_messages_message_ce, SL("field"), ZEND_ACC_PROTECTED);
49
50 /**
51 * @var string
52 */
53 zend_declare_property_null(phalcon_messages_message_ce, SL("message"), ZEND_ACC_PROTECTED);
54
55 /**
56 * @var string
57 */
58 zend_declare_property_null(phalcon_messages_message_ce, SL("type"), ZEND_ACC_PROTECTED);
59
60 /**
61 * @var array
62 */
63 zend_declare_property_null(phalcon_messages_message_ce, SL("metaData"), ZEND_ACC_PROTECTED);
64
65 phalcon_messages_message_ce->create_object = zephir_init_properties_Phalcon_Messages_Message;
66
67 zend_class_implements(phalcon_messages_message_ce, 1, phalcon_messages_messageinterface_ce);
68 zend_class_implements(phalcon_messages_message_ce, 1, zephir_get_internal_ce(SL("jsonserializable")));
69 return SUCCESS;
70
71 }
72
73 /**
74 */
PHP_METHOD(Phalcon_Messages_Message,getCode)75 PHP_METHOD(Phalcon_Messages_Message, getCode) {
76
77 zval *this_ptr = getThis();
78
79
80 RETURN_MEMBER(getThis(), "code");
81
82 }
83
84 /**
85 */
PHP_METHOD(Phalcon_Messages_Message,getField)86 PHP_METHOD(Phalcon_Messages_Message, getField) {
87
88 zval *this_ptr = getThis();
89
90
91 RETURN_MEMBER(getThis(), "field");
92
93 }
94
95 /**
96 */
PHP_METHOD(Phalcon_Messages_Message,getMessage)97 PHP_METHOD(Phalcon_Messages_Message, getMessage) {
98
99 zval *this_ptr = getThis();
100
101
102 RETURN_MEMBER(getThis(), "message");
103
104 }
105
106 /**
107 */
PHP_METHOD(Phalcon_Messages_Message,getType)108 PHP_METHOD(Phalcon_Messages_Message, getType) {
109
110 zval *this_ptr = getThis();
111
112
113 RETURN_MEMBER(getThis(), "type");
114
115 }
116
117 /**
118 */
PHP_METHOD(Phalcon_Messages_Message,getMetaData)119 PHP_METHOD(Phalcon_Messages_Message, getMetaData) {
120
121 zval *this_ptr = getThis();
122
123
124 RETURN_MEMBER(getThis(), "metaData");
125
126 }
127
128 /**
129 * Phalcon\Messages\Message constructor
130 */
PHP_METHOD(Phalcon_Messages_Message,__construct)131 PHP_METHOD(Phalcon_Messages_Message, __construct) {
132
133 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
134 zval metaData;
135 zend_long code;
136 zval *message_param = NULL, *field = NULL, field_sub, *type_param = NULL, *code_param = NULL, *metaData_param = NULL, _0;
137 zval message, type;
138 zval *this_ptr = getThis();
139
140 ZVAL_UNDEF(&message);
141 ZVAL_UNDEF(&type);
142 ZVAL_UNDEF(&field_sub);
143 ZVAL_UNDEF(&_0);
144 ZVAL_UNDEF(&metaData);
145
146 ZEPHIR_MM_GROW();
147 zephir_fetch_params(1, 1, 4, &message_param, &field, &type_param, &code_param, &metaData_param);
148
149 if (UNEXPECTED(Z_TYPE_P(message_param) != IS_STRING && Z_TYPE_P(message_param) != IS_NULL)) {
150 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'message' must be of the type string"));
151 RETURN_MM_NULL();
152 }
153 if (EXPECTED(Z_TYPE_P(message_param) == IS_STRING)) {
154 zephir_get_strval(&message, message_param);
155 } else {
156 ZEPHIR_INIT_VAR(&message);
157 ZVAL_EMPTY_STRING(&message);
158 }
159 if (!field) {
160 field = &field_sub;
161 ZEPHIR_INIT_VAR(field);
162 ZVAL_STRING(field, "");
163 }
164 if (!type_param) {
165 ZEPHIR_INIT_VAR(&type);
166 ZVAL_STRING(&type, "");
167 } else {
168 zephir_get_strval(&type, type_param);
169 }
170 if (!code_param) {
171 code = 0;
172 } else {
173 code = zephir_get_intval(code_param);
174 }
175 if (!metaData_param) {
176 ZEPHIR_INIT_VAR(&metaData);
177 array_init(&metaData);
178 } else {
179 zephir_get_arrval(&metaData, metaData_param);
180 }
181
182
183 zephir_update_property_zval(this_ptr, ZEND_STRL("message"), &message);
184 zephir_update_property_zval(this_ptr, ZEND_STRL("field"), field);
185 zephir_update_property_zval(this_ptr, ZEND_STRL("type"), &type);
186 ZEPHIR_INIT_ZVAL_NREF(_0);
187 ZVAL_LONG(&_0, code);
188 zephir_update_property_zval(this_ptr, ZEND_STRL("code"), &_0);
189 zephir_update_property_zval(this_ptr, ZEND_STRL("metaData"), &metaData);
190 ZEPHIR_MM_RESTORE();
191
192 }
193
194 /**
195 * Magic __toString method returns verbose message
196 */
PHP_METHOD(Phalcon_Messages_Message,__toString)197 PHP_METHOD(Phalcon_Messages_Message, __toString) {
198
199 zval *this_ptr = getThis();
200
201
202 RETURN_MEMBER(getThis(), "message");
203
204 }
205
206 /**
207 * Serializes the object for json_encode
208 */
PHP_METHOD(Phalcon_Messages_Message,jsonSerialize)209 PHP_METHOD(Phalcon_Messages_Message, jsonSerialize) {
210
211 zval _0;
212 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
213 zval *this_ptr = getThis();
214
215 ZVAL_UNDEF(&_0);
216
217 ZEPHIR_MM_GROW();
218
219 zephir_create_array(return_value, 5, 0);
220 ZEPHIR_OBS_VAR(&_0);
221 zephir_read_property(&_0, this_ptr, ZEND_STRL("field"), PH_NOISY_CC);
222 zephir_array_update_string(return_value, SL("field"), &_0, PH_COPY | PH_SEPARATE);
223 ZEPHIR_OBS_NVAR(&_0);
224 zephir_read_property(&_0, this_ptr, ZEND_STRL("message"), PH_NOISY_CC);
225 zephir_array_update_string(return_value, SL("message"), &_0, PH_COPY | PH_SEPARATE);
226 ZEPHIR_OBS_NVAR(&_0);
227 zephir_read_property(&_0, this_ptr, ZEND_STRL("type"), PH_NOISY_CC);
228 zephir_array_update_string(return_value, SL("type"), &_0, PH_COPY | PH_SEPARATE);
229 ZEPHIR_OBS_NVAR(&_0);
230 zephir_read_property(&_0, this_ptr, ZEND_STRL("code"), PH_NOISY_CC);
231 zephir_array_update_string(return_value, SL("code"), &_0, PH_COPY | PH_SEPARATE);
232 ZEPHIR_OBS_NVAR(&_0);
233 zephir_read_property(&_0, this_ptr, ZEND_STRL("metaData"), PH_NOISY_CC);
234 zephir_array_update_string(return_value, SL("metaData"), &_0, PH_COPY | PH_SEPARATE);
235 RETURN_MM();
236
237 }
238
239 /**
240 * Sets code for the message
241 */
PHP_METHOD(Phalcon_Messages_Message,setCode)242 PHP_METHOD(Phalcon_Messages_Message, setCode) {
243
244 zval *code_param = NULL, _0;
245 zend_long code;
246 zval *this_ptr = getThis();
247
248 ZVAL_UNDEF(&_0);
249
250 zephir_fetch_params_without_memory_grow(1, 0, &code_param);
251
252 code = zephir_get_intval(code_param);
253
254
255 ZEPHIR_INIT_ZVAL_NREF(_0);
256 ZVAL_LONG(&_0, code);
257 zephir_update_property_zval(this_ptr, ZEND_STRL("code"), &_0);
258 RETURN_THISW();
259
260 }
261
262 /**
263 * Sets field name related to message
264 */
PHP_METHOD(Phalcon_Messages_Message,setField)265 PHP_METHOD(Phalcon_Messages_Message, setField) {
266
267 zval *field, field_sub;
268 zval *this_ptr = getThis();
269
270 ZVAL_UNDEF(&field_sub);
271
272 zephir_fetch_params_without_memory_grow(1, 0, &field);
273
274
275
276 zephir_update_property_zval(this_ptr, ZEND_STRL("field"), field);
277 RETURN_THISW();
278
279 }
280
281 /**
282 * Sets verbose message
283 */
PHP_METHOD(Phalcon_Messages_Message,setMessage)284 PHP_METHOD(Phalcon_Messages_Message, setMessage) {
285
286 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
287 zval *message_param = NULL;
288 zval message;
289 zval *this_ptr = getThis();
290
291 ZVAL_UNDEF(&message);
292
293 ZEPHIR_MM_GROW();
294 zephir_fetch_params(1, 1, 0, &message_param);
295
296 if (UNEXPECTED(Z_TYPE_P(message_param) != IS_STRING && Z_TYPE_P(message_param) != IS_NULL)) {
297 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'message' must be of the type string"));
298 RETURN_MM_NULL();
299 }
300 if (EXPECTED(Z_TYPE_P(message_param) == IS_STRING)) {
301 zephir_get_strval(&message, message_param);
302 } else {
303 ZEPHIR_INIT_VAR(&message);
304 ZVAL_EMPTY_STRING(&message);
305 }
306
307
308 zephir_update_property_zval(this_ptr, ZEND_STRL("message"), &message);
309 RETURN_THIS();
310
311 }
312
313 /**
314 * Sets message metadata
315 */
PHP_METHOD(Phalcon_Messages_Message,setMetaData)316 PHP_METHOD(Phalcon_Messages_Message, setMetaData) {
317
318 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
319 zval *metaData_param = NULL;
320 zval metaData;
321 zval *this_ptr = getThis();
322
323 ZVAL_UNDEF(&metaData);
324
325 ZEPHIR_MM_GROW();
326 zephir_fetch_params(1, 1, 0, &metaData_param);
327
328 ZEPHIR_OBS_COPY_OR_DUP(&metaData, metaData_param);
329
330
331 zephir_update_property_zval(this_ptr, ZEND_STRL("metaData"), &metaData);
332 RETURN_THIS();
333
334 }
335
336 /**
337 * Sets message type
338 */
PHP_METHOD(Phalcon_Messages_Message,setType)339 PHP_METHOD(Phalcon_Messages_Message, setType) {
340
341 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
342 zval *type_param = NULL;
343 zval type;
344 zval *this_ptr = getThis();
345
346 ZVAL_UNDEF(&type);
347
348 ZEPHIR_MM_GROW();
349 zephir_fetch_params(1, 1, 0, &type_param);
350
351 if (UNEXPECTED(Z_TYPE_P(type_param) != IS_STRING && Z_TYPE_P(type_param) != IS_NULL)) {
352 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'type' must be of the type string"));
353 RETURN_MM_NULL();
354 }
355 if (EXPECTED(Z_TYPE_P(type_param) == IS_STRING)) {
356 zephir_get_strval(&type, type_param);
357 } else {
358 ZEPHIR_INIT_VAR(&type);
359 ZVAL_EMPTY_STRING(&type);
360 }
361
362
363 zephir_update_property_zval(this_ptr, ZEND_STRL("type"), &type);
364 RETURN_THIS();
365
366 }
367
zephir_init_properties_Phalcon_Messages_Message(zend_class_entry * class_type TSRMLS_DC)368 zend_object *zephir_init_properties_Phalcon_Messages_Message(zend_class_entry *class_type TSRMLS_DC) {
369
370 zval _0, _1$$3;
371 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
372 ZVAL_UNDEF(&_0);
373 ZVAL_UNDEF(&_1$$3);
374
375 ZEPHIR_MM_GROW();
376
377 {
378 zval local_this_ptr, *this_ptr = &local_this_ptr;
379 ZEPHIR_CREATE_OBJECT(this_ptr, class_type);
380 zephir_read_property_ex(&_0, this_ptr, ZEND_STRL("metaData"), PH_NOISY_CC | PH_READONLY);
381 if (Z_TYPE_P(&_0) == IS_NULL) {
382 ZEPHIR_INIT_VAR(&_1$$3);
383 array_init(&_1$$3);
384 zephir_update_property_zval_ex(this_ptr, ZEND_STRL("metaData"), &_1$$3);
385 }
386 ZEPHIR_MM_RESTORE();
387 return Z_OBJ_P(this_ptr);
388 }
389
390 }
391
392