@@ -19,21 +19,22 @@ trait ResourceControllerTrait {
19
19
protected $ resource = null ;
20
20
public $ queryParser ;
21
21
22
- public function _getResourceName () : string {
23
- if ($ this ->resource )
22
+ public function _getResourceName (): string {
23
+ if ($ this ->resource ) {
24
24
return $ this ->resource ;
25
- else
26
- return str_replace ('Controllers ' , 'Models ' , singular (get_class ($ this )).'Model ' );
25
+ } else {
26
+ return str_replace ('Controllers ' , 'Models ' , singular (get_class ($ this )) . 'Model ' );
27
+ }
27
28
}
28
29
29
30
public function get ($ id = 0 ) {
30
31
/** @var Model|ResourceBaseModelInterface|ResourceModelInterface $model */
31
32
$ className = $ this ->_getResourceName ();
32
33
$ model = new $ className ();
33
34
$ items = $ model ->restGet ($ id , $ this ->queryParser );
34
- if ($ id )
35
+ if ($ id ) {
35
36
$ this ->_setResource ($ items ->first ());
36
- else {
37
+ } else {
37
38
$ this ->_setResources ($ items );
38
39
}
39
40
$ this ->success ();
@@ -49,11 +50,11 @@ public function post() {
49
50
50
51
$ data = $ this ->request ->getJSON (true );
51
52
52
- if (is_array ($ this ->request ->getJSON ())) {
53
+ if (is_array ($ this ->request ->getJSON ())) {
53
54
54
55
/** @var Entity $resources */
55
56
$ resources = new $ entityName ();
56
- foreach ($ data as $ dataItem ) {
57
+ foreach ($ data as $ dataItem ) {
57
58
$ resources ->add ($ entityName ::post ($ dataItem ));
58
59
}
59
60
$ this ->_setResources ($ resources );
@@ -78,12 +79,12 @@ public function put($id = 0) {
78
79
79
80
$ data = $ this ->request ->getJSON (true );
80
81
81
- if (is_array ($ this ->request ->getJSON ())) {
82
+ if (is_array ($ this ->request ->getJSON ())) {
82
83
83
84
/** @var Entity $resources */
84
85
$ resources = new $ entityName ();
85
- foreach ($ data as $ dataItem ) {
86
- $ resources ->add ($ entityName ::put (isset ($ dataItem [' id ' ]) ? $ dataItem [' id ' ] : 0 , $ dataItem ));
86
+ foreach ($ data as $ dataItem ) {
87
+ $ resources ->add ($ entityName ::put (isset ($ dataItem [$ model -> getPrimaryKey () ]) ? $ dataItem [$ model -> getPrimaryKey () ] : 0 , $ dataItem ));
87
88
}
88
89
$ this ->_setResources ($ resources );
89
90
@@ -102,23 +103,24 @@ public function patch($id = 0) {
102
103
103
104
/** @var Model $model */
104
105
$ model = new $ className ();
106
+ $ primaryKey = $ model ->getPrimaryKey ();
105
107
/** @var Entity|ResourceEntityInterface $entityName */
106
108
$ entityName = $ model ->returnType ;
107
109
108
110
$ data = $ this ->request ->getJSON (true );
109
111
110
- if ($ id ) {
112
+ if ($ id ) {
111
113
112
114
$ item = $ entityName ::patch ($ id , $ data );
113
115
$ this ->_setResource ($ item );
114
116
115
- } else if (is_array ($ data )) {
117
+ } else if (is_array ($ data )) {
116
118
117
119
/** @var Entity $resources */
118
120
$ resources = new $ entityName ();
119
- foreach ($ data as $ dataItem ) {
120
- if (isset ($ dataItem [' id ' ]) && $ dataItem [' id ' ] > 0 )
121
- $ resources ->add ($ entityName ::patch ($ dataItem [' id ' ], $ dataItem ));
121
+ foreach ($ data as $ dataItem ) {
122
+ if (isset ($ dataItem [$ primaryKey ]) && $ dataItem [$ primaryKey ] > 0 )
123
+ $ resources ->add ($ entityName ::patch ($ dataItem [$ primaryKey ], $ dataItem ));
122
124
else
123
125
$ resources ->add ($ entityName ::post ($ dataItem ));
124
126
}
@@ -134,13 +136,13 @@ public function delete($id) {
134
136
135
137
/** @var Model|ResourceBaseModelInterface|ResourceModelInterface $model */
136
138
$ model = new $ className ();
137
- $ model ->where (' id ' , $ id );
139
+ $ model ->where ($ model -> getPrimaryKey () , $ id );
138
140
139
141
/** @var Entity $item */
140
142
$ item = $ model ->find ();
141
143
142
- if ($ item ->exists ()) {
143
- if (!$ model ->isRestDeleteAllowed ($ item )) {
144
+ if ($ item ->exists ()) {
145
+ if (!$ model ->isRestDeleteAllowed ($ item )) {
144
146
$ this ->error (ErrorCodes::InsufficientAccess, 403 );
145
147
return ;
146
148
}
@@ -153,5 +155,4 @@ public function delete($id) {
153
155
}
154
156
155
157
156
-
157
158
}
0 commit comments