我想为对象设置许多JSON数据,但我不想写很多设置方法。例:
//(POST : JSON DATA) JSON keys equals entity variable names
{
"data1": "banana",
"data2": "index, follow",
"data3": "true",
"data4": "abcdef",
"data5": "false",
"big_data1": "lorem ipsum ...."
"name": "apple"
....
}
我的实体(简短版)
class Category
{
//....
private $data1;
public setData1(string $data1): self{
$this->data1 = $data1;
return $this;
}
public getData1():string{ return $this->data1; }
//...
}
我的控制器
//...
public function index(){
//post data json to object
$data = json_decode($postdata); # data object
$category = new Category(); # new category class
$object = new \ReflectionObject($category);
/**
What is the next step?
$this->setData1($data->data1);
$this->setData2($data->data2);
if(!empty($data->big_data12)
$this->setBigData12($data->big_data12)
.
.
.
$this->setData12($data->data12)
*/
}
如何做一个简单的方法?如何避免编写许多集合方法?
UYOU