summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS2
-rw-r--r--ext/filter/filter.c22
-rw-r--r--ext/filter/tests/054.phpt26
3 files changed, 42 insertions, 8 deletions
diff --git a/NEWS b/NEWS
index b7bb64ba7e..93108980d6 100644
--- a/NEWS
+++ b/NEWS
@@ -42,6 +42,8 @@ PHP NEWS
. Fixed bug #54121 (error message format string typo). (Ilia)
- Filter extension:
+ . Added 3rd parameter to filter_var_array() and filter_input_array()
+ functions that allows disabling addition of empty elements. (Ilia)
. Fixed bug #53037 (FILTER_FLAG_EMPTY_STRING_NULL is not implemented). (Ilia)
- intl extension:
diff --git a/ext/filter/filter.c b/ext/filter/filter.c
index 8a0919358a..a1882ea3a4 100644
--- a/ext/filter/filter.c
+++ b/ext/filter/filter.c
@@ -96,11 +96,13 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_input_array, 0, 0, 1)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, definition)
+ ZEND_ARG_INFO(0, add_empty)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_filter_var_array, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, definition)
+ ZEND_ARG_INFO(0, add_empty)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_filter_list, 0)
@@ -689,7 +691,7 @@ static void php_filter_call(zval **filtered, long filter, zval **filter_args, co
}
/* }}} */
-static void php_filter_array_handler(zval *input, zval **op, zval *return_value TSRMLS_DC) /* {{{ */
+static void php_filter_array_handler(zval *input, zval **op, zval *return_value, zend_bool add_empty TSRMLS_DC) /* {{{ */
{
char *arg_key;
uint arg_key_len;
@@ -724,7 +726,9 @@ static void php_filter_array_handler(zval *input, zval **op, zval *return_value
RETURN_FALSE;
}
if (zend_hash_find(Z_ARRVAL_P(input), arg_key, arg_key_len, (void **)&tmp) != SUCCESS) {
- add_assoc_null_ex(return_value, arg_key, arg_key_len);
+ if (add_empty) {
+ add_assoc_null_ex(return_value, arg_key, arg_key_len);
+ }
} else {
zval *nval;
@@ -821,15 +825,16 @@ PHP_FUNCTION(filter_var)
}
/* }}} */
-/* {{{ proto mixed filter_input_array(constant type, [, mixed options]])
+/* {{{ proto mixed filter_input_array(constant type, [, mixed options [, bool add_empty]]])
* Returns an array with all arguments defined in 'definition'.
*/
PHP_FUNCTION(filter_input_array)
{
long fetch_from;
zval *array_input = NULL, **op = NULL;
+ zend_bool add_empty = 1;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|Z", &fetch_from, &op) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|Zb", &fetch_from, &op, &add_empty) == FAILURE) {
return;
}
@@ -865,18 +870,19 @@ PHP_FUNCTION(filter_input_array)
}
}
- php_filter_array_handler(array_input, op, return_value TSRMLS_CC);
+ php_filter_array_handler(array_input, op, return_value, add_empty TSRMLS_CC);
}
/* }}} */
-/* {{{ proto mixed filter_var_array(array data, [, mixed options]])
+/* {{{ proto mixed filter_var_array(array data, [, mixed options [, bool add_empty]]])
* Returns an array with all arguments defined in 'definition'.
*/
PHP_FUNCTION(filter_var_array)
{
zval *array_input = NULL, **op = NULL;
+ zend_bool add_empty = 1;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|Z", &array_input, &op) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|Zb", &array_input, &op, &add_empty) == FAILURE) {
return;
}
@@ -887,7 +893,7 @@ PHP_FUNCTION(filter_var_array)
RETURN_FALSE;
}
- php_filter_array_handler(array_input, op, return_value TSRMLS_CC);
+ php_filter_array_handler(array_input, op, return_value, add_empty TSRMLS_CC);
}
/* }}} */
diff --git a/ext/filter/tests/054.phpt b/ext/filter/tests/054.phpt
new file mode 100644
index 0000000000..c8a5dad755
--- /dev/null
+++ b/ext/filter/tests/054.phpt
@@ -0,0 +1,26 @@
+--TEST--
+filter_var_array() - using the add_empty option
+--SKIPIF--
+<?php if (!extension_loaded("filter")) die("skip"); ?>
+--FILE--
+<?php
+
+$data = array('foo' => 123);
+
+var_dump(
+ filter_var_array($data, array('foo' => array('filter' => FILTER_DEFAULT), 'bar' => array('filter' => FILTER_DEFAULT)), false),
+ filter_var_array($data, array('foo' => array('filter' => FILTER_DEFAULT), 'bar' => array('filter' => FILTER_DEFAULT)))
+);
+
+?>
+--EXPECT--
+array(1) {
+ ["foo"]=>
+ string(3) "123"
+}
+array(2) {
+ ["foo"]=>
+ string(3) "123"
+ ["bar"]=>
+ NULL
+}