ext/filter, add input_get_args, support of scalar or array result

php.internals

Pierre Joye

20 years ago
Hello, Here is another patch for ext/filter (I still wait the answer for the other one about decimal separator...). This patchs adds the input_get_args function. This functions allows to fetch all input you like in one call using a definition array. It is also more flexible than input_get as you can mix options and flags for one value. I put a small example here: http://pecl.php.net/~pierre/filter_input_get_args_example.phps and the patch: http://pecl.php.net/~pierre/patch_filter_input_get_args.txt I voluntary duplicate codes from input_get. It behaves slightly differently and I like to keep them separate as long as the patch is not accepted. I also like to change input_get behaviors. We actually have no way to know if a variable does not exists or is invalid. Both return NULL. I like to return false when a variable does not fit the condition and NULL when it does not exist. Is it fine? If yes, I'll provide another patch. Cheers, -- Pierre

Pierre Joye

20 years ago
hello, I forgot the scalar/array part :) I also added FILTER_FLAG_SCALAR and FILTER_FLAG_ARRAY. These flags let you choose if you allow scalar only or array. For example: foo.php?a[]=1&a[]=2 will be invalid if you use the FILTER_FLAG_SCALAR. I like to add this feature to input_get as well. FILTER_FLAG_ARRAY will always returns you an array, even for foo.php?a=1 (array(1)). --Pierre

Pierre Joye

20 years ago
On Mon, 1 May 2006 16:17:54 +0200 pierre.php@gmail.com (Pierre) wrote:
> I also like to change input_get behaviors. We actually have no way to > know if a variable does not exists or is invalid. Both return NULL. > > I like to return false when a variable does not fit the condition and > NULL when it does not exist. Is it fine? If yes, I'll provide another > patch.
The patch is updated to support this behavior in input_get_args. If a variable does not exist, its index is set to NULL, if its value is invalid, it is set to FALSE. It starts to be usefull :-)
-- Pierre Did I help you? Please donate http://blog.thepimp.net/index.php/1998/04/12/57-please-donate

Rasmus Lerdorf

20 years ago
Pierre wrote:
> I put a small example here: > http://pecl.php.net/~pierre/filter_input_get_args_example.phps > > and the patch: > http://pecl.php.net/~pierre/patch_filter_input_get_args.txt
I think this looks ok. I have been trying to come up with a shorter and cleaner syntax to specify these things, but so far I haven't come up with anything I really like. The closest I have come is something like this: $args = array( 'product_id' => 'Enc', 'component' => 'Int:Array:1-10', 'versions' => 'Enc', 'doesnotexist' => 'Int', 'testscalar' => 'Int:Scalar', 'testarray' => 'Int:Array' ); But I am not completely happy with the magic shortcuts in the strings there. The above would be equivalent to: $args = array( 'product_id' => FILTER_SANITIZE_ENCODED, 'component' => array('filter' => FILTER_VALIDATE_INT, 'flags' => FILTER_FLAG_ARRAY, 'options' => array("min_range"=>1, "max_range"=>10) ), 'versions' => FILTER_SANITIZE_ENCODED, 'doesnotexist' => FILTER_VALIDATE_INT, 'testscalar' => array( 'filter' => FILTER_VALIDATE_INT, 'flags' => FILTER_FLAG_SCALAR, ), 'testarray' => array( 'filter' => FILTER_VALIDATE_INT, 'flags' => FILTER_FLAG_ARRAY, ) ); -Rasmus

Pierre Joye

20 years ago
Hi Rasmus, Thanks for the feedback, it is appreciated :-) On 5/2/06, Rasmus Lerdorf <rasmus@lerdorf.com> wrote:
> Pierre wrote: > > I put a small example here: > > http://pecl.php.net/~pierre/filter_input_get_args_example.phps > > > > and the patch: > > http://pecl.php.net/~pierre/patch_filter_input_get_args.txt > > I think this looks ok. I have been trying to come up with a shorter > and cleaner syntax to specify these things, but so far I haven't come > up with anything I really like. The closest I have come is something > like this: > > $args = array( > 'product_id' => 'Enc', > 'component' => 'Int:Array:1-10', > 'versions' => 'Enc', > 'doesnotexist' => 'Int', > 'testscalar' => 'Int:Scalar', > 'testarray' => 'Int:Array' > ); > > But I am not completely happy with the magic shortcuts in the strings > there.
I had the same problem, it is hard to make them shorter and still easy to remember or as fast as constants. One of my idea was to create a filter class, both for the functions and the constants: FILTER::TO_INT,FILTER::TO_ENCODED filter::get() (input_get), filter::data() (filter_data, filter::isset() (filter_has_variable),... We gain a few chars and a bit of visibility. The :: separator makes the real constant easier to read. See an example at the end of this mail: $args = array( 'product_id' => FILTER::TO_ENCODED, 'component' => array('filter' => FILTER::TO_INT, 'flags' => FILTER::ARRAY, 'options' => array("min_range"=>1, "max_range"=>10) ), 'versions' => FILTER::TO_ENCODED, 'doesnotexist' => FILTER::TO_INT, 'testscalar' => array( 'filter' => FILTER::TO_INT, 'flags' => FILTER::SCALAR, ), 'testarray' => array( 'filter' => FILTER::TO_INT, 'flags' =>FILTER::ARRAY, ) ); I will then remove the duplicated code and clean the patch, add some tests and commit. What do you think to change input_get to behave like input_get_args? invalid data uses false and not found NULL. Cheers, -- Pierre

Jared Williams

20 years ago
> -----Original Message----- > From: Rasmus Lerdorf [mailto:rasmus@lerdorf.com] > Sent: 02 May 2006 07:42 > To: pierre.php@gmail.com > Cc: internals@lists.php.net > Subject: Re: [PHP-DEV] ext/filter, add input_get_args, > support of scalar orarray result > > Pierre wrote: > > I put a small example here: > > http://pecl.php.net/~pierre/filter_input_get_args_example.phps > > > > and the patch: > > http://pecl.php.net/~pierre/patch_filter_input_get_args.txt > > I think this looks ok. I have been trying to come up with a > shorter and cleaner syntax to specify these things, but so > far I haven't come up with anything I really like. The > closest I have come is something like > this: > > $args = array( > 'product_id' => 'Enc', > 'component' => 'Int:Array:1-10', > 'versions' => 'Enc', > 'doesnotexist' => 'Int', > 'testscalar' => 'Int:Scalar', > 'testarray' => 'Int:Array' > );
Would a little helper function to create the array be a little neater (and less typing :) for the more common complex options? $args = array( 'component' => somefunc(FILTER_VALIDATE_INT, FILTER_FLAG_ARRAY, 1, 10); );
> > But I am not completely happy with the magic shortcuts in the > strings there. The above would be equivalent to: > > $args = array( > 'product_id' => FILTER_SANITIZE_ENCODED, > 'component' => array('filter' => FILTER_VALIDATE_INT, > 'flags' => FILTER_FLAG_ARRAY, > 'options' => array("min_range"=>1, > "max_range"=>10) > ), > 'versions' => FILTER_SANITIZE_ENCODED, > 'doesnotexist' => FILTER_VALIDATE_INT, > 'testscalar' => array( > 'filter' => FILTER_VALIDATE_INT, > 'flags' => FILTER_FLAG_SCALAR, > > ), > 'testarray' => array( > 'filter' => FILTER_VALIDATE_INT, > 'flags' => FILTER_FLAG_ARRAY, > ) > > );
Jared

Andrei Zmievski

20 years ago
On May 1, 2006, at 11:42 PM, Rasmus Lerdorf wrote:
> I think this looks ok. I have been trying to come up with a shorter > and cleaner syntax to specify these things, but so far I haven't come > up with anything I really like. The closest I have come is something > like this: > > $args = array( > 'product_id' => 'Enc', > 'component' => 'Int:Array:1-10', > 'versions' => 'Enc', > 'doesnotexist' => 'Int', > 'testscalar' => 'Int:Scalar', > 'testarray' => 'Int:Array' > );
You should just take Perl approach and use sigils [1]. :) $args = array( 'product_id' => '%', 'component' => 'i[1-10]', 'versions' => '%', 'doesnotexist' => 'i', 'testscalar' => 'i$', 'testarray' => 'i[]' ); -Andrei [1] http://en.wikipedia.org/wiki/Sigil_%28computer_programming%29