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 "kernel/array.h"
18 #include "ext/spl/spl_exceptions.h"
19 #include "kernel/exception.h"
20 #include "kernel/operators.h"
21
22
23 /**
24 * Phalcon\Mvc\Collection\Behavior
25 *
26 * This is an optional base class for ORM behaviors
27 */
ZEPHIR_INIT_CLASS(Phalcon_Mvc_Collection_Behavior)28 ZEPHIR_INIT_CLASS(Phalcon_Mvc_Collection_Behavior) {
29
30 ZEPHIR_REGISTER_CLASS(Phalcon\\Mvc\\Collection, Behavior, phalcon, mvc_collection_behavior, phalcon_mvc_collection_behavior_method_entry, ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
31
32 zend_declare_property_null(phalcon_mvc_collection_behavior_ce, SL("_options"), ZEND_ACC_PROTECTED TSRMLS_CC);
33
34 zend_class_implements(phalcon_mvc_collection_behavior_ce TSRMLS_CC, 1, phalcon_mvc_collection_behaviorinterface_ce);
35 return SUCCESS;
36
37 }
38
39 /**
40 * Phalcon\Mvc\Collection\Behavior
41 *
42 * @param array options
43 */
PHP_METHOD(Phalcon_Mvc_Collection_Behavior,__construct)44 PHP_METHOD(Phalcon_Mvc_Collection_Behavior, __construct) {
45
46 zval *options = NULL;
47
48 zephir_fetch_params(0, 0, 1, &options);
49
50 if (!options) {
51 options = ZEPHIR_GLOBAL(global_null);
52 }
53
54
55 zephir_update_property_this(getThis(), SL("_options"), options TSRMLS_CC);
56
57 }
58
59 /**
60 * Checks whether the behavior must take action on certain event
61 */
PHP_METHOD(Phalcon_Mvc_Collection_Behavior,mustTakeAction)62 PHP_METHOD(Phalcon_Mvc_Collection_Behavior, mustTakeAction) {
63
64 zval *eventName_param = NULL, *_0;
65 zval *eventName = NULL;
66
67 ZEPHIR_MM_GROW();
68 zephir_fetch_params(1, 1, 0, &eventName_param);
69
70 if (UNEXPECTED(Z_TYPE_P(eventName_param) != IS_STRING && Z_TYPE_P(eventName_param) != IS_NULL)) {
71 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'eventName' must be a string") TSRMLS_CC);
72 RETURN_MM_NULL();
73 }
74 if (EXPECTED(Z_TYPE_P(eventName_param) == IS_STRING)) {
75 zephir_get_strval(eventName, eventName_param);
76 } else {
77 ZEPHIR_INIT_VAR(eventName);
78 ZVAL_EMPTY_STRING(eventName);
79 }
80
81
82 _0 = zephir_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY_CC);
83 RETURN_MM_BOOL(zephir_array_isset(_0, eventName));
84
85 }
86
87 /**
88 * Returns the behavior options related to an event
89 *
90 * @param string eventName
91 * @return array
92 */
PHP_METHOD(Phalcon_Mvc_Collection_Behavior,getOptions)93 PHP_METHOD(Phalcon_Mvc_Collection_Behavior, getOptions) {
94
95 zval *eventName_param = NULL, *options = NULL, *eventOptions = NULL;
96 zval *eventName = NULL;
97
98 ZEPHIR_MM_GROW();
99 zephir_fetch_params(1, 0, 1, &eventName_param);
100
101 if (!eventName_param) {
102 ZEPHIR_INIT_VAR(eventName);
103 ZVAL_EMPTY_STRING(eventName);
104 } else {
105 if (UNEXPECTED(Z_TYPE_P(eventName_param) != IS_STRING && Z_TYPE_P(eventName_param) != IS_NULL)) {
106 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'eventName' must be a string") TSRMLS_CC);
107 RETURN_MM_NULL();
108 }
109 if (EXPECTED(Z_TYPE_P(eventName_param) == IS_STRING)) {
110 zephir_get_strval(eventName, eventName_param);
111 } else {
112 ZEPHIR_INIT_VAR(eventName);
113 ZVAL_EMPTY_STRING(eventName);
114 }
115 }
116
117
118 options = zephir_fetch_nproperty_this(this_ptr, SL("_options"), PH_NOISY_CC);
119 if (!ZEPHIR_IS_STRING_IDENTICAL(eventName, "")) {
120 if (zephir_array_isset_fetch(&eventOptions, options, eventName, 1 TSRMLS_CC)) {
121 RETURN_CTOR(eventOptions);
122 }
123 RETURN_MM_NULL();
124 }
125 RETURN_CTOR(options);
126
127 }
128
129 /**
130 * This method receives the notifications from the EventsManager
131 */
PHP_METHOD(Phalcon_Mvc_Collection_Behavior,notify)132 PHP_METHOD(Phalcon_Mvc_Collection_Behavior, notify) {
133
134 zval *type_param = NULL, *model;
135 zval *type = NULL;
136
137 ZEPHIR_MM_GROW();
138 zephir_fetch_params(1, 2, 0, &type_param, &model);
139
140 zephir_get_strval(type, type_param);
141
142
143 RETURN_MM_NULL();
144
145 }
146
147 /**
148 * Acts as fallbacks when a missing method is called on the collection
149 */
PHP_METHOD(Phalcon_Mvc_Collection_Behavior,missingMethod)150 PHP_METHOD(Phalcon_Mvc_Collection_Behavior, missingMethod) {
151
152 zval *method = NULL;
153 zval *model, *method_param = NULL, *arguments = NULL;
154
155 ZEPHIR_MM_GROW();
156 zephir_fetch_params(1, 2, 1, &model, &method_param, &arguments);
157
158 zephir_get_strval(method, method_param);
159 if (!arguments) {
160 arguments = ZEPHIR_GLOBAL(global_null);
161 }
162
163
164 RETURN_MM_NULL();
165
166 }
167
168