@@ -52,7 +52,7 @@ protected function updateModelFromRequestUsingId(Request $request, $id)
52
52
$ model = $ repository ->getOne ($ id , $ config ->getApi ()->getFixedFiltering ());
53
53
54
54
// check if model is loaded
55
- if (null === $ model ) {
55
+ if (null === $ model ) {
56
56
throw new NotFoundHttpException ();
57
57
}
58
58
@@ -63,22 +63,14 @@ protected function updateModelFromRequestUsingId(Request $request, $id)
63
63
}
64
64
65
65
/**
66
- * @param Request $request
66
+ * @param ConfigInterface $config
67
67
* @param $model
68
+ * @param Request $request
68
69
* @return object
69
70
* @throws ModelValidationException
70
71
*/
71
- protected function updateModelFromRequestUsingModel (Request $ request , $ model ) {
72
- /** @var ConfigInterface $config */
73
- $ config = $ this ->getJsonApiConfig ();
74
-
75
- //TODO make sure request has data in array
76
-
77
- /** @var RepositoryInterface $repository */
78
- $ repository = $ config ->getApi ()->getRepository ();
79
-
80
- // TODO - check if model correct class?
81
-
72
+ protected function handleUpdateModelInputFromRequest (ConfigInterface $ config , $ model , Request $ request )
73
+ {
82
74
// merge it with request data
83
75
if (true === method_exists ($ this , 'getUpdateInputHandler ' )) {
84
76
// TODO - add check if callable, and give info it should be protected
@@ -93,10 +85,11 @@ protected function updateModelFromRequestUsingModel(Request $request, $model) {
93
85
94
86
// update model input with files from request
95
87
$ modelInput = $ request ->request ->all ();
96
- if ($ request ->files ->count () > 0 ) {
88
+ if ($ request ->files ->count () > 0 ) {
97
89
foreach ($ request ->files ->all () as $ filesKey => $ filesValue ) {
98
- if (array_key_exists ($ filesKey , $ modelInput )) {
99
- throw new RuntimeException (sprintf ('Conflict with request files, duplicate param found in request and files %s ' , $ filesKey ));
90
+ if (array_key_exists ($ filesKey , $ modelInput )) {
91
+ throw new RuntimeException (sprintf ('Conflict with request files, duplicate param found in request and files %s ' ,
92
+ $ filesKey ));
100
93
}
101
94
$ modelInput [$ filesKey ] = $ filesValue ;
102
95
}
@@ -105,12 +98,23 @@ protected function updateModelFromRequestUsingModel(Request $request, $model) {
105
98
$ model = $ handler ->forModel ($ model )->handle ($ modelInput )->getResult ();
106
99
107
100
// validate result
108
- if ($ handler instanceof ModelValidatorInterface) {
101
+ if ($ handler instanceof ModelValidatorInterface) {
109
102
$ validated = $ handler ->validate ();
110
103
if (true !== $ validated ) {
111
104
throw new ModelValidationException ($ validated );
112
105
}
113
106
}
107
+
108
+ return $ model ;
109
+ }
110
+
111
+ /**
112
+ * @param ConfigInterface $config
113
+ * @param $model
114
+ * @throws ModelValidationException
115
+ */
116
+ protected function validateUpdatedModel (ConfigInterface $ config , $ model )
117
+ {
114
118
if (true === method_exists ($ this , 'getUpdateValidator ' )) {
115
119
// TODO - add check if callable, and give info it should be protected
116
120
/** @var ModelValidatorInterface $validator */
@@ -126,6 +130,29 @@ protected function updateModelFromRequestUsingModel(Request $request, $model) {
126
130
if (true !== $ validated ) {
127
131
throw new ModelValidationException ($ validated );
128
132
}
133
+ }
134
+
135
+ /**
136
+ * @param Request $request
137
+ * @param $model
138
+ * @return object
139
+ * @throws ModelValidationException
140
+ */
141
+ protected function updateModelFromRequestUsingModel (Request $ request , $ model )
142
+ {
143
+ /** @var ConfigInterface $config */
144
+ $ config = $ this ->getJsonApiConfig ();
145
+
146
+ //TODO make sure request has data in array
147
+
148
+ // TODO - check if model correct class?
149
+
150
+ $ model = $ this ->handleUpdateModelInputFromRequest ($ config , $ model , $ request );
151
+
152
+ $ this ->validateUpdatedModel ($ config , $ model );
153
+
154
+ /** @var RepositoryInterface $repository */
155
+ $ repository = $ config ->getApi ()->getRepository ();
129
156
130
157
// save it
131
158
$ repository ->save ($ model );
0 commit comments