Skip to content

Commit 0338ef4

Browse files
committed
added quering option in nested data
1 parent 84d39ed commit 0338ef4

File tree

3 files changed

+64
-41
lines changed

3 files changed

+64
-41
lines changed

examples/index.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,10 @@
1919

2020
try {
2121
$result = $jq->from('data')
22-
->pipe(function($j) {
23-
return $j->transform(function($val) {
24-
$val['user_id'] = $val['user']['id'];
25-
$val['issued_at'] = date('Y, M d', strtotime($val['issued_at']));
26-
$val['created_at'] = date('Y, M d h:i:s', strtotime($val['created_at']));
27-
// $val['balance'] = $val['balance'] * 80;
28-
return $val;
29-
});
30-
})
31-
//->select('user_id', 'number', 'balance')
32-
->implode('balance', ' ');
33-
22+
//->where('user.id', '=', 345101090)
23+
// ->sortBy('user.id', 'desc')
24+
->countGroupBy('user.id')
25+
->get();
3426
dump($result);
3527
} catch (\Nahid\JsonQ\Exceptions\ConditionNotAllowedException $e) {
3628

src/JsonQueriable.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ protected function prepareResult($data, $isObject)
255255
$output[$key] = $isObject ? (object) $val : $val;
256256
}
257257
} else {
258-
$output = json_decode(json_encode($this->takeColumn($data)), !$isObject);
258+
$output = json_decode(json_encode($this->takeColumn($data)), $isObject);
259259
}
260260

261261
return $output;
@@ -293,21 +293,24 @@ protected function getDataFromFile($file, $type = 'application/json')
293293
throw new FileNotFoundException();
294294
}
295295

296+
297+
296298
/**
297-
* get data from node path
299+
* Get data from nested array
298300
*
299-
* @return mixed
301+
* @param $map array
302+
* @param $node string
303+
* @return bool|array|mixed
300304
*/
301-
protected function getData()
305+
protected function getFromNested($map, $node)
302306
{
303-
if (empty($this->_node) || $this->_node == '.') {
304-
return $this->_map;
307+
if (empty($node) || $node == '.') {
308+
return $map;
305309
}
306310

307-
if ($this->_node) {
311+
if ($node) {
308312
$terminate = false;
309-
$map = $this->_map;
310-
$path = $this->_node;
313+
$path = explode('.', $node);
311314

312315
foreach ($path as $val) {
313316
if (!isset($map[$val])) {
@@ -328,6 +331,16 @@ protected function getData()
328331
return false;
329332
}
330333

334+
/**
335+
* get data from node path
336+
*
337+
* @return mixed
338+
*/
339+
protected function getData()
340+
{
341+
return $this->getFromNested($this->_map, $this->_node);
342+
}
343+
331344
/**
332345
* process AND and OR conditions
333346
*
@@ -352,8 +365,8 @@ protected function processConditions()
352365
$function = [Condition::class, $function];
353366
}
354367

355-
$key = $rule['key'];
356-
$return = isset($val[$key]) ? call_user_func_array($function, [$val[$key], $rule['value']]) : false;
368+
$value = $this->getFromNested($val, $rule['key']);
369+
$return = $value ? call_user_func_array($function, [$value, $rule['value']]) : false;
357370
$tmp &= $return;
358371
}
359372
$res |= $tmp;

src/Jsonq.php

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,7 @@ public function from($node = null)
5959
throw new NullValueException("Null node exception");
6060
}
6161

62-
if ($node == '.') {
63-
$this->_node = $node;
64-
65-
return $this;
66-
}
67-
68-
$this->_node = explode('.', $node);
62+
$this->_node = $node;
6963

7064
return $this;
7165
}
@@ -121,7 +115,7 @@ public function except()
121115
* @return array|object
122116
* @throws ConditionNotAllowedException
123117
*/
124-
public function get($object = true)
118+
public function get($object = false)
125119
{
126120
$this->prepare();
127121

@@ -130,7 +124,7 @@ public function get($object = true)
130124
}
131125

132126
if (!$this->isMultiArray($this->_map)) {
133-
return (object) $this->takeColumn($this->_map);
127+
return $object ? (object) $this->takeColumn($this->_map) : $this->takeColumn($this->_map);
134128
}
135129

136130
return $this->prepareResult($this->_map, $object);
@@ -191,8 +185,32 @@ public function groupBy($column)
191185

192186
$data = [];
193187
foreach ($this->_map as $map) {
194-
if (isset($map[$column])) {
195-
$data[$map[$column]][] = $map;
188+
$value = $this->getFromNested($map, $column);
189+
if ($value) {
190+
$data[$value][] = $map;
191+
}
192+
}
193+
194+
$this->_map = $data;
195+
return $this;
196+
}
197+
198+
public function countGroupBy($column)
199+
{
200+
201+
$this->prepare();
202+
203+
$data = [];
204+
foreach ($this->_map as $map) {
205+
$value = $this->getFromNested($map, $column);
206+
if (!$value) {
207+
continue;
208+
}
209+
210+
if (isset($data[$value])) {
211+
$data[$value] ++;
212+
} else {
213+
$data[$value] = 1;
196214
}
197215
}
198216

@@ -314,7 +332,7 @@ public function avg($column = null)
314332
* @return object|array|null
315333
* @throws ConditionNotAllowedException
316334
*/
317-
public function first($object = true)
335+
public function first($object = false)
318336
{
319337
$this->prepare();
320338

@@ -333,7 +351,7 @@ public function first($object = true)
333351
* @return object|array|null
334352
* @throws ConditionNotAllowedException
335353
*/
336-
public function last($object = true)
354+
public function last($object = false)
337355
{
338356
$this->prepare();
339357

@@ -353,7 +371,7 @@ public function last($object = true)
353371
* @return object|array|null
354372
* @throws ConditionNotAllowedException
355373
*/
356-
public function nth($index, $object = true)
374+
public function nth($index, $object = false)
357375
{
358376
$this->prepare();
359377

@@ -391,8 +409,8 @@ public function sortBy($column, $order = 'asc')
391409
}
392410

393411
usort($this->_map, function ($a, $b) use ($column, $order) {
394-
$val1 = $a[$column];
395-
$val2 = $b[$column];
412+
$val1 = $this->getFromNested($a, $column);
413+
$val2 = $this->getFromNested($b, $column);
396414
if (is_string($val1)) {
397415
$val1 = strtolower($val1);
398416
}
@@ -401,7 +419,7 @@ public function sortBy($column, $order = 'asc')
401419
$val2 = strtolower($val2);
402420
}
403421

404-
if ($a[$column] == $b[$column]) {
422+
if ($val1 == $val2) {
405423
return 0;
406424
}
407425
$order = strtolower(trim($order));
@@ -442,7 +460,7 @@ public function sort($order = 'asc')
442460
* @throws NullValueException
443461
* @throws ConditionNotAllowedException
444462
*/
445-
public function find($path, $object = true)
463+
public function find($path, $object = false)
446464
{
447465
return $this->from($path)->prepare()->get($object);
448466
}

0 commit comments

Comments
 (0)