1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mascame\Artificer\Requests; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
7
|
|
|
use Mascame\Artificer\Artificer; |
8
|
|
|
use Mascame\Artificer\Model\ModelManager; |
9
|
|
|
|
10
|
|
|
class ArtificerFormRequest extends FormRequest |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ModelManager |
14
|
|
|
*/ |
15
|
|
|
protected $modelManager; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Model |
19
|
|
|
*/ |
20
|
|
|
protected $model; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $isUpdating; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Determine if the user is authorized to make this request. |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function authorize() |
33
|
|
|
{ |
34
|
|
|
if ($this->isUpdating) { |
35
|
|
|
$this->model = $this->modelManager->model->findOrFail($this->route('id')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// Todo |
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Init the needed properties |
44
|
|
|
*/ |
45
|
|
|
protected function init() { |
46
|
|
|
$this->modelManager = Artificer::getModelManager(); |
47
|
|
|
$this->model = $this->modelManager->model; |
48
|
|
|
$this->isUpdating = (bool)($this->route('id')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function getValidatorInstance() |
52
|
|
|
{ |
53
|
|
|
$this->init(); |
54
|
|
|
|
55
|
|
|
return parent::getValidatorInstance(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the validation rules that apply to the request. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function rules() |
64
|
|
|
{ |
65
|
|
|
return $this->modelManager->getOption('rules', []); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Apply the rules given for the model |
70
|
|
|
*/ |
71
|
|
|
protected function applyMassAssignmentRules() { |
72
|
|
|
$this->model->guard($this->modelManager->getGuarded()); |
73
|
|
|
$this->model->fillable($this->modelManager->getFillable()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Persist the data |
78
|
|
|
*/ |
79
|
|
|
public function persist() { |
80
|
|
|
$data = $this->getData(); |
81
|
|
|
|
82
|
|
|
if ($this->isUpdating) { |
83
|
|
|
return $this->model->update($data); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->model->create($data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
protected function getData() |
93
|
|
|
{ |
94
|
|
|
$this->applyMassAssignmentRules(); |
95
|
|
|
|
96
|
|
|
return $this->all(); |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
|
|
// if ($this->modelManager->hasGuarded()) { |
101
|
|
|
// $filteredInput = []; |
102
|
|
|
// |
103
|
|
|
// foreach ($data as $key => $value) { |
104
|
|
|
// if (in_array($key, $this->modelObject->columns)) { |
105
|
|
|
// $filteredInput[$key] = $value; |
106
|
|
|
// } |
107
|
|
|
// } |
108
|
|
|
// |
109
|
|
|
// return $this->except($this->modelManager->getGuarded(), $filteredInput); |
110
|
|
|
// } |
111
|
|
|
|
112
|
|
|
// Todo |
113
|
|
|
// $data = $this->handleFiles($data); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|