我有请求类:
class UpdateProductRequest extends ApiRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required',
'description' => 'required',
];
}
}
但我想要类似的东西:
class UpdateProductRequest extends ApiRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name'=> [
'name' => 'Product name',
'description' => 'Product name description',
'validation' => 'required',
'type' => 'string',
],
'description'=> [
'name' => 'Product description',
'description' => 'Product description description',
'validation' => 'required',
'type' => 'string',
]
];
}
}
因为我想将这些附加数据用于Rest API文档。我不想复制像这样的名字:
class UpdateProductRequest extends ApiRequest
{
public function authorize()
{
return true;
}
public function apiData(){
return [
'name'=> [
'name' => 'Product name',
'description' => 'Product name description',
'type' => 'string',
],
'description'=> [
'name' => 'Product description',
'description' => 'Product description description',
'type' => 'string',
]
];
}
public function rules()
{
return [
'name' => 'required',
'description' => 'required',
];
}
}
答案 0 :(得分:2)
您可以rules
使用apiData
,如下所示:
class UpdateProductRequest extends ApiRequest
{
public function authorize()
{
return true;
}
public function apiData()
{
return [
'name'=> [
'name' => 'Product name',
'description' => 'Product name description',
'validation' => 'required',
'type' => 'string',
],
'description'=> [
'name' => 'Product description',
'description' => 'Product description description',
'validation' => 'required',
'type' => 'string',
]
];
}
public function rules()
{
return array_map(function($item){
return $item['validation'];
}, $this->apiData());
}
}
但是,查看Swagger文档可能是个好主意。 larave的绑定很少:https://packagist.org/search/?q=swagger%20laravel
修改强>
rules
是apiData
的一种适配器,用于保存数据。因此,对于未提及的问题,您需要将您的逻辑置于apiData
而不更改rules
中的逻辑:
public function apiData()
{
return array_diff_key(
(new Requests\Backend\UpdateProductRequest)->apiData(),
['something' => false]
);
}
public function rules()
{
return array_map(function($item){
return $item['validation'];
}, $this->apiData());
}