@@ -52,7 +52,7 @@ protected function updateModelFromRequestUsingId(Request $request, $id)
5252 $ model = $ repository ->getOne ($ id , $ config ->getApi ()->getFixedFiltering ());
5353
5454 // check if model is loaded
55- if (null === $ model ) {
55+ if (null === $ model ) {
5656 throw new NotFoundHttpException ();
5757 }
5858
@@ -63,22 +63,14 @@ protected function updateModelFromRequestUsingId(Request $request, $id)
6363 }
6464
6565 /**
66- * @param Request $request
66+ * @param ConfigInterface $config
6767 * @param $model
68+ * @param Request $request
6869 * @return object
6970 * @throws ModelValidationException
7071 */
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+ {
8274 // merge it with request data
8375 if (true === method_exists ($ this , 'getUpdateInputHandler ' )) {
8476 // TODO - add check if callable, and give info it should be protected
@@ -93,10 +85,11 @@ protected function updateModelFromRequestUsingModel(Request $request, $model) {
9385
9486 // update model input with files from request
9587 $ modelInput = $ request ->request ->all ();
96- if ($ request ->files ->count () > 0 ) {
88+ if ($ request ->files ->count () > 0 ) {
9789 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 ));
10093 }
10194 $ modelInput [$ filesKey ] = $ filesValue ;
10295 }
@@ -105,12 +98,23 @@ protected function updateModelFromRequestUsingModel(Request $request, $model) {
10598 $ model = $ handler ->forModel ($ model )->handle ($ modelInput )->getResult ();
10699
107100 // validate result
108- if ($ handler instanceof ModelValidatorInterface) {
101+ if ($ handler instanceof ModelValidatorInterface) {
109102 $ validated = $ handler ->validate ();
110103 if (true !== $ validated ) {
111104 throw new ModelValidationException ($ validated );
112105 }
113106 }
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+ {
114118 if (true === method_exists ($ this , 'getUpdateValidator ' )) {
115119 // TODO - add check if callable, and give info it should be protected
116120 /** @var ModelValidatorInterface $validator */
@@ -126,6 +130,29 @@ protected function updateModelFromRequestUsingModel(Request $request, $model) {
126130 if (true !== $ validated ) {
127131 throw new ModelValidationException ($ validated );
128132 }
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 ();
129156
130157 // save it
131158 $ repository ->save ($ model );
0 commit comments