note 118741 added to filter.filters.misc
| From: | Vufi | Date: | Thu, 28 Jan 2016 19:50:45 +0000 |
| Subject: | note 118741 added to filter.filters.misc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204794@lists.php.net to get a copy of this message | ||
Are you also struggling with how to send arguments into the FILTER_CALLBACK.
Lets say you like to have a collection of custom filters that you can use in filter_var,
filter_var_array and so on, but you want be able to call the new filters with different arguments,
as you can in filter_var.
The easiest way to do this is to make a new class with all your custom filters. Then construct the
class with your arguments inside filter_var argument array and enter the method in your class you
like to run after.
If you send the argument as an array it is easy to make default values. But you can pass as many
arguments you want and in any type, with this solution.
Example:
<?php
/**
* Class CustomFilters
* Custom filters for filter_var, filter_var_array, filter_input, filter_input_array
*/
class CustomFilters {
private $options = array();
public function __construct(array $options=array()){
$this->options = $options;
}
/**
* Returns the default options merged with the construction options
* @param array $defaults
* @return array
*/
private function get_options(array $defaults){
return array_merge($defaults, $this->options);
}
/**
* Checks if a value is in a range
* @param mixed $val Value provided by filter_var
* @return mixed
*/
public function FILTER_STEP_RANGE($val){
$options = $this->get_options(
// default options
array(
"min_range" => 1,
"max_range" => 10,
"step" => 1,
"default" => null, // Value to return on fail
"strict" => false, // Check value for correct type
"cast" => false // Cast the value in a certain type
)
);
if(in_array( $val, range($options["min_range"], $options["max_range"],
$options["step"]), $options["strict"])){
// Return default if the value cant be cast as the right type
if( $options["cast"] && !settype($val, $options["cast"])) {
return $options["default"];
}
return $val;
}else{
return $options["default"];
}
}
/**
* Checks if a value is in array
* @param mixed $val Value provided by filter_var
* @return mixed
*/
public function FILTER_ENUM($val){
$options = $this->get_options(
// default options
array(
"values" => array(),
"strict" => false, // Value to return on fail
"default" => null, // Check value for correct type
"cast" => false // Cast the value in a certain type
)
);
if(in_array($val, $options["values"], $options["strict"])){
// Return default if the value cant be cast as the right type
if( $options["cast"] && !settype($val, $options["cast"])){
return $options["default"];
}
return $val;
}else{
return $options["default"];
}
}
}
$data = array(
"range1" => "200",
"range2" => 25,
"enum" => "yes"
);
$args = array(
"range1" => array(
"filter" => FILTER_CALLBACK,
"options" => array(
new CustomFilters(array( "min_range" => 10, "max_range" => 600,
"step" => 10, "default" => 120,
"cast" => "integer")),
'FILTER_STEP_RANGE'
)
),
"range2" => array(
"filter" => FILTER_CALLBACK,
"options" => array(
new CustomFilters(array( "min_range" => 0, "max_range" => 1,
"step" => .1, "default" => .5,
"cast" => "float")),
'FILTER_STEP_RANGE'
)
),
"enum" => array(
"filter" => FILTER_CALLBACK,
"options" => array(
new CustomFilters(array( "values" => array("yes", "no",
"Yes","No"), "cast" => "string")),
'FILTER_ENUM'
)
)
);
var_dump( filter_var_array($data, $args) );
?>
Returns:
array(3) {
["range1"] => int(200)
["range2"] => float(0.5)
["enum"] => string(3) "yes"
}
----
Server IP: 72.52.91.14
Probable Submitter: 87.104.65.99
----
Manual Page -- http://php.net/manual/en/filter.filters.misc.php
Edit -- https://master.php.net/note/edit/118741
Del: integrated -- https://master.php.net/note/delete/118741/integrated
Del: useless -- https://master.php.net/note/delete/118741/useless
Del: bad code -- https://master.php.net/note/delete/118741/bad+code
Del: spam -- https://master.php.net/note/delete/118741/spam
Del: non-english -- https://master.php.net/note/delete/118741/non-english
Del: in docs -- https://master.php.net/note/delete/118741/in+docs
Del: other reasons-- https://master.php.net/note/delete/118741
Reject -- https://master.php.net/note/reject/118741
Search -- https://master.php.net/manage/user-notes.php