1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors:                                                             |
14    |          Israel Ekpo <iekpo@php.net>                                 |
15    |          Omar Shaban <omars@php.net>                                 |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "php_solr.h"
20 
21 /* {{{ PHP_SOLR_API void solr_object_write_property(zval *object, zval *member, zval *value) */
22 #if PHP_VERSION_ID < 70400
solr_object_write_property(zval * object,zval * member,zval * value,void ** cache_slot)23 PHP_SOLR_API void  solr_object_write_property(zval *object, zval *member, zval *value, void **cache_slot)
24 #elif PHP_VERSION_ID < 80000
25 PHP_SOLR_API zval *solr_object_write_property(zval *object, zval *member, zval *value, void **cache_slot)
26 #else
27 PHP_SOLR_API zval *solr_object_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot)
28 #endif
29 {
30 	solr_throw_exception(solr_ce_SolrIllegalOperationException, SOLR_ERROR_1006_MSG, SOLR_ERROR_1006, SOLR_FILE_LINE_FUNC);
31 
32 #if PHP_VERSION_ID >= 70400
33 	return value;
34 #endif
35 }
36 /* }}} */
37 
38 /* {{{ PHP_SOLR_API void solr_object_write_dimension(zval *object, zval *offset, zval *value) */
39 #if PHP_VERSION_ID < 80000
solr_object_write_dimension(zval * object,zval * offset,zval * value)40 PHP_SOLR_API void solr_object_write_dimension(zval *object, zval *offset, zval *value)
41 #else
42 PHP_SOLR_API void solr_object_write_dimension(zend_object *object, zval *offset, zval *value)
43 #endif
44 {
45 	solr_throw_exception(solr_ce_SolrIllegalOperationException, SOLR_ERROR_1006_MSG, SOLR_ERROR_1006, SOLR_FILE_LINE_FUNC);
46 
47 	if (Z_TYPE_P(offset) == IS_STRING)
48 	{
49 		php_error_docref(NULL, E_WARNING, "Attempting to set value for [%s] property in a SolrObject instance", Z_STRVAL_P(offset));
50 	}
51 }
52 /* }}} */
53 
54 /* {{{ PHP_SOLR_API void solr_object_unset_property(zval *object, zval *member) */
55 #if PHP_VERSION_ID < 80000
solr_object_unset_property(zval * object,zval * member,void ** cache_slot)56 PHP_SOLR_API void solr_object_unset_property(zval *object, zval *member, void **cache_slot)
57 #else
58 PHP_SOLR_API void solr_object_unset_property(zend_object *object, zend_string *member, void **cache_slot)
59 #endif
60 {
61 	solr_throw_exception(solr_ce_SolrIllegalOperationException, SOLR_ERROR_1006_MSG, SOLR_ERROR_1006, SOLR_FILE_LINE_FUNC);
62 }
63 /* }}} */
64 
65 /* {{{ PHP_SOLR_API void solr_object_unset_dimension(zval *object, zval *offset) */
66 #if PHP_VERSION_ID < 80000
solr_object_unset_dimension(zval * object,zval * offset)67 PHP_SOLR_API void solr_object_unset_dimension(zval *object, zval *offset)
68 #else
69 PHP_SOLR_API void solr_object_unset_dimension(zend_object *object, zval *offset)
70 #endif
71 {
72 	solr_throw_exception(solr_ce_SolrIllegalOperationException, SOLR_ERROR_1006_MSG, SOLR_ERROR_1006, SOLR_FILE_LINE_FUNC);
73 
74 	if (Z_TYPE_P(offset) == IS_STRING)
75 	{
76 		php_error_docref(NULL, E_WARNING, "Attempting to remove [%s] property in a SolrObject instance", Z_STRVAL_P(offset));
77 	}
78 }
79 /* }}} */
80 
81 /* {{{ PHP_SOLR_API zval *solr_object_read_property(zval *object, zval *member, int type) */
82 #if PHP_VERSION_ID < 80000
solr_object_read_property(zval * object,zval * member,int type,void ** cache_slot,zval * rv)83 PHP_SOLR_API zval *solr_object_read_property(zval *object, zval *member, int type, void **cache_slot, zval* rv)
84 #else
85 PHP_SOLR_API zval *solr_object_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval* rv)
86 #endif
87 {
88 	zval *value = &EG(uninitialized_zval);
89 	char *name = NULL;
90 	HashTable *properties = NULL;
91 
92 #if PHP_VERSION_ID < 70000
93 	if (Z_TYPE_P(member) != IS_STRING)
94 	{
95 		return value;
96 	}
97 #endif
98 
99 	SOLR_HASHTABLE_FOR_LOOP(properties)
100 	{
101 		char *property_name = NULL;
102 
103 		/* If the property name is in the HashTable */
104 		if (property_name && !strcmp(property_name, name))
105 		{
106 			value = zend_hash_get_current_data(properties);
107 		}
108 	}
109 
110 	return value;
111 }
112 /* }}} */
113 
114 /* {{{ proto SolrObject::__construct(void)
115    Constructor for SolrObject */
PHP_METHOD(SolrObject,__construct)116 PHP_METHOD(SolrObject, __construct)
117 {
118 	Z_OBJ_HT_P(getThis()) = &solr_object_handlers;
119 }
120 /* }}} */
121 
122 /* {{{ proto SolrObject::__destruct(void)
123    Destructor for SolrObject. */
PHP_METHOD(SolrObject,__destruct)124 PHP_METHOD(SolrObject, __destruct)
125 {
126 
127 }
128 /* }}} */
129 
130 /* {{{ proto void SolrObject::__set(string property_name, mixed value)
131    Magic method for setting/updating property values. */
PHP_METHOD(SolrObject,__set)132 PHP_METHOD(SolrObject, __set)
133 {
134 
135 }
136 /* }}} */
137 
138 /* {{{ proto mixed SolrObject::__get(string property_name)
139    Magic method for retrieving properties. */
PHP_METHOD(SolrObject,__get)140 PHP_METHOD(SolrObject, __get)
141 {
142 	solr_char_t *name = NULL;
143 	COMPAT_ARG_SIZE_T name_len = 0;
144 	zval *property = NULL, rv;
145 	zend_bool copy_value = 1;
146 
147 
148 	void *dtor = NULL;
149 
150 	/* Process the parameters passed to the method */
151 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
152 
153 		RETURN_FALSE;
154 	}
155 
156 	property = zend_read_property(solr_ce_SolrObject, OBJ_FOR_PROP(getThis()), name, name_len, 0, &rv);
157 
158 	if (property)
159 	{
160 		RETURN_ZVAL(property, copy_value, dtor);
161 	}
162 }
163 /* }}} */
164 
165 /* {{{ proto bool SolrObject::__isset(string property_name)
166    Magic method for checking if a property exists in the object. */
PHP_METHOD(SolrObject,__isset)167 PHP_METHOD(SolrObject, __isset)
168 {
169 	solr_char_t *name = NULL;
170 	COMPAT_ARG_SIZE_T name_len = 0;
171 	zend_object *zobject = NULL;
172 	zval *value = NULL;
173 
174 	/* Process the parameters passed to the method */
175 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
176 
177 		RETURN_FALSE;
178 	}
179 
180 	zobject = Z_OBJ_P(getThis());
181 
182 	value = zend_hash_str_find(zobject->properties, name, name_len);
183 
184 	if (value)
185 	{
186 		RETURN_TRUE;
187 	}
188 
189 	RETURN_FALSE;
190 }
191 /* }}} */
192 
193 /* {{{ proto void SolrObject::__unset(string property_name)
194    Magic method for removing the specified property. */
PHP_METHOD(SolrObject,__unset)195 PHP_METHOD(SolrObject, __unset)
196 {
197 	solr_char_t *name = NULL;
198 	COMPAT_ARG_SIZE_T name_len = 0;
199 
200 	/* Process the parameters passed to the method */
201 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
202 
203 		return;
204 	}
205 
206 	solr_throw_exception_ex(solr_ce_SolrIllegalOperationException, SOLR_ERROR_1002, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1002_MSG, name);
207 
208 	RETURN_FALSE;
209 }
210 /* }}} */
211 
212 /* {{{ proto void SolrObject::offsetSet(string key, mixed value)
213    Sets the specified key to value. */
PHP_METHOD(SolrObject,offsetSet)214 PHP_METHOD(SolrObject, offsetSet)
215 {
216 	solr_char_t *name = NULL;
217 	COMPAT_ARG_SIZE_T name_len = 0;
218 	zval *prop = NULL;
219 
220 	/* Process the parameters passed to the method */
221 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &name, &name_len, &prop) == FAILURE) {
222 
223 		RETURN_FALSE;
224 	}
225 
226 	if (prop && Z_TYPE_P(prop) == IS_NULL)
227 	{
228 		solr_throw_exception_ex(solr_ce_SolrIllegalOperationException, SOLR_ERROR_1002, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1002_MSG, name);
229 
230 		return ;
231 	}
232 
233 	zend_update_property(Z_OBJCE_P(getThis()), OBJ_FOR_PROP(getThis()), name, name_len, prop);
234 }
235 /* }}} */
236 
237 /* {{{ proto mixed SolrObject::offsetGet(string key)
238    Retrieves the requested key. */
PHP_METHOD(SolrObject,offsetGet)239 PHP_METHOD(SolrObject, offsetGet)
240 {
241 	solr_char_t *name = NULL;
242 	COMPAT_ARG_SIZE_T name_len = 0;
243 	zend_object *zobject = Z_OBJ_P(getThis());
244 	HashTable *properties = zobject->properties;
245 	zval *property_value = NULL;
246 	zend_bool copy_value = 1;
247 
248 	void *dtor = NULL;
249 
250 	/* Process the parameters passed to the method */
251 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
252 
253 		RETURN_FALSE;
254 	}
255 
256 	if ((property_value = zend_hash_str_find(properties, name, name_len)) != NULL)
257 	{
258 	    goto job_complete;
259 	} else {
260 	    RETURN_NULL();
261 	}
262 
263 job_complete :
264 
265 	if (property_value && (property_value))
266 	{
267 		RETURN_ZVAL((property_value), copy_value, dtor);
268 	}
269 }
270 /* }}} */
271 
272 /* {{{ proto bool SolrObject::offsetExists(string key)
273    Checks if the key exists. */
PHP_METHOD(SolrObject,offsetExists)274 PHP_METHOD(SolrObject, offsetExists)
275 {
276 	solr_char_t *name = NULL;
277 	COMPAT_ARG_SIZE_T name_len = 0;
278 	zend_object *zobject = Z_OBJ_P(getThis());
279 	HashTable *properties = zobject->properties;
280 	zend_bool property_exists = 0;
281 
282 	/* Process the parameters passed to the method */
283 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
284 		RETURN_FALSE;
285 	}
286 	if (!properties) {
287 	    RETURN_FALSE;
288 	}
289 	property_exists = zend_hash_str_exists(properties, name, name_len);
290 
291 	zend_hash_internal_pointer_reset(properties);
292 
293 	RETURN_BOOL(property_exists);
294 }
295 /* }}} */
296 
297 /* {{{ proto void SolrObject::offsetUnset(string key)
298    Removes the specified key. */
PHP_METHOD(SolrObject,offsetUnset)299 PHP_METHOD(SolrObject, offsetUnset)
300 {
301 	solr_char_t *name = NULL;
302 	COMPAT_ARG_SIZE_T name_len = 0;
303 
304 	/* Process the parameters passed to the method */
305 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
306 
307 		return;
308 	}
309 
310 	solr_throw_exception_ex(solr_ce_SolrIllegalOperationException, SOLR_ERROR_1002, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1002_MSG, name);
311 
312 	RETURN_FALSE;
313 }
314 /* }}} */
315 
316 /* {{{ proto array SolrObject::getPropertyNames(void)
317    Returns an array of all the properties/keys in the object. */
PHP_METHOD(SolrObject,getPropertyNames)318 PHP_METHOD(SolrObject, getPropertyNames)
319 {
320 	zend_object *zobject = Z_OBJ_P(getThis());
321 	zval new_val;
322 	zend_ulong num_idx;
323 	zend_string *str_idx;
324 	HashTable *properties = zobject->properties;
325 
326 	if (! properties || !zend_hash_num_elements(properties)) {
327 	    array_init_size(return_value, 0);
328 	    zend_hash_real_init(Z_ARRVAL_P(return_value), 1);
329 	    return;
330 	}
331 	array_init_size(return_value, zend_hash_num_elements(properties));
332 	zend_hash_real_init(Z_ARRVAL_P(return_value), 1);
333 	ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
334 	    /* Go through input array and add keys to the return array */
335 	    ZEND_HASH_FOREACH_KEY(properties, num_idx, str_idx) {
336 	        if (str_idx) {
337 	            ZVAL_STR_COPY(&new_val, str_idx);
338 	        } else {
339 	            ZVAL_LONG(&new_val, num_idx);
340 	        }
341 	        ZEND_HASH_FILL_ADD(&new_val);
342 	    } ZEND_HASH_FOREACH_END();
343 	} ZEND_HASH_FILL_END();
344 }
345 /* }}} */
346 
347 /*
348  * Local variables:
349  * tab-width: 4
350  * c-basic-offset: 4
351  * indent-tabs-mode: t
352  * End:
353  * vim600: fdm=marker
354  * vim: noet sw=4 ts=4
355  */
356