File tree Expand file tree Collapse file tree 3 files changed +57
-15
lines changed Expand file tree Collapse file tree 3 files changed +57
-15
lines changed Original file line number Diff line number Diff line change 5
5
use Nahid \JsonQ \Jsonq ;
6
6
7
7
8
- $ result = '' ;
9
- Jsonq::macro ('less ' , function ($ payable , $ val ) {
10
- return $ payable < $ val ;
11
- });
12
-
13
- Jsonq::macro ('int ' , function ($ payable , $ val ) {
14
- return is_integer ($ payable );
15
- });
16
-
8
+ // $result = '';
9
+ // Jsonq::macro('less', function ($payable, $val) {
10
+ // return $payable < $val;
11
+ // });
12
+ //
13
+ // Jsonq::macro('int', function ($payable, $val) {
14
+ // return is_integer($payable);
15
+ // });
16
+ //
17
17
$ jq = new Jsonq ('data.json ' );
18
18
19
19
$ result = $ jq ->from ('products ' )
20
- ->where ('cat ' , 'int ' , 0 )
21
-
22
- ->sum ('price ' );
20
+ ->select ('name ' , 'id ' )
21
+ ->where ('cat ' , '= ' , 2 )
22
+ ->sortBy ('user_id ' , 'asc ' )
23
+ ->get ();
23
24
dump ($ result );
Original file line number Diff line number Diff line change @@ -20,6 +20,12 @@ trait JsonQueriable
20
20
*/
21
21
protected $ _map ;
22
22
23
+ /**
24
+ * contains column names
25
+ * @var array
26
+ */
27
+ protected $ _select = [];
28
+
23
29
/**
24
30
* Stores base contents.
25
31
*
@@ -185,6 +191,24 @@ public function isJson($value, $isReturnMap = false)
185
191
return $ isReturnMap ? $ data : true ;
186
192
}
187
193
194
+
195
+ /**
196
+ * selecting specific column
197
+ *
198
+ * @param $array
199
+ * @return array
200
+ */
201
+ protected function selectColumn ($ array )
202
+ {
203
+ $ keys = $ this ->_select ;
204
+
205
+ if (count ($ keys ) == 0 ) {
206
+ return $ array ;
207
+ }
208
+
209
+ return array_intersect_key ($ array , array_flip ((array ) $ keys ));
210
+ }
211
+
188
212
/**
189
213
* Prepare data for result
190
214
*
@@ -195,12 +219,13 @@ public function isJson($value, $isReturnMap = false)
195
219
protected function prepareResult ($ data , $ isObject )
196
220
{
197
221
$ output = [];
198
- if (is_array ($ data )) {
222
+ if ($ this -> isMultiArray ($ data )) {
199
223
foreach ($ data as $ key => $ val ) {
224
+ $ val = $ this ->selectColumn ($ val );
200
225
$ output [$ key ] = $ isObject ? (object ) $ val : $ val ;
201
226
}
202
227
} else {
203
- $ output = json_decode (json_encode ($ data ), $ isObject );
228
+ $ output = json_decode (json_encode ($ this -> selectColumn ( $ data )), ! $ isObject );
204
229
}
205
230
206
231
return $ output ;
Original file line number Diff line number Diff line change @@ -82,6 +82,22 @@ public function at($node = null)
82
82
return $ this ->from ($ node );
83
83
}
84
84
85
+ /**
86
+ * select desired column
87
+ *
88
+ * @param ... scalar
89
+ * @return $this
90
+ */
91
+ public function select ()
92
+ {
93
+ $ args = func_get_args ();
94
+ if (count ($ args ) > 0 ){
95
+ $ this ->_select = $ args ;
96
+ }
97
+
98
+ return $ this ;
99
+ }
100
+
85
101
/**
86
102
* getting prepared data
87
103
*
@@ -98,7 +114,7 @@ public function get($object = true)
98
114
}
99
115
100
116
if (!$ this ->isMultiArray ($ this ->_map )) {
101
- return (object ) $ this ->_map ;
117
+ return (object ) $ this ->selectColumn ( $ this -> _map ) ;
102
118
}
103
119
104
120
return $ this ->prepareResult ($ this ->_map , $ object );
You can’t perform that action at this time.
0 commit comments