4
4
5
5
use Phpsa \LaravelApiController \Exceptions \UnknownColumnException ;
6
6
7
-
8
- Trait Parser {
9
-
10
- /**
7
+ trait Parser
8
+ {
9
+ /**
11
10
* Default Fields to response with.
12
11
*
13
12
* @var array
36
35
*/
37
36
protected $ maximumLimit = 0 ;
38
37
39
- /**
40
- * Parses our include joins
41
- *
42
- * @return void
43
- */
44
- protected function parseIncludeParams () : void
38
+ /**
39
+ * Parses our include joins.
40
+ *
41
+ * @return void
42
+ */
43
+ protected function parseIncludeParams () : void
45
44
{
46
45
$ field = config ('laravel-api-controller.parameters.include ' );
47
46
48
- if (empty ($ field )) {
47
+ if (empty ($ field )) {
49
48
return ;
50
- }
49
+ }
51
50
52
- $ with = $ this ->request ->input ($ field );
51
+ $ with = $ this ->request ->input ($ field );
53
52
54
53
if ($ with !== null ) {
55
54
$ this ->repository ->with (explode (', ' , $ with ));
56
55
}
57
- }
58
-
59
- /**
60
- * Parses our sort parameters
61
- *
62
- * @return void
63
- */
64
- protected function parseSortParams () : void
56
+ }
57
+
58
+ /**
59
+ * Parses our sort parameters.
60
+ *
61
+ * @return void
62
+ */
63
+ protected function parseSortParams () : void
65
64
{
65
+ $ sorts = $ this ->getSortValue ();
66
66
67
- $ sorts = $ this ->getSortValue ();
67
+ foreach ($ sorts as $ sort ) {
68
+ $ sortP = explode (' ' , $ sort );
69
+ $ sortF = $ sortP [0 ];
68
70
69
- foreach ($ sorts as $ sort ) {
71
+ if (empty ($ sortF ) || ! in_array ($ sortF , $ this ->tableColumns )) {
72
+ continue ;
73
+ }
70
74
71
- $ sortP = explode (' ' , $ sort );
72
- $ sortF = $ sortP [0 ];
75
+ $ sortD = ! empty ($ sortP [1 ]) && strtolower ($ sortP [1 ]) == 'desc ' ? 'desc ' : 'asc ' ;
76
+ $ this ->repository ->orderBy ($ sortF , $ sortD );
77
+ }
78
+ }
73
79
74
- if (empty ($ sortF ) || ! in_array ($ sortF , $ this ->tableColumns )) {
75
- continue ;
76
- }
80
+ /**
81
+ * gets the sort value.
82
+ *
83
+ * @returns array
84
+ */
85
+ protected function getSortValue () : array
86
+ {
87
+ $ field = config ('laravel-api-controller.parameters.sort ' );
88
+ $ sort = $ field && $ this ->request ->has ($ field ) ? $ this ->request ->input ($ field ) : $ this ->defaultSort ;
77
89
78
- $ sortD = ! empty ($ sortP [1 ]) && strtolower ($ sortP [1 ]) == 'desc ' ? 'desc ' : 'asc ' ;
79
- $ this ->repository ->orderBy ($ sortF , $ sortD );
80
- }
81
- }
90
+ if (! $ sort ) {
91
+ return [];
92
+ }
82
93
83
- /**
84
- * gets the sort value
85
- *
86
- * @returns array
87
- */
88
- protected function getSortValue () : array
89
- {
94
+ return is_array ($ sort ) ? $ sort : explode (', ' , $ sort );
95
+ }
90
96
91
- $ field = config ('laravel-api-controller.parameters.sort ' );
92
- $ sort = $ field && $ this ->request ->has ($ field ) ? $ this ->request ->input ($ field ) : $ this ->defaultSort ;
97
+ /**
98
+ * parses our filter parameters.
99
+ *
100
+ * @return void
101
+ */
102
+ protected function parseFilterParams () : void
103
+ {
104
+ $ where = $ this ->uriParser ->whereParameters ();
105
+ if (empty ($ where )) {
106
+ return ;
107
+ }
93
108
94
- if (!$ sort ){
95
- return [];
96
- }
109
+ foreach ($ where as $ whr ) {
110
+ if (strpos ($ whr ['key ' ], '. ' ) > 0 ) {
111
+ //@TODO: test if exists in the withs, if not continue out to exclude from the qbuild
112
+ //continue;
113
+ } elseif (! in_array ($ whr ['key ' ], $ this ->tableColumns )) {
114
+ continue ;
115
+ }
97
116
98
- return is_array ($ sort ) ? $ sort : explode (', ' , $ sort );
99
- }
117
+ $ this ->setWhereClause ($ whr );
118
+ }
119
+ }
100
120
101
- /**
102
- * parses our filter parameters
103
- *
104
- * @return void
105
- */
106
- protected function parseFilterParams () : void
121
+ /**
122
+ * set the Where clause.
123
+ *
124
+ * @param array $where the where clause
125
+ *
126
+ * @return void
127
+ */
128
+ protected function setWhereClause ($ where ) : void
107
129
{
108
- $ where = $ this ->uriParser ->whereParameters ();
109
- if (empty ($ where )){
110
- return ;
111
- }
112
-
113
- foreach ($ where as $ whr ) {
114
- if (strpos ($ whr ['key ' ], '. ' ) > 0 ) {
115
- //@TODO: test if exists in the withs, if not continue out to exclude from the qbuild
116
- //continue;
117
- } elseif (! in_array ($ whr ['key ' ], $ this ->tableColumns )) {
118
- continue ;
119
- }
120
-
121
- $ this ->setWhereClause ($ whr );
122
-
123
- }
124
-
125
- }
126
-
127
- /**
128
- * set the Where clause
129
- *
130
- * @param array $where the where clause
131
- *
132
- * @return void
133
- */
134
- protected function setWhereClause ($ where ) : void
135
- {
136
- switch ($ where ['type ' ]) {
137
- case 'In ' :
138
- if (! empty ($ where ['values ' ])) {
139
- $ this ->repository ->whereIn ($ where ['key ' ], $ where ['values ' ]);
140
- }
141
- break ;
142
- case 'NotIn ' :
143
- if (! empty ($ where ['values ' ])) {
144
- $ this ->repository ->whereNotIn ($ where ['key ' ], $ where ['values ' ]);
145
- }
146
- break ;
147
- case 'Basic ' :
148
- $ this ->repository ->where ($ where ['key ' ], $ where ['value ' ], $ where ['operator ' ]);
149
- break ;
150
- }
151
- }
152
-
153
- /**
154
- * parses the fields to return
155
- *
156
- * @throws UnknownColumnException
157
- * @return array
158
- */
159
- protected function parseFieldParams () : array
130
+ switch ($ where ['type ' ]) {
131
+ case 'In ' :
132
+ if (! empty ($ where ['values ' ])) {
133
+ $ this ->repository ->whereIn ($ where ['key ' ], $ where ['values ' ]);
134
+ }
135
+ break ;
136
+ case 'NotIn ' :
137
+ if (! empty ($ where ['values ' ])) {
138
+ $ this ->repository ->whereNotIn ($ where ['key ' ], $ where ['values ' ]);
139
+ }
140
+ break ;
141
+ case 'Basic ' :
142
+ $ this ->repository ->where ($ where ['key ' ], $ where ['value ' ], $ where ['operator ' ]);
143
+ break ;
144
+ }
145
+ }
146
+
147
+ /**
148
+ * parses the fields to return.
149
+ *
150
+ * @throws UnknownColumnException
151
+ * @return array
152
+ */
153
+ protected function parseFieldParams () : array
160
154
{
161
155
$ attributes = $ this ->model ->attributesToArray ();
162
156
$ fields = $ this ->request ->has ('fields ' ) && ! empty ($ this ->request ->input ('fields ' )) ? explode (', ' , $ this ->request ->input ('fields ' )) : $ this ->defaultFields ;
163
157
foreach ($ fields as $ k => $ field ) {
164
- if (
165
- $ field === '* ' ||
166
- in_array ($ field , $ this ->tableColumns ) ||
167
- array_key_exists ($ field , $ attributes )
168
- ) {
158
+ if (
159
+ $ field === '* ' ||
160
+ in_array ($ field , $ this ->tableColumns ) ||
161
+ array_key_exists ($ field , $ attributes )
162
+ ) {
169
163
continue ;
170
164
}
171
165
if (strpos ($ field , '. ' ) > 0 ) {
172
166
//@TODO check if mapped field exists
173
167
//@todo
174
168
unset($ fields [$ k ]);
175
169
continue ;
176
- }
177
-
178
- unset($ fields [$ k ]);
170
+ }
179
171
172
+ unset($ fields [$ k ]);
180
173
}
181
174
182
175
return $ fields ;
183
- }
184
-
185
- /**
186
- * parses the limit value
187
- *
188
- * @return int
189
- */
190
- protected function parseLimitParams () : int
191
- {
176
+ }
192
177
193
- $ limit = $ this ->request ->has ('limit ' ) ? intval ($ this ->request ->input ('limit ' )) : $ this ->defaultLimit ;
178
+ /**
179
+ * parses the limit value.
180
+ *
181
+ * @return int
182
+ */
183
+ protected function parseLimitParams () : int
184
+ {
185
+ $ limit = $ this ->request ->has ('limit ' ) ? intval ($ this ->request ->input ('limit ' )) : $ this ->defaultLimit ;
194
186
195
- if ($ this ->maximumLimit && ($ limit > $ this ->maximumLimit || ! $ limit )) {
187
+ if ($ this ->maximumLimit && ($ limit > $ this ->maximumLimit || ! $ limit )) {
196
188
$ limit = $ this ->maximumLimit ;
197
189
}
198
190
199
- return $ limit ;
200
- }
201
-
202
- }
191
+ return $ limit ;
192
+ }
193
+ }
0 commit comments