class Request{
private static $method_type=array(‘get‘,‘post‘,‘put‘,‘delete‘,‘patch‘);
private static $FruitList=array(
1=>array(‘name‘=>‘apple‘,‘num‘=>10),
2=>array(‘name‘=>‘grape‘,‘num‘=>20),
3=>array(‘name‘=>‘orange‘,‘num‘=>40),
4=>array(‘name‘=>‘banana‘,‘num‘=>30),
5=>array(‘name‘=>‘peach‘,‘num‘=>50),
6=>array(‘name‘=>‘mango‘,‘num‘=>60)
);
public static function getRequest(){
$method=strtolower($_SERVER[‘REQUEST_METHOD‘]);
if(in_array($method,self::$method_type)){
$methodName=$method.‘Data‘;
return self::$methodName($_REQUEST);
}
return false;
}
//get 查询
private static function getData($params){
$fruitId=(int)$params[‘fruit‘];
if($fruitId>0){
return self::$FruitList[$fruitId];
}else{
return self::$FruitList;
}
}
//post 添加
private static function postData($params){
if(!empty($params[‘name‘])){
$data[‘name‘]=$params[‘name‘];
$data[‘num‘]=(int)$params[‘num‘];
self::$FruitList[]=$data;
return self::$FruitList;
}else{
return false;
}
}
//put 更新水果信息(全部信息)
private static function putData($params){
$fruitId=(int)$params[‘fruit‘];
if($fruitId==0){
return false;
}
$data=array();
if(!empty($params[‘name‘] && isset($params[‘num‘]))){
$data[‘name‘]=$params[‘name‘];
$data[‘num‘]=(int)$params[‘num‘];
self::$FruitList[‘fruitId‘]=$data;
return self::$FruitList;
}else{
return false;
}
}
//patch 更新水果部分信息
private static function patchData($params){
$fruitId=(int)$params[‘fruit‘];
if($fruitId==0){
return false;
}
if(!empty($params[‘name‘])){
self::$FruitList[$fruitId][‘name‘]=$params[‘name‘];
}
if(!isset($params[‘num‘])){
self::$FruitList[$fruitId][‘num‘]=(int)$params[‘num‘];
}
return self::$FruitList;
}
//delete 删除
private static function deleteData($params)
{
$fruitId = (int)$params[‘fruit‘];
if($fruitId==0){
return false;
}
unset(self::$FruitList[$fruitId]);
return self::$FruitList;
}
}
?>