作为一名phper,今天才发现,从加入osc到现在,分享的各种代码,就只有一篇php.顿觉脸红,特分享在项目中自己写的一个常用类.
GET数据/POST数据/普通数组 通过key返回格式化后的数据 http://www.du52.com/text.php?id=581
- include_once('Param.class.php');
- // 模拟设置GET/POST数据
- $_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');
- // 单个数据读取
- 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 '
';echo ' '; - // 批量获取
- $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 ' '; - // 获取数组数据
- $source = array('int' => '1234', 'str' => 'hello');
- var_dump(Param::getInt('int', -1));echo '
'; - var_dump(Param::getStr('str', 'default'));echo '
'; - ?>
- 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"
- /**
- * 参数管理
- *
- * @author wangaibo168@163.com
- * @charset utf-8
- */
- class Param {
- /**
- * 默认构造函数
- */
- private function __construct(){}
- /**
- * 获取原生数据
- * @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;
- }
- /**
- * 获取字符串数据
- * @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);
- }
- /**
- * 获取数值数据
- * @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);
- }
- /**
- * 获取布尔类型数据
- * @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;
- }
- /**
- * 获取数组类型数据
- * @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;
- }
- /**
- * 获取JSON类型数据
- * @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;
- }
- /**
- * 时间格式为2013-12-02 11:00:11,转换为时间戳
- * @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;
- }
- /**
- * 时间格式为2013-12-02 11:00:11,转换为数字(14位)
- * @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;
- }
- /**
- * 格式化字符串日期为标准日期
- * @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;
- }
- }
- }
- /**
- * 获取浮点数据
- * @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);
- }
- /**
- * 获取并将字符串转换为数组
- * @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;
- }
- /**
- * 设置原生数据
- * @param $name
- * @param $value
- */
- public static function setData($name,$value){
- if(empty($name)) return;
- $_GET[$name] = $value;
- $_POST[$name] = $value;
- $_REQUEST[$name] = $value;
- }
- /**
- * 根据旧的数据设置新的原生数据
- * @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 原生数据类型
- */
- public static $FIELD_TYPE_DATA = 'data';
- /**
- * @var string 数值数据类型
- */
- public static $FIELD_TYPE_INT = 'int';
- /**
- * @var string 字符串数据类型
- */
- public static $FIELD_TYPE_STR = 'str';
- /**
- * @var string 浮点数据类型
- */
- public static $FIELD_TYPE_DOUBLE = 'double';
- /**
- * @var string 布尔数据类型
- */
- public static $FIELD_TYPE_BOOL = 'bool';
- /**
- * @var string JSON数据类型
- */
- public static $FIELD_TYPE_JSON = 'json';
- /**
- * @var string 数组数据类型
- */
- public static $FIELD_TYPE_ARRAY = 'array';
- /**
- * @var string 时间戳数据类型
- */
- public static $FIELD_TYPE_TIME = 'time';
- /**
- * @var string 字符串时间数据类型
- */
- public static $FIELD_TYPE_DATE = 'date';
- /**
- * @var string 字符串数组数据类型
- */
- public static $FIELD_TYPE_STRARR = 'strarr';
- /**
- * 根据数据字段格式化数据
- * @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;
- }
- /**
- * 格式化JSON数据
- * @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;
- }
- }
- ?>
|