From: Michael Wallner Date: Mon, 30 Jan 2006 20:46:55 +0000 (+0000) Subject: - remove HttpQueryString::del() and ::mod() can be done with ::set() X-Git-Tag: RELEASE_0_22_0~8 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=253b7ec9bbb6006e4b79102ba8fe35a9fdf9ef46;p=m6w6%2Fext-http - remove HttpQueryString::del() and ::mod() can be done with ::set() - add optional second param to constructor --- diff --git a/http_querystring_object.c b/http_querystring_object.c index f9a0590..fc17c43 100644 --- a/http_querystring_object.c +++ b/http_querystring_object.c @@ -12,10 +12,12 @@ /* $Id$ */ +#define HTTP_WANT_SAPI #include "php_http.h" #ifdef ZEND_ENGINE_2 +#include "php_variables.h" #include "zend_interfaces.h" #include "php_http_api.h" @@ -30,13 +32,15 @@ HTTP_BEGIN_ARGS(__construct, 0, 0) HTTP_ARG_VAL(global, 0) + HTTP_ARG_VAL(params, 0) HTTP_END_ARGS; HTTP_BEGIN_ARGS(getInstance, 0, 0) HTTP_ARG_VAL(global, 0) HTTP_END_ARGS; -HTTP_EMPTY_ARGS(__toString, 0); +HTTP_EMPTY_ARGS(toArray, 0); +HTTP_EMPTY_ARGS(toString, 0); HTTP_BEGIN_ARGS(get, 0, 0) HTTP_ARG_VAL(name, 0) @@ -50,14 +54,6 @@ HTTP_BEGIN_ARGS(set, 0, 2) HTTP_ARG_VAL(value, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(del, 0, 1) - HTTP_ARG_VAL(params, 0) -HTTP_END_ARGS; - -HTTP_BEGIN_ARGS(mod, 0, 1) - HTTP_ARG_VAL(params, 0) -HTTP_END_ARGS; - HTTP_BEGIN_ARGS(__getter, 0, 1) HTTP_ARG_VAL(name, 0) HTTP_ARG_VAL(defval, 0) @@ -73,11 +69,14 @@ static inline void _http_querystring_object_declare_default_properties(TSRMLS_D) zend_class_entry *http_querystring_object_ce; zend_function_entry http_querystring_object_fe[] = { HTTP_QUERYSTRING_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL) - HTTP_QUERYSTRING_ME(__toString, ZEND_ACC_PUBLIC) + + HTTP_QUERYSTRING_ME(toArray, ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_ME(toString, ZEND_ACC_PUBLIC) + ZEND_MALIAS(HttpQueryString, __toString, toString, HTTP_ARGS(HttpQueryString, toString), ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_ME(get, ZEND_ACC_PUBLIC) HTTP_QUERYSTRING_ME(set, ZEND_ACC_PUBLIC) - HTTP_QUERYSTRING_ME(del, ZEND_ACC_PUBLIC) - HTTP_QUERYSTRING_ME(mod, ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_ME(getInstance, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) HTTP_QUERYSTRING_GME(getBool, ZEND_ACC_PUBLIC) @@ -179,11 +178,11 @@ static inline void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_D } } -#define http_querystring_modify_array(a, k, l, v) _http_querystring_modify_array((a), (k), (l), (v) TSRMLS_CC) -static inline int _http_querystring_modify_array(zval *qarray, char *key, uint keylen, zval *data TSRMLS_DC) +#define http_querystring_modify_ex(a, k, l, v) _http_querystring_modify_ex((a), (k), (l), (v) TSRMLS_CC) +static inline int _http_querystring_modify_ex(zval *qarray, char *key, uint keylen, zval *data TSRMLS_DC) { if (Z_TYPE_P(data) == IS_NULL) { - if (SUCCESS != zend_hash_del(Z_ARRVAL_P(qarray), key, keylen + 1)) { + if (SUCCESS != zend_hash_del(Z_ARRVAL_P(qarray), key, keylen)) { return 0; } } else { @@ -193,6 +192,56 @@ static inline int _http_querystring_modify_array(zval *qarray, char *key, uint k return 1; } +#define http_querystring_modify_array(q, a) _http_querystring_modify_array((q), (a) TSRMLS_CC) +static inline int _http_querystring_modify_array(zval *qarray, zval *array TSRMLS_DC) +{ + zval **value; + HashPosition pos; + char *key = NULL; + uint keylen = 0; + ulong idx = 0; + int rv = 0; + + FOREACH_KEYLENVAL(pos, array, key, keylen, idx, value) { + if (key) { + if (http_querystring_modify_ex(qarray, key, keylen, *value)) { + rv = 1; + } + } else { + keylen = spprintf(&key, 0, "%lu", idx); + if (http_querystring_modify_ex(qarray, key, keylen, *value)) { + rv = 1; + } + efree(key); + } + key = NULL; + } + + return rv; +} + +#define http_querystring_modify(q, p) _http_querystring_modify((q), (p) TSRMLS_CC) +static inline int _http_querystring_modify(zval *qarray, zval *params TSRMLS_DC) +{ + if (Z_TYPE_P(params) == IS_ARRAY) { + return http_querystring_modify_array(qarray, params); + } else { + int rv; + zval array; + + INIT_PZVAL(&array); + array_init(&array); + + ZVAL_ADDREF(params); + convert_to_string_ex(¶ms); + sapi_module.treat_data(PARSE_STRING, estrdup(Z_STRVAL_P(params)), &array TSRMLS_CC); + zval_ptr_dtor(¶ms); + rv = http_querystring_modify_array(qarray, &array); + zval_dtor(&array); + return rv; + } +} + #define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC) static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC) { @@ -231,7 +280,7 @@ static inline void _http_querystring_get(zval *this_ptr, int type, char *name, u } } -/* {{{ proto void HttpQueryString::__construct([bool global = true]) +/* {{{ proto void HttpQueryString::__construct([bool global = true[, mixed add]) * * Creates a new HttpQueryString object instance. * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE. @@ -239,10 +288,10 @@ static inline void _http_querystring_get(zval *this_ptr, int type, char *name, u PHP_METHOD(HttpQueryString, __construct) { zend_bool global = 1; - zval *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING; + zval *params = NULL, *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING; SET_EH_THROW_HTTP(); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bz", &global, ¶ms)) { if (global) { if ( (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &_SERVER)) && (Z_TYPE_PP(_SERVER) == IS_ARRAY) && @@ -268,29 +317,49 @@ PHP_METHOD(HttpQueryString, __construct) SET_PROP(queryString, qstring); GET_PROP(queryArray)->is_ref = 1; GET_PROP(queryString)->is_ref = 1; + + if (params && http_querystring_modify(GET_PROP(queryArray), params)) { + http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString)); + } } } else { qarray = ecalloc(1, sizeof(zval)); array_init(qarray); + SET_PROP(queryArray, qarray); UPD_STRL(queryString, "", 0); + + if (params && http_querystring_modify(qarray, params)) { + http_querystring_update(qarray, GET_PROP(queryString)); + } } } SET_EH_NORMAL(); } /* }}} */ -/* {{{ proto string HttpQueryString::__toString() +/* {{{ proto string HttpQueryString::toString() * * Returns the string representation. */ -PHP_METHOD(HttpQueryString, __toString) +PHP_METHOD(HttpQueryString, toString) { NO_ARGS; RETURN_PROP(queryString); } /* }}} */ +/* {{{ proto array HttpQueryString::toArray() + * + * Returns the array representation. + */ +PHP_METHOD(HttpQueryString, toArray) +{ + NO_ARGS; + RETURN_PROP(queryArray); +} +/* }}} */ + /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]]) * * Get (part of) the query string. @@ -331,97 +400,19 @@ PHP_METHOD(HttpQueryString, get) } /* }}} */ -/* {{{ proto string HttpQueryString::set(string name, mixed value) +/* {{{ proto string HttpQueryString::set(mixed params) * - * Set a query string entry. + * Set query string entry/entries. NULL values will unset the variable. */ PHP_METHOD(HttpQueryString, set) -{ - char *name; - int name_len; - zval *value; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name, &name_len, &value)) { - zval *qarray = GET_PROP(queryArray); - - if (http_querystring_modify_array(qarray, name, name_len, value)) { - http_querystring_update(qarray, GET_PROP(queryString)); - } - } - - IF_RETVAL_USED { - RETURN_PROP(queryString); - } -} -/* }}} */ - -/* {{{ proto string HttpQueryString::del(mixed param) - * - * Deletes entry/entries from the query string. - */ -PHP_METHOD(HttpQueryString, del) { zval *params; - + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", ¶ms)) { zval *qarray = GET_PROP(queryArray); - - if (Z_TYPE_P(qarray) == IS_ARRAY) { - if (Z_TYPE_P(params) == IS_ARRAY) { - HashPosition pos; - zval **name; - - FOREACH_VAL(pos, params, name) { - ZVAL_ADDREF(*name); - convert_to_string_ex(name); - zend_hash_del(Z_ARRVAL_P(qarray), Z_STRVAL_PP(name), Z_STRLEN_PP(name) + 1); - zval_ptr_dtor(name); - } - - http_querystring_update(qarray, GET_PROP(queryString)); - } else { - ZVAL_ADDREF(params); - convert_to_string_ex(¶ms); - if (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), Z_STRVAL_P(params), Z_STRLEN_P(params) + 1)) { - http_querystring_update(qarray, GET_PROP(queryString)); - } - zval_ptr_dtor(¶ms); - } - } - } - IF_RETVAL_USED { - RETURN_PROP(queryString); - } -} -/* }}} */ - -/* {{{ proto string HttpQueryString::mod(array params) - * - * Modifies the query string according to params. NULL values will unset the variable. - */ -PHP_METHOD(HttpQueryString, mod) -{ - zval *params; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", ¶ms)) { - zval **value, *qarray = GET_PROP(queryArray); - HashPosition pos; - char *key = NULL; - uint keylen = 0; - ulong idx = 0; - - FOREACH_KEYLENVAL(pos, params, key, keylen, idx, value) { - if (key) { - http_querystring_modify_array(qarray, key, keylen, *value); - } else { - keylen = spprintf(&key, 0, "%lu", idx); - http_querystring_modify_array(qarray, key, keylen, *value); - efree(key); - } - key = NULL; + if (http_querystring_modify(qarray, params)) { + http_querystring_update(qarray, GET_PROP(queryString)); } - - http_querystring_update(qarray, GET_PROP(queryString)); } IF_RETVAL_USED { diff --git a/php_http_querystring_object.h b/php_http_querystring_object.h index 90e9f09..e228bbc 100644 --- a/php_http_querystring_object.h +++ b/php_http_querystring_object.h @@ -40,11 +40,10 @@ extern zend_object_value _http_querystring_object_new_ex(zend_class_entry *ce, h extern void _http_querystring_object_free(zend_object *object TSRMLS_DC); PHP_METHOD(HttpQueryString, __construct); -PHP_METHOD(HttpQueryString, __toString); +PHP_METHOD(HttpQueryString, toString); +PHP_METHOD(HttpQueryString, toArray); PHP_METHOD(HttpQueryString, get); PHP_METHOD(HttpQueryString, set); -PHP_METHOD(HttpQueryString, del); -PHP_METHOD(HttpQueryString, mod); PHP_METHOD(HttpQueryString, getInstance); PHP_METHOD(HttpQueryString, getBool); PHP_METHOD(HttpQueryString, getInt); diff --git a/tests/HttpQueryString_001.phpt b/tests/HttpQueryString_001.phpt index a61e605..31137dd 100644 --- a/tests/HttpQueryString_001.phpt +++ b/tests/HttpQueryString_001.phpt @@ -25,10 +25,10 @@ var_dump(HttpQueryString::getInstance()->get('r')); var_dump(HttpQueryString::getInstance()->get('r', HttpQueryString::TYPE_ARRAY)); var_dump(HttpQueryString::getInstance()->get('r', HttpQueryString::TYPE_OBJECT)); -HttpQueryString::getInstance()->set('z', array(2)); +HttpQueryString::getInstance()->set('z[0]=2'); -HttpQueryString::getInstance()->mod(array('a'=>'b', 'c'=> "3.4")); -HttpQueryString::getInstance()->del('a'); +HttpQueryString::getInstance()->set(array('a'=>'b', 'c'=> "3.4")); +HttpQueryString::getInstance()->set(array('a' => NULL)); var_dump(HttpQueryString::getInstance()); var_dump($_GET); @@ -88,7 +88,7 @@ object(HttpQueryString)#1 (2) { ["z"]=> array(1) { [0]=> - int(2) + string(1) "2" } } ["queryString:private"]=> @@ -109,7 +109,7 @@ array(3) { ["z"]=> array(1) { [0]=> - int(2) + string(1) "2" } } string(33) "c=3.4&r[0]=1&r[1]=2&r[2]=3&z[0]=2" diff --git a/tests/HttpQueryString_002.phpt b/tests/HttpQueryString_002.phpt index 944f2a0..867c700 100644 --- a/tests/HttpQueryString_002.phpt +++ b/tests/HttpQueryString_002.phpt @@ -9,8 +9,7 @@ checkver(5); mod(array('a'=>'b','c'=>'3.4','r'=>array(1,2,3))); +$q = new HttpQueryString(false, $array = array('a'=>'b','c'=>'3.4','r'=>array(1,2,3))); var_dump($q->get()); var_dump($q->get('n')); var_dump($q->get('a')); @@ -24,14 +23,12 @@ var_dump($q->get('r')); var_dump($q->get('r', HttpQueryString::TYPE_ARRAY)); var_dump($q->get('r', HttpQueryString::TYPE_OBJECT)); -$q->set('z', array(2)); - -$q->mod(array('a'=>'b', 'c'=> "3.4")); -$q->del('a'); +$q->set('z[0]=2'); +$q->set(array('a'=>'b', 'c'=> "3.4")); +$q->set(array('a' => NULL)); var_dump($q); -var_dump($_GET); -var_dump($_SERVER['QUERY_STRING']); +var_dump($array); echo "Done\n"; ?> @@ -87,13 +84,25 @@ object(HttpQueryString)#1 (2) { ["z"]=> array(1) { [0]=> - int(2) + string(1) "2" } } ["queryString:private"]=> string(33) "c=3.4&r[0]=1&r[1]=2&r[2]=3&z[0]=2" } -array(0) { +array(3) { + ["a"]=> + string(1) "b" + ["c"]=> + string(3) "3.4" + ["r"]=> + array(3) { + [0]=> + int(1) + [1]=> + int(2) + [2]=> + int(3) + } } -NULL Done \ No newline at end of file