/** * @file * @brief manage the http input (get , post, request) */ /** * @file * @brief manage the http input (get , post, request) */ /** * @class HttpInput * @brief manage the http input (get , post, request) and extract from an array */ class HttpInput { private $array; private $empty; //!< if empty that replace by $empty function _construct() { $this->array=null; $this->empty=""; } public function __toString(): string { return "http_input".var_export($this,true); } public function get_array() { return $this->array; } public function get_empty() { return $this->empty; } public function set_array($array) { $this->array=$array; return $this; } /*! * \brief $empty replace the empty value * */ public function set_empty($empty) { $this->empty=$empty; return $this; } /** * \brief Check the type of the value * @param $p_name name of the variable * @param $p_type (string) type of the variable (number,string,text,date,array,raw) * @throws Exception if the variable doesn't exist or type incorrect * @todo Add regex:pattern */ function check_type($p_name, $p_type) { try { // no check on string if ($p_type=="string" || $p_type=="text") { return; } // Check if number else if ($p_type=="number") { if (trim($this->array[$p_name]) == "") { $this->array[$p_name]=$this->empty; } if ( isNumber($this->array[$p_name])==0 ) { throw new Exception(_("Type invalide")."[ $p_name ] = {$this->array[$p_name]}" , EXC_PARAM_TYPE); } $this->array[$p_name]=h($this->array[$p_name]); } // Check if date dd.mm.yyyy else if ($p_type=="date") { if (trim($this->array[$p_name]) == "" ) { $this->array[$p_name]=$this->empty; } if (isDate($this->array[$p_name]) <> $this->array[$p_name]) { throw new Exception(_("Type invalide")."[ $p_name ] = {$this->array[$p_name]}" , EXC_PARAM_TYPE); } $this->array[$p_name]=h($this->array[$p_name]); } else if ($p_type=="raw") { /* NoOperation*/ } else if ($p_type=="array") { if ( empty($this->array[$p_name]) ) { $this->array[$p_name]=$this->empty; } if (!is_array($this->array[$p_name]) ) { throw new Exception(_("Type invalide")."[ $p_name ] = {$this->array[$p_name]}" , EXC_PARAM_TYPE); } if (is_string($this->array )) { $this->array[$p_name]=h($this->array[$p_name]); } }else { throw new Exception(_("Unknown type")); } } catch (Exception $ex) { throw $ex; } } /** * @brief Retrieve from $this->array the variable * @param $p_name name of the variable * @param $p_type type of the variable (number,string or text,date('dd.mm.yyyy'),array) * @param $p_default default value is variable * @throws Exception if invalid * @see check_type */ function get_value($p_name, $p_type="string", $p_default="") { try { if (func_num_args()==3) { if (array_key_exists($p_name,$this->array) ) { $this->check_type($p_name, $p_type); if ($p_type != 'raw' && is_string($this->array[$p_name]) ) { return preg_replace("/array[$p_name]); }elseif ($p_type == 'raw') { $a=preg_replace("/