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 55use Nahid \JsonQ \Jsonq ;
66
77
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+ //
1717$ jq = new Jsonq ('data.json ' );
1818
1919$ 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 ();
2324dump ($ result );
Original file line number Diff line number Diff line change @@ -20,6 +20,12 @@ trait JsonQueriable
2020 */
2121 protected $ _map ;
2222
23+ /**
24+ * contains column names
25+ * @var array
26+ */
27+ protected $ _select = [];
28+
2329 /**
2430 * Stores base contents.
2531 *
@@ -185,6 +191,24 @@ public function isJson($value, $isReturnMap = false)
185191 return $ isReturnMap ? $ data : true ;
186192 }
187193
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+
188212 /**
189213 * Prepare data for result
190214 *
@@ -195,12 +219,13 @@ public function isJson($value, $isReturnMap = false)
195219 protected function prepareResult ($ data , $ isObject )
196220 {
197221 $ output = [];
198- if (is_array ($ data )) {
222+ if ($ this -> isMultiArray ($ data )) {
199223 foreach ($ data as $ key => $ val ) {
224+ $ val = $ this ->selectColumn ($ val );
200225 $ output [$ key ] = $ isObject ? (object ) $ val : $ val ;
201226 }
202227 } else {
203- $ output = json_decode (json_encode ($ data ), $ isObject );
228+ $ output = json_decode (json_encode ($ this -> selectColumn ( $ data )), ! $ isObject );
204229 }
205230
206231 return $ output ;
Original file line number Diff line number Diff line change @@ -82,6 +82,22 @@ public function at($node = null)
8282 return $ this ->from ($ node );
8383 }
8484
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+
85101 /**
86102 * getting prepared data
87103 *
@@ -98,7 +114,7 @@ public function get($object = true)
98114 }
99115
100116 if (!$ this ->isMultiArray ($ this ->_map )) {
101- return (object ) $ this ->_map ;
117+ return (object ) $ this ->selectColumn ( $ this -> _map ) ;
102118 }
103119
104120 return $ this ->prepareResult ($ this ->_map , $ object );
You can’t perform that action at this time.
0 commit comments