Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 1st, 2012  |  syntax: None  |  size: 2.78 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PHP input filtering for complex arrays
  2. $data = array(
  3.     'testarray'    => array('2', '23', '10', '12')
  4. );
  5.  
  6. $args = array(
  7.     'testarray'    => array('filter'    => FILTER_VALIDATE_INT,
  8.                             'flags'     => FILTER_FORCE_ARRAY
  9.                            )    
  10. );
  11.  
  12. $myinputs = filter_var_array($data, $args);
  13.        
  14. $data = array(
  15.     'testhash'    => array('level1'=>'email',
  16.                            'level2'=> array('23', '10', '12'))
  17. );
  18.        
  19. function validate_array($args) {
  20.     return function ($data) use ($args) {
  21.         return filter_input_array($data, $args);
  22.     };
  23. }
  24.        
  25. $args = array(
  26.     'user' => array(
  27.         'filter' => FILTER_CALLBACK,
  28.         'options' => validate_array(array(
  29.             'age' => array('filter' => FILTER_INPUT_INT),
  30.             'email' => array('filter' => FILTER_INPUT_EMAIL)
  31.         ))
  32.     )
  33. );
  34.        
  35. $args = array(
  36.     'user/age' => array('filter' => FILTER_INPUT_INT),
  37.     'user/email' => array('filter' => FILTER_INPUT_EMAIL),
  38.     'user/parent/age' => array('filter' => FILTER_INPUT_INT),
  39.     'foo' => array('filter' => FILTER_INPUT_INT)
  40. );
  41.        
  42. $data = array(
  43.     'user' => array(
  44.         'age' => 15,
  45.         'email' => 'foo@gmail.com',
  46.         'parent' => array(
  47.             'age' => 38
  48.         )
  49.     ),
  50.     'foo' => 5
  51. );
  52.        
  53. function my_filter_array($data, $args) {
  54.     $ref_map = array();
  55.     foreach ($args as $key => $a) {
  56.         $parts = explode('/', $key);
  57.         $ref =& $data;
  58.         foreach ($parts as $p) $ref =& $ref[$p];
  59.         $ref_map[$key] =& $ref;
  60.     }
  61.     return filter_var_array($ref_map, $args);
  62. }
  63.  
  64. var_dump(my_filter_array($data, $args));
  65.        
  66. /** Like filter_var_array but recursive, this way you can nest your filters.
  67.  * Small caveat : if an array in filters contains the keys 'filter' and
  68.  * 'options' it will be considered as a filter rule.
  69.  * param $data (mixed) : data to filter.
  70.  * param $filters (mixed) : array of filters or filter.
  71.  * return filtered data. Unknown fields will be deleted.
  72.  * */
  73. function filter_var_recursive($data, $filters) {
  74.     // Just a rule, don't recurse.
  75.     if(!is_array($filters))
  76.         return filter_var($data, $filters);
  77.  
  78.     // Only a detailed rule, don't recurse.
  79.     if(array_key_exists('options', $filters) && array_key_exists('filter', $filters)) {
  80.         // Hackish but ok.
  81.         $ret = filter_var_array(array('value' => $data), array('value' => $filters));
  82.         return $ret['value'];
  83.     }
  84.  
  85.     // At this point the data should be an array, if it's not, we don't care.
  86.     if(!is_array($data))
  87.         $data = array();
  88.  
  89.     // Iterate and recurse.
  90.     $ret = array();
  91.     foreach($filters as $key => $filter) {
  92.         if(!array_key_exists($key, $data))
  93.             $ret[$key] = null;
  94.         else
  95.             $ret[$key] = filter_var_recursive($data[$key], $filter);
  96.     }
  97.  
  98.     return $ret;
  99. }