Skip to content

Instantly share code, notes, and snippets.

@gaogao-9
Last active December 13, 2015 14:20
Show Gist options
  • Save gaogao-9/af5049db38edd2cddd57 to your computer and use it in GitHub Desktop.
Save gaogao-9/af5049db38edd2cddd57 to your computer and use it in GitHub Desktop.
<?php
$getURLParam = function($input,$method=''){
if(!is_array($input)) return [];
$info = [];
foreach($input as $key => $val){
unset($_param);
unset($_key);
unset($_type);
unset($_def);
unset($_filter);
if(is_string($val)){
$_param = $val;
$_key = $val;
$_type = 'string';
$_def = '';
$_filter = null;
}
else if(is_array($val)){
$_param = (string)$key;
$_key = (array_key_exists('query',$val) && is_string($val['query'])) ? $val['query'] : $_param;
$_type = (array_key_exists('type',$val) && is_string($val['type'])) ? strtolower($val['type']) : string;
$_filter = (array_key_exists('filter',$val) && is_callable($val['filter'], false)) ? $val['filter'] : null;
if(is_numeric($key)){
if(!is_numeric($_key)) $_param = $_key;
else continue;
}
if(array_key_exists('def',$val)) $_def = $val['def'];
if(!isset($_def)){
if(isset($_type)){
switch($_type){
case 'int':
case 'integer':
$_type = 'int';
$_def = 0;
break;
case 'float':
$_type = 'float';
$_def = (float)0.0;
break;
case 'double':
$_type = 'double';
$_def = (double)0.0;
break;
case 'bool':
case 'boolean':
$_type = 'bool';
$_def = false;
break;
default:
$_type = 'string';
$_def = '';
break;
}
}
else{
$_type = 'string';
$_def = '';
}
}
}
$info[] = [
'param' => $_param,
'key' => $_key,
'type' => $_type,
'def' => $_def,
'filter' => $_filter,
];
}
$methodList = [];
if($method === 'GET'){
$methodList[] = INPUT_GET;
}
else if($method === 'POST'){
$methodList[] = INPUT_POST;
}
else{
$methodList[] = INPUT_POST;
$methodList[] = INPUT_GET;
}
return array_reduce($info,function($arr,$info) use($methodList){
$arr[$info['param']] = array_reduce($methodList,function($value,$method) use ($info){
return ($value==='') ? (string)filter_input($method, $info['key']) : $value;
},'');
if($arr[$info['param']] === ''){
$arr[$info['param']] = $info['def'];
}
else{
switch($info['type']){
case 'int':
$arr[$info['param']] = (int)$arr[$info['param']];
break;
case 'float':
$arr[$info['param']] = (float)$arr[$info['param']];
break;
case 'double':
$arr[$info['param']] = (double)$arr[$info['param']];
break;
case 'bool':
$arr[$info['param']] = (bool)$arr[$info['param']];
break;
}
}
if($info['filter']){
$arr[$info['param']] = $info['filter']($arr[$info['param']]);
}
return $arr;
},[]);
};
<?php
require_once(__DIR__.'/getURLParam.php');
// 文字列としてURLパラメータ「a」「b」「c」を受け取る例(?a=gao&b=18&c=[])
$param = $getURLParam(['a','b','c']);
var_dump($param);
// [
// 'a' => 'gao',
// 'b' => '18', // 数値を渡しても文字列になる
// 'c' => '' // 配列を渡されるなど、文字列として解釈できないものは強制的に空文字になる
// ]
// 整数として「a」「b」「c」「d」「e」を受け取る例(?a=18&b=38.5&e=10000000)
$param = $getURLParam([
'a' => [
'type' => int
],
'b' => [
'type' => int,
'def' => -1
],
'c' => [
'type' => int
],
'd' => [
'type' => int,
'def' => -1
],
'e' => [
'type' => int,
'filter' => function($num){
if($num<0) return 0;
if($num>30) return 30;
return $num;
}
],
]);
var_dump($param);
// [
// 'a' => 18, // aの値を整数値で得る
// 'b' => 38, // bの値を整数値で得る(切り捨て処理される。bに数値が渡されていた際はdefの値を無視する)
// 'c' => 0, // cの値を整数値で得る(数値以外だったり、渡されなかった場合は強制的に0になる)
// 'd' => -1, // dの値を整数値で得る(defが与えられていたら、その値を既定値とする)
// 'e' => 30 // eの値を整数値で得る(filterが与えられていたら、filter関数を介した後の値を得る)
// ]
@gaogao-9
Copy link
Author

第二引数に「'GET'」もしくは「'POST'」を指定することで取得する先を引き絞るようにしました。
例) GET通信のパラメータに絞る例
$param = $getURLParam(['a','b','c'], 'GET');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment