@@ -156,13 +156,6 @@ class Restful extends Controller
156
156
*/
157
157
public $ model ;
158
158
159
- /**
160
- * Restful::$getParams
161
- *
162
- * @var array
163
- */
164
- public $ getParams = [];
165
-
166
159
/**
167
160
* Restful::$getValidationRules
168
161
*
@@ -387,7 +380,7 @@ public function index()
387
380
unset($ _GET [ 'search ' ]);
388
381
}
389
382
390
- if (count ($ this ->getParams )) {
383
+ if (empty ($ this ->getValidationRules )) {
391
384
if ($ get = input ()->get ()) {
392
385
if (false !== ($ result = $ this ->model ->findWhere ($ get ->getArrayCopy (), $ limit ))) {
393
386
if ($ result ->count ()) {
@@ -398,39 +391,25 @@ public function index()
398
391
} else {
399
392
$ this ->sendError (204 );
400
393
}
394
+ } elseif (false !== ($ result = $ this ->model ->all ())) {
395
+ $ this ->sendPayload ($ result );
401
396
} else {
402
- $ this ->sendError (400 , ' Get parameters cannot be empty! ' );
397
+ $ this ->sendError (204 );
403
398
}
404
- } elseif (count ($ this ->getValidationRules )) {
405
- if ($ get = input ()->get ()) {
406
- $ get ->validation ($ this ->getValidationRules , $ this ->getValidationCustomErrors );
399
+ } elseif ($ get = input ()->get ()) {
400
+ $ get ->validation ($ this ->getValidationRules , $ this ->getValidationCustomErrors );
407
401
408
- if ( ! $ get ->validate ()) {
409
- $ this ->sendError (400 , implode (', ' , $ get ->validator ->getErrors ()));
410
- } else {
411
- $ conditions = [];
402
+ if ( ! $ get ->validate ()) {
403
+ $ this ->sendError (400 , implode (', ' , $ get ->validator ->getErrors ()));
404
+ }
412
405
413
- foreach ($ this ->getValidationRules as $ field => $ rule ) {
414
- if ($ get ->offsetExists ($ field )) {
415
- $ conditions [ $ field ] = $ get ->offsetGet ($ field );
416
- }
417
- }
406
+ $ conditions = [];
418
407
419
- if (false !== ($ result = $ this ->model ->findWhere ($ conditions , $ limit ))) {
420
- if ($ result ->count ()) {
421
- $ this ->sendPayload ($ result );
422
- } else {
423
- $ this ->sendError (204 );
424
- }
425
- } else {
426
- $ this ->sendError (204 );
427
- }
428
- }
429
- } else {
430
- $ this ->sendError (400 , 'Get parameters cannot be empty! ' );
408
+ foreach ($ this ->getValidationRules as $ field => $ rule ) {
409
+ $ conditions [ $ field ] = $ get ->offsetGet ($ field );
431
410
}
432
- } elseif ( $ get = input ()-> get ()) {
433
- if (false !== ($ result = $ this ->model ->findWhere ($ get -> getArrayCopy () , $ limit ))) {
411
+
412
+ if (false !== ($ result = $ this ->model ->findWhere ($ conditions , $ limit ))) {
434
413
if ($ result ->count ()) {
435
414
$ this ->sendPayload ($ result );
436
415
} else {
@@ -439,12 +418,10 @@ public function index()
439
418
} else {
440
419
$ this ->sendError (204 );
441
420
}
421
+ } elseif (false !== ($ result = $ this ->model ->all ())) {
422
+ $ this ->sendPayload ($ result );
442
423
} else {
443
- if (false !== ($ result = $ this ->model ->all ())) {
444
- $ this ->sendPayload ($ result );
445
- } else {
446
- $ this ->sendError (204 );
447
- }
424
+ $ this ->sendError (204 );
448
425
}
449
426
}
450
427
}
@@ -463,10 +440,6 @@ public function datatable()
463
440
$ this ->sendError (503 , 'Model is not exists! ' );
464
441
}
465
442
466
- if ( ! $ this ->model instanceof Model) {
467
- $ this ->sendError (503 , 'Model is not exists! ' );
468
- }
469
-
470
443
$ hasAction = false ;
471
444
if ($ request = input ()->request ()) {
472
445
// Start as limit
@@ -744,7 +717,8 @@ public function delete($id = null)
744
717
];
745
718
} else {
746
719
foreach ($ this ->model ->primaryKeys as $ primaryKey ) {
747
- $ conditions = [$ primaryKey => $ post ->offsetGet ($ primaryKey )];
720
+ $ conditions [ $ primaryKey ] = $ post ->offsetGet ($ primaryKey );
721
+
748
722
$ this ->actionValidationRules [ $ primaryKey ] = 'required ' ;
749
723
$ this ->actionValidationCustomErrors [ $ primaryKey ] = [
750
724
'required ' => language ('LABEL_ ' . strtoupper ($ primaryKey )) . ' cannot be empty! ' ,
@@ -774,7 +748,7 @@ public function delete($id = null)
774
748
$ this ->sendError (501 , 'Unavailable primary keys data ' );
775
749
}
776
750
777
- if ( ! $ post ->validate ($ conditions )) {
751
+ if ( ! $ post ->validate ()) {
778
752
$ this ->sendError (400 , implode (', ' , $ post ->validator ->getErrors ()));
779
753
}
780
754
@@ -882,20 +856,17 @@ public function delete($id = null)
882
856
*/
883
857
private function updateRecordStatus (array $ params , $ method )
884
858
{
885
- if ($ post = input ()->post ()) {
886
- if (empty ($ this ->actionValidationRules )) {
887
- if (empty ($ this ->model ->primaryKeys )) {
888
- $ primaryKey = empty ($ this ->model ->primaryKey ) ? 'id ' : $ this ->model ->primaryKey ;
889
- if ($ post ->offsetExists ($ primaryKey )) {
890
- $ conditions = [$ primaryKey => $ post ->offsetGet ($ primaryKey )];
891
- }
859
+ if (empty ($ this ->model )) {
860
+ output ()->sendError (204 );
861
+ } else {
862
+ if ( ! $ this ->model instanceof Model) {
863
+ $ this ->sendError (503 , 'Model is not ready ' );
864
+ }
892
865
893
- $ this ->actionValidationRules [ $ primaryKey ] = 'required ' ;
894
- $ this ->actionValidationCustomErrors [ $ primaryKey ] = [
895
- 'required ' => language ('LABEL_ ' . strtoupper ($ primaryKey )) . ' cannot be empty! ' ,
896
- ];
897
- } else {
898
- foreach ($ this ->model ->primaryKeys as $ primaryKey ) {
866
+ if ($ post = input ()->post ()) {
867
+ if (empty ($ this ->actionValidationRules )) {
868
+ if (empty ($ this ->model ->primaryKeys )) {
869
+ $ primaryKey = empty ($ this ->model ->primaryKey ) ? 'id ' : $ this ->model ->primaryKey ;
899
870
if ($ post ->offsetExists ($ primaryKey )) {
900
871
$ conditions = [$ primaryKey => $ post ->offsetGet ($ primaryKey )];
901
872
}
@@ -904,115 +875,126 @@ private function updateRecordStatus(array $params, $method)
904
875
$ this ->actionValidationCustomErrors [ $ primaryKey ] = [
905
876
'required ' => language ('LABEL_ ' . strtoupper ($ primaryKey )) . ' cannot be empty! ' ,
906
877
];
878
+ } else {
879
+ foreach ($ this ->model ->primaryKeys as $ primaryKey ) {
880
+ if ($ post ->offsetExists ($ primaryKey )) {
881
+ $ conditions [ $ primaryKey ] = $ post ->offsetGet ($ primaryKey );
882
+ }
883
+
884
+ $ this ->actionValidationRules [ $ primaryKey ] = 'required ' ;
885
+ $ this ->actionValidationCustomErrors [ $ primaryKey ] = [
886
+ 'required ' => language ('LABEL_ ' . strtoupper ($ primaryKey )) . ' cannot be empty! ' ,
887
+ ];
888
+ }
907
889
}
908
890
}
909
- }
910
891
911
- if (count ($ this ->actionValidationRules )) {
912
- $ post ->validation ($ this ->actionValidationRules , $ this ->actionValidationCustomErrors );
913
- }
892
+ if (count ($ this ->actionValidationRules )) {
893
+ $ post ->validation ($ this ->actionValidationRules , $ this ->actionValidationCustomErrors );
894
+ }
914
895
915
- if (empty ($ conditions )) {
916
- $ this ->sendError (501 , 'Unavailable primary keys data ' );
917
- }
896
+ if (empty ($ conditions )) {
897
+ $ this ->sendError (501 , 'Unavailable primary keys data ' );
898
+ }
918
899
919
- if ( ! $ post ->validate ($ conditions )) {
920
- $ this ->sendError (400 , implode (', ' , $ post ->validator ->getErrors ()));
921
- }
900
+ if ( ! $ post ->validate ()) {
901
+ $ this ->sendError (400 , implode (', ' , $ post ->validator ->getErrors ()));
902
+ }
903
+
904
+ if ($ result = $ this ->model ->findWhere ($ conditions )) {
905
+ if ($ result instanceof Row) {
906
+ if ($ row ->{$ method }()) {
907
+ $ this ->sendError (200 );
908
+ } else {
909
+ $ this ->sendError (501 );
910
+ }
911
+ } elseif ($ result instanceof Result) {
912
+ foreach ($ result as $ row ) {
913
+ $ row ->{$ method }();
914
+ }
922
915
923
- if ($ result = $ this ->model ->findWhere ($ conditions )) {
924
- if ($ result instanceof Row) {
925
- if ($ row ->{$ method }()) {
926
916
$ this ->sendError (200 );
927
- } else {
928
- $ this ->sendError (501 );
929
- }
930
- } elseif ($ result instanceof Result) {
931
- foreach ($ result as $ row ) {
932
- $ row ->{$ method }();
933
917
}
934
-
935
- $ this ->sendError (200 );
918
+ } else {
919
+ $ this ->sendError (404 , ' Data not found! ' );
936
920
}
937
- } else {
938
- $ this ->sendError (404 , 'Data not found! ' );
939
- }
940
- } elseif (input ()->server ('REQUEST_METHOD ' ) === 'PATCH ' ) {
941
- $ validator = new Validator ();
942
-
943
- if (empty ($ this ->actionValidationRules )) {
944
- if (empty ($ this ->model ->primaryKeys )) {
945
- $ primaryKey = empty ($ this ->model ->primaryKey ) ? 'id ' : $ this ->model ->primaryKey ;
946
- if (count ($ params )) {
947
- $ conditions = [$ primaryKey => reset ($ params )];
948
- }
921
+ } elseif (input ()->server ('REQUEST_METHOD ' ) === 'PATCH ' ) {
922
+ $ validator = new Validator ();
949
923
950
- $ this ->actionValidationRules [ $ primaryKey ] = 'required ' ;
951
- $ this ->actionValidationCustomErrors [ $ primaryKey ] = [
952
- 'required ' => language ('LABEL_ ' . strtoupper ($ primaryKey )) . ' cannot be empty! ' ,
953
- ];
954
- } else {
955
- foreach ($ this ->model ->primaryKeys as $ key => $ primaryKey ) {
956
- if (isset ($ params [ $ key ])) {
957
- $ conditions [ $ primaryKey ] = $ params [ $ key ];
924
+ if (empty ($ this ->actionValidationRules )) {
925
+ if (empty ($ this ->model ->primaryKeys )) {
926
+ $ primaryKey = empty ($ this ->model ->primaryKey ) ? 'id ' : $ this ->model ->primaryKey ;
927
+ if (count ($ params )) {
928
+ $ conditions = [$ primaryKey => reset ($ params )];
958
929
}
959
930
960
931
$ this ->actionValidationRules [ $ primaryKey ] = 'required ' ;
961
932
$ this ->actionValidationCustomErrors [ $ primaryKey ] = [
962
933
'required ' => language ('LABEL_ ' . strtoupper ($ primaryKey )) . ' cannot be empty! ' ,
963
934
];
964
- }
965
- }
966
- } else {
967
- if (empty ($ this ->model ->primaryKeys )) {
968
- $ primaryKey = empty ($ this ->model ->primaryKey ) ? 'id ' : $ this ->model ->primaryKey ;
969
- if (count ($ params )) {
970
- $ conditions = [$ primaryKey => reset ($ params )];
935
+ } else {
936
+ foreach ($ this ->model ->primaryKeys as $ key => $ primaryKey ) {
937
+ if (isset ($ params [ $ key ])) {
938
+ $ conditions [ $ primaryKey ] = $ params [ $ key ];
939
+ }
940
+
941
+ $ this ->actionValidationRules [ $ primaryKey ] = 'required ' ;
942
+ $ this ->actionValidationCustomErrors [ $ primaryKey ] = [
943
+ 'required ' => language ('LABEL_ ' . strtoupper ($ primaryKey )) . ' cannot be empty! ' ,
944
+ ];
945
+ }
971
946
}
972
947
} else {
973
- foreach ($ this ->model ->primaryKeys as $ key => $ primaryKey ) {
974
- if (isset ($ params [ $ key ])) {
975
- $ conditions [ $ primaryKey ] = $ params [ $ key ];
948
+ if (empty ($ this ->model ->primaryKeys )) {
949
+ $ primaryKey = empty ($ this ->model ->primaryKey ) ? 'id ' : $ this ->model ->primaryKey ;
950
+ if (count ($ params )) {
951
+ $ conditions = [$ primaryKey => reset ($ params )];
952
+ }
953
+ } else {
954
+ foreach ($ this ->model ->primaryKeys as $ key => $ primaryKey ) {
955
+ if (isset ($ params [ $ key ])) {
956
+ $ conditions [ $ primaryKey ] = $ params [ $ key ];
957
+ }
976
958
}
977
959
}
978
960
}
979
- }
980
961
981
- if (count ($ this ->actionValidationRules )) {
982
- $ validator ->setRules ($ this ->actionValidationRules , $ this ->actionValidationCustomErrors );
983
- }
962
+ if (count ($ this ->actionValidationRules )) {
963
+ $ validator ->setRules ($ this ->actionValidationRules , $ this ->actionValidationCustomErrors );
964
+ }
984
965
985
- if (empty ($ conditions )) {
986
- $ this ->sendError (501 , 'Unavailable primary keys data ' );
987
- }
966
+ if (empty ($ conditions )) {
967
+ $ this ->sendError (501 , 'Unavailable primary keys data ' );
968
+ }
988
969
989
- if ( ! $ validator ->validate ($ conditions )) {
990
- $ this ->sendError (400 , implode (', ' , $ validator ->getErrors ()));
991
- }
970
+ if ( ! $ validator ->validate ($ conditions )) {
971
+ $ this ->sendError (400 , implode (', ' , $ validator ->getErrors ()));
972
+ }
992
973
993
- if ( ! $ this ->model instanceof Model) {
994
- $ this ->sendError (503 , 'Model is not ready! ' );
995
- }
974
+ if ( ! $ this ->model instanceof Model) {
975
+ $ this ->sendError (503 , 'Model is not ready! ' );
976
+ }
977
+
978
+ if ($ result = $ this ->model ->findWhere ($ conditions )) {
979
+ if ($ result instanceof Row) {
980
+ if ($ row ->{$ method }()) {
981
+ $ this ->sendError (200 );
982
+ } else {
983
+ $ this ->sendError (501 );
984
+ }
985
+ } elseif ($ result instanceof Result) {
986
+ foreach ($ result as $ row ) {
987
+ $ row ->{$ method }();
988
+ }
996
989
997
- if ($ result = $ this ->model ->findWhere ($ conditions )) {
998
- if ($ result instanceof Row) {
999
- if ($ row ->{$ method }()) {
1000
990
$ this ->sendError (200 );
1001
- } else {
1002
- $ this ->sendError (501 );
1003
991
}
1004
- } elseif ($ result instanceof Result) {
1005
- foreach ($ result as $ row ) {
1006
- $ row ->{$ method }();
1007
- }
1008
-
1009
- $ this ->sendError (200 );
992
+ } else {
993
+ $ this ->sendError (404 , 'Data not found! ' );
1010
994
}
1011
995
} else {
1012
- $ this ->sendError (404 , ' Data not found! ' );
996
+ $ this ->sendError (400 );
1013
997
}
1014
- } else {
1015
- $ this ->sendError (400 );
1016
998
}
1017
999
}
1018
1000
@@ -1030,7 +1012,7 @@ public function publish($id = null)
1030
1012
$ this ->updateRecordStatus (func_get_args (), 'publish ' );
1031
1013
}
1032
1014
1033
- // ------------------------------------------------------------------------
1015
+ // ------------------------------------------------------------------------
1034
1016
1035
1017
/**
1036
1018
* Restful::unpublish
@@ -1044,7 +1026,7 @@ public function unpublish($id = null)
1044
1026
$ this ->updateRecordStatus (func_get_args (), 'unpublish ' );
1045
1027
}
1046
1028
1047
- // ------------------------------------------------------------------------
1029
+ // ------------------------------------------------------------------------
1048
1030
1049
1031
/**
1050
1032
* Restful::archive
@@ -1053,8 +1035,10 @@ public function unpublish($id = null)
1053
1035
*
1054
1036
* @throws OutOfRangeException
1055
1037
*/
1056
- public function archive ($ id = null )
1057
- {
1038
+ public
1039
+ function archive (
1040
+ $ id = null
1041
+ ) {
1058
1042
$ this ->updateRecordStatus (func_get_args (), 'archive ' );
1059
1043
}
1060
1044
0 commit comments