As a phper, I just found out today that from joining OSC to now, the only code I have shared is php. I felt red and shared a common class I wrote in the project.
GET data/POST data /Ordinary array returns formatted data through key http://www.du52.com/text.php?id=581
- include_once('Param.class.php');
- // Simulate setting GET/POST data
- $_REQUEST['int'] = '1243';
- $_REQUEST['str'] = 'hello';
- $_REQUEST['bool'] = 'true';
- $_REQUEST['arr'] = '1,2,3,4';
- $_REQUEST['json'] = json_encode(array( 'a' => 'a', 'b' => 'b'));
- $_REQUEST['date'] = date('Y-m-d H:i:s');
- // Single data reading
- var_dump(Param::getInt('int', 0));echo '
';
- var_dump(Param::getInt('undefined-int', -1));echo '
';
- var_dump(Param::getStr('str', 'default'));echo '
';
- var_dump(Param::getBool('bool', NULL));echo '
';
- var_dump(Param::getStrArray('arr', ',', NULL));echo '
';
- var_dump(Param::getJson('json' , NULL));echo '
';
- var_dump(Param::getTime('date', -1));echo '
';
- echo '
';
- // Get batches of
- $fields = array();
- $fields[] = array('int', Param::$FIELD_TYPE_INT, 0);
- $fields [] = array('undefined-int', Param::$FIELD_TYPE_INT, -1);
- $fields[] = array('str', Param::$FIELD_TYPE_STR, 'default');
- $fields[] = array('bool', Param::$FIELD_TYPE_BOOL, 0);
- $fields[] = array('arr', Param::$FIELD_TYPE_STRARR, 0);
- $fields[] = array('json', Param: :$FIELD_TYPE_JSON, 0);
- $fields[] = array('date', Param::$FIELD_TYPE_TIME, -1);
- $data = Param::parse($fields);
- var_dump($data);
- echo '
';echo ' ';
- // Get array data
- $source = array('int' => '1234', 'str' => 'hello ');
- var_dump(Param::getInt('int', -1));echo '
';
- var_dump(Param::getStr('str', 'default'));echo '
';
- ?>
Copy code
- int(1243)
- int(-1)
- string(5) "hello"
- bool(true)
- array(4) { [0]=> string(1) "1" [1]= > string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" }
- array(2) { ["a"]=> string(1) "a" ["b"]=> string(1) "b" }
- int(1408603747)
-
-
- array(7) { ["int"]=> int(1243) [" undefined-int"]=> int(-1) ["str"]=> string(5) "hello" ["bool"]=> bool(true) ["arr"]=> array( 4) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string( 1) "4" } ["json"]=> array(2) { ["a"]=> string(1) "a" ["b"]=> string(1) "b" } ["date"]=> int(1408603747) }
-
- int(1243)
- string(5) "hello"
Copy code
- /**
- * Parameter management
- *
- * @author wangaibo168@163.com
- * @charset utf-8
- */
- class Param {
- /**
- *Default constructor
- */
- private function __construct(){}
- /**
- * Get raw data
- * @param $name
- * @param null $def
- * @param null $arr
- * @return null
- */
- public static function getData($name,$def=null,$arr=null){
- if(is_null($name) || $name==='') return $def;
- $name = trim($name);
- $temp = is_array($arr)?$arr:$_REQUEST;
- if(array_key_exists($name,$temp)) return $temp[$name];
- return $def;
- }
- /**
- * Get string data
- * @param $name
- * @param string $def
- * @param null $arr
- * @return string
- */
- public static function getStr($name,$def='',$arr=null){
- $value = self::getData($name,$def,$arr);
- return @strval($value);
- }
- /**
- * Get numerical data
- * @param $name
- * @param int $def
- * @param null $arr
- * @return int
- */
- public static function getInt($name,$def=0,$arr=null){
- $value = self::getData($name,$def,$arr);
- return @intval($value);
- }
- /**
- * Get Boolean type data
- * @param $name
- * @param bool $def
- * @param null $arr
- * @return bool
- */
- public static function getBool($name,$def=false,$arr=null){
- $value = self::getData($name,$def,$arr);
- if(is_string($value)){
- $value = strtolower($value);
- if($value=='true' || $value=='1') return true;
- if($value=='false' || $value=='0') return false;
- }
- if(is_int($value)){
- if($value==1) return true;
- if($value==0) return false;
- }
- if(is_object($value)){
- return $value!=null;
- }
- return $def;
- }
- /**
- * Get array type data
- * @param $name
- * @param array $def
- * @param null $arr
- * @return array
- */
- public static function getArray($name,$def=array(),$arr=null){
- $value = self::getData($name,$def,$arr);
- if(!is_array($value)){
- $value = array($value);
- }
- return $value;
- }
- /**
- * Get JSON type data
- * @param $name
- * @param array $def
- * @param null $arr
- * @return array
- */
- public static function getJson($name,$def=array(),$arr=null){
- $value = self::getStr($name,null,$arr);
- if($value==null) return $def;
- $value = @json_decode($value,true);
- if(is_array($value) && count($value)>0) return $value;
- return $def;
- }
-
- /**
- * The time format is 2013-12-02 11:00:11, converted to timestamp
- * @param $name
- * @param int $def
- * @param null $arr
- * @return int
- */
- public static function getTime($name,$def=0,$arr=null){
- $value = self::getStr($name,'',$arr);
- if(empty($value)) return $def;
- $value = trim($value);
- if(preg_match('/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})s+([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})$/',$value,$ret)){
- if(is_array($ret) && count($ret)==7){
- list($t,$y,$m,$d,$h,$mi,$s) = $ret;
- return mktime($h,$mi,$s,$m,$d,$y);
- }else{
- return $def;
- }
- }
- if(preg_match('/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/',$value,$ret)){
- if(is_array($ret) && count($ret)==4){
- list($t,$y,$m,$d) = $ret;
- return mktime(0,0,0,$m,$d,$y);
- }else{
- return $def;
- }
- }
- return $def;
- }
-
- /**
- * The time format is 2013-12-02 11:00:11, converted to a number (14 digits)
- * @param $name
- * @param string $def
- * @param null $arr
- * @return bool|string
- */
- public static function getDate($name,$def='00000000000000',$arr=null){
- $value = self::getTime($name,0,$arr);
- if($value>0){
- return date('YmdHis',$value);
- }
- return $def;
- }
-
- /**
- * Format string date as standard date
- * @param $value
- * @param bool $full
- * @param string $def
- * @return string
- */
- public static function formatDate($value,$full=false,$def=''){
- if(empty($value)) return $def;
- $value = trim($value);
- if(preg_match('/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/',$value,$ret)){
- if(is_array($ret) && count($ret)==7){
- list($t,$y,$m,$d,$h,$mi,$s) = $ret;
- if($y==0 || $m==0 || $d==0) return $def;
- if(!$full && $h==0 && $mi==0 && $s==0){
- return "$y-$m-$d";
- }
- return "$y-$m-$d $h:$mi:$s";
- }else{
- return $def;
- }
- }
- }
-
- /**
- * Get floating point data
- * @param $name
- * @param int $def
- * @param null $arr
- * @return float
- */
- public static function getDouble($name,$def=0,$arr=null){
- $value = self::getData($name,$def,$arr);
- return @doubleval($value);
- }
-
- /**
- * Get and convert string to array
- * @param $name
- * @param string $limit
- * @param array $def
- * @param null $arr
- * @return array
- */
- public static function getStrArray($name,$limit=',',$def=array(),$arr=null){
- $value = self::getStr($name,'',$arr);
- if(empty($value)) return $def;
- $arr = explode($limit,$value);
- if(!is_array($arr)) return $def;
- $value = array();
- foreach($arr as $v){
- if(empty($v)) continue;
- $value[] = $v;
- }
- return $value;
- }
-
- /**
- * Set native data
- * @param $name
- * @param $value
- */
- public static function setData($name,$value){
- if(empty($name)) return;
- $_GET[$name] = $value;
- $_POST[$name] = $value;
- $_REQUEST[$name] = $value;
- }
-
- /**
- * Set new native data based on old data
- * @param $name
- * @param $oldName
- */
- public static function setDataByName($name,$oldName){
- if(empty($name) || empty($oldName)) return;
- $value = self::getData($oldName);
- self::setData($name,$value);
- }
-
- /**
- * @var string native data type
- */
- public static $FIELD_TYPE_DATA = 'data';
-
- /**
- * @var string numerical data type
- */
- public static $FIELD_TYPE_INT = 'int';
-
- /**
- * @var string string data type
- */
- public static $FIELD_TYPE_STR = 'str';
-
- /**
- * @var string floating point data type
- */
- public static $FIELD_TYPE_DOUBLE = 'double';
-
- /**
- * @var string Boolean data type
- */
- public static $FIELD_TYPE_BOOL = 'bool';
-
- /**
- * @var string JSON data type
- */
- public static $FIELD_TYPE_JSON = 'json';
-
- /**
- * @var string array data type
- */
- public static $FIELD_TYPE_ARRAY = 'array';
-
- /**
- * @var string timestamp data type
- */
- public static $FIELD_TYPE_TIME = 'time';
-
- /**
- * @var string string time data type
- */
- public static $FIELD_TYPE_DATE = 'date';
-
- /**
- * @var string string array data type
- */
- public static $FIELD_TYPE_STRARR = 'strarr';
-
- /**
- * Format data according to data fields
- * @param $fields
- * @return array
- */
- public static function parse($fields){
- if(!is_array($fields) || count($fields)==0) return array();
- $data = array();
- foreach($fields as $field){
- if(!is_array($field) || count($field)!=3) continue;
- list($name,$type,$def) = $field;
- if(empty($name) || empty($type)) continue;
- $type = strtolower($type);
- if($type==self::$FIELD_TYPE_DATA){
- $value = self::getData($name,$def);
- }else if($type==self::$FIELD_TYPE_INT){
- $value = self::getInt($name,$def);
- }else if($type==self::$FIELD_TYPE_STR){
- $value = self::getStr($name,$def);
- }else if($type==self::$FIELD_TYPE_DOUBLE){
- $value = self::getDouble($name,$def);
- }else if($type==self::$FIELD_TYPE_BOOL){
- $value = self::getBool($name,$def);
- }else if($type==self::$FIELD_TYPE_ARRAY){
- $value = self::getArray($name,$def);
- }else if($type==self::$FIELD_TYPE_TIME){
- $value = self::getTime($name,$def);
- }else if($type==self::$FIELD_TYPE_DATE){
- $value = self::getDate($name,$def);
- }else if($type==self::$FIELD_TYPE_JSON){
- $value = self::getJson($name,$def);
- }else if($type==self::$FIELD_TYPE_STRARR){
- $value = self::getStrArray($name,',',$def);
- }else{
- $value = $def;
- }
- $data[$name] = $value;
- }
- return $data;
- }
-
- /**
- * Format JSON data
- * @param $name
- * @param $fields
- * @param null $arr
- * @return array
- */
- public static function parseJSON($name,$fields,$arr=null){
- if(!is_array($fields) || count($fields)==0) return array();
- $data = array();
- $temp = self::getJson($name,null,$arr);
- if(!is_array($temp)) $temp = array();
- foreach($fields as $field){
- if(!is_array($field) || count($field)!=3) continue;
- list($name,$type,$def) = $field;
- if(empty($name) || empty($type)) continue;
- $type = strtolower($type);
- if($type==self::$FIELD_TYPE_DATA){
- $value = self::getData($name,$def,$temp);
- }else if($type==self::$FIELD_TYPE_INT){
- $value = self::getInt($name,$def,$temp);
- }else if($type==self::$FIELD_TYPE_STR){
- $value = self::getStr($name,$def,$temp);
- }else if($type==self::$FIELD_TYPE_DOUBLE){
- $value = self::getDouble($name,$def,$temp);
- }else if($type==self::$FIELD_TYPE_BOOL){
- $value = self::getBool($name,$def,$temp);
- }else if($type==self::$FIELD_TYPE_ARRAY){
- $value = self::getArray($name,$def,$temp);
- }else if($type==self::$FIELD_TYPE_TIME){
- $value = self::getTime($name,$def,$temp);
- }else if($type==self::$FIELD_TYPE_DATE){
- $value = self::getDate($name,$def,$temp);
- }else if($type==self::$FIELD_TYPE_JSON){
- $value = self::getJson($name,$def,$temp);
- }else if($type==self::$FIELD_TYPE_STRARR){
- $value = self::getStrArray($name,',',$def,$temp);
- }else{
- $value = $def;
- }
- $data[$name] = $value;
- }
- return $data;
- }
- }
- ?>
复制代码
|