Skip to content

Commit e4fcb08

Browse files
committed
fix whereNull, whereNotNull issue, fix some conditional issues and added some features
1 parent 0338ef4 commit e4fcb08

File tree

5 files changed

+20
-315
lines changed

5 files changed

+20
-315
lines changed

examples/data1.json

Lines changed: 0 additions & 292 deletions
This file was deleted.

examples/index.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
$jq = new Jsonq('data1.json');
1919

2020
try {
21-
$result = $jq->from('data')
22-
//->where('user.id', '=', 345101090)
23-
// ->sortBy('user.id', 'desc')
24-
->countGroupBy('user.id')
25-
->get();
21+
$result = $jq->from('.')
22+
//->whereContains('title', 'Day')
23+
//->where('year', '>=', 2000)
24+
->countGroupBy('year')
25+
->avg();
2626
dump($result);
2727
} catch (\Nahid\JsonQ\Exceptions\ConditionNotAllowedException $e) {
2828

src/Condition.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public static function notIn($value, $comparable)
141141
*
142142
* @return bool
143143
*/
144-
public static function isNull($value)
144+
public static function isNull($value, $comparable)
145145
{
146146
return is_null($value);
147147
}
@@ -153,7 +153,7 @@ public static function isNull($value)
153153
*
154154
* @return bool
155155
*/
156-
public static function isNotNull($value)
156+
public static function isNotNull($value, $comparable)
157157
{
158158
return !is_null($value);
159159
}

src/JsonQueriable.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,16 @@ protected function exceptColumn($array)
244244
*
245245
* @param mixed $data
246246
* @param bool $isObject
247-
* @return array
247+
* @return array|mixed
248248
*/
249249
protected function prepareResult($data, $isObject)
250250
{
251251
$output = [];
252+
253+
if (is_null($data) || is_scalar($data)) {
254+
return $data;
255+
}
256+
252257
if ($this->isMultiArray($data)) {
253258
foreach ($data as $key => $val) {
254259
$val = $this->takeColumn($val);
@@ -313,7 +318,7 @@ protected function getFromNested($map, $node)
313318
$path = explode('.', $node);
314319

315320
foreach ($path as $val) {
316-
if (!isset($map[$val])) {
321+
if (!array_key_exists($val, $map)) {
317322
$terminate = true;
318323
break;
319324
}
@@ -366,7 +371,7 @@ protected function processConditions()
366371
}
367372

368373
$value = $this->getFromNested($val, $rule['key']);
369-
$return = $value ? call_user_func_array($function, [$value, $rule['value']]) : false;
374+
$return = $value === null || $value ? call_user_func_array($function, [$value, $rule['value']]) : false;
370375
$tmp &= $return;
371376
}
372377
$res |= $tmp;
@@ -481,7 +486,7 @@ public function whereNotIn($key = null, $value = [])
481486
*/
482487
public function whereNull($key = null)
483488
{
484-
$this->where($key, 'null', null);
489+
$this->where($key, 'null', 'null');
485490
return $this;
486491
}
487492

@@ -493,7 +498,7 @@ public function whereNull($key = null)
493498
*/
494499
public function whereNotNull($key = null)
495500
{
496-
$this->where($key, 'notnull', null);
501+
$this->where($key, 'notnull', 'null');
497502

498503
return $this;
499504
}

0 commit comments

Comments
 (0)