Skip to content

Commit 0d1b942

Browse files
committed
update macro, and refactor code
1 parent dc26178 commit 0d1b942

File tree

2 files changed

+95
-92
lines changed

2 files changed

+95
-92
lines changed

examples/index.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66

77

88
$result = '';
9-
$json=new Jsonq('data.json');
10-
$result = $json->from('users')
11-
->where('cat', '=', 2)
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+
$jq = new Jsonq('data.json');
18+
19+
$result = $jq->from('products')
20+
->where('cat', 'int', 0)
1221
->sum('price');
1322
dump($result);

src/JsonQueriable.php

Lines changed: 83 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ trait JsonQueriable
4242
* map all conditions with methods
4343
* @var array
4444
*/
45-
protected $_rulesMap = [
45+
protected static $_rulesMap = [
4646
'=' => 'equal',
4747
'eq' => 'equal',
4848
'==' => 'exactEqual',
@@ -67,7 +67,6 @@ trait JsonQueriable
6767
'endswith' => 'endsWith',
6868
'match' => 'match',
6969
'contains' => 'contains',
70-
'macro' => 'macro',
7170
];
7271

7372

@@ -269,10 +268,18 @@ protected function processConditions()
269268
foreach ($conditions as $cond) {
270269
$tmp = true;
271270
foreach ($cond as $rule) {
272-
$func = 'cond' . ucfirst($this->_rulesMap[$rule['condition']]);
273-
if (method_exists($this, $func)) {
271+
$call_func = [];
272+
if (is_callable(self::$_rulesMap[$rule['condition']])) {
273+
$func = self::$_rulesMap[$rule['condition']];
274+
$call_func = $func;
275+
} else {
276+
$func = 'cond' . ucfirst(self::$_rulesMap[$rule['condition']]);
277+
$call_func[] = $this;
278+
$call_func[] = $func;
279+
}
280+
if (is_callable($func) || method_exists($this, $func) ) {
274281
if (isset($val[$rule['key']])) {
275-
$return = call_user_func_array([$this, $func], [$val[$rule['key']], $rule['value']]);
282+
$return = call_user_func_array($call_func, [$val[$rule['key']], $rule['value']]);
276283
}else {
277284
$return = false;
278285
}
@@ -469,173 +476,176 @@ public function whereContains($key, $value)
469476
/**
470477
* make macro for custom where clause
471478
*
472-
* @param $key string
479+
* @param $name string
473480
* @param $fn callable
474-
* @return $this
481+
* @return bool
475482
*/
476-
public function macro($key, callable $fn)
483+
public static function macro($name, callable $fn)
477484
{
478-
$this->where($key, 'macro', $fn);
485+
if (!in_array($name, self::$_rulesMap)) {
486+
self::$_rulesMap[$name] = $fn;
487+
return true;
488+
}
479489

480-
return $this;
490+
return false;
481491
}
482492

483493
// condition methods
484494

485495
/**
486496
* make Equal condition
487497
*
488-
* @param $key string
489-
* @param $val mixed
498+
* @param $val string
499+
* @param $payable mixed
490500
* @return bool
491501
*/
492-
protected function condEqual($key, $val)
502+
protected function condEqual($val, $payable)
493503
{
494-
return $key == $val;
504+
return $val == $payable;
495505
}
496506

497507
/**
498508
* make Exact Equal condition
499509
*
500-
* @param $key string
501-
* @param $val mixed
510+
* @param $val string
511+
* @param $payable mixed
502512
* @return bool
503513
*/
504-
protected function condExactEqual($key, $val)
514+
protected function condExactEqual($val, $payable)
505515
{
506-
return $key === $val;
516+
return $val === $payable;
507517
}
508518

509519
/**
510520
* make Not Equal condition
511521
*
512-
* @param $key string
513-
* @param $val mixed
522+
* @param $val string
523+
* @param $payable mixed
514524
* @return bool
515525
*/
516-
protected function condNotEqual($key, $val)
526+
protected function condNotEqual($val, $payable)
517527
{
518-
return $key != $val;
528+
return $val != $payable;
519529
}
520530

521531
/**
522532
* make Not Exact Equal condition
523533
*
524-
* @param $key string
525-
* @param $val mixed
534+
* @param $val string
535+
* @param $payable mixed
526536
* @return bool
527537
*/
528-
protected function condNotExactEqual($key, $val)
538+
protected function condNotExactEqual($val, $payable)
529539
{
530-
return $key !== $val;
540+
return $val !== $payable;
531541
}
532542

533543
/**
534544
* make Greater Than condition
535545
*
536-
* @param $key string
537-
* @param $val mixed
546+
* @param $val string
547+
* @param $payable mixed
538548
* @return bool
539549
*/
540-
protected function condGreater($key, $val)
550+
protected function condGreater($val, $payable)
541551
{
542-
return $key > $val;
552+
return $val > $payable;
543553
}
544554

545555
/**
546556
* make Less Than condition
547557
*
548-
* @param $key string
549-
* @param $val mixed
558+
* @param $val string
559+
* @param $payable mixed
550560
* @return bool
551561
*/
552-
protected function condLess($key, $val)
562+
protected function condLess($val, $payable)
553563
{
554-
return $key < $val;
564+
return $val < $payable;
555565
}
556566

557567
/**
558568
* make Greater Equal condition
559569
*
560-
* @param $key string
561-
* @param $val mixed
570+
* @param $val string
571+
* @param $payable mixed
562572
* @return bool
563573
*/
564-
protected function condGreaterEqual($key, $val)
574+
protected function condGreaterEqual($val, $payable)
565575
{
566-
return $key >= $val;
576+
return $val >= $payable;
567577
}
568578

569579
/**
570580
* make Less Equal condition
571581
*
572-
* @param $key string
573-
* @param $val mixed
582+
* @param $val string
583+
* @param $payable mixed
574584
* @return bool
575585
*/
576-
protected function condLessEqual($key, $val)
586+
protected function condLessEqual($val, $payable)
577587
{
578-
return $key <= $val;
588+
return $val <= $payable;
579589
}
580590

581591
/**
582592
* make In condition
583593
*
584-
* @param $key string
585-
* @param $val mixed
594+
* @param $val string
595+
* @param $payable mixed
586596
* @return bool
587597
*/
588-
protected function condIn($key, $val)
598+
protected function condIn($val, $payable)
589599
{
590-
return (is_array($val) && in_array($key, $val));
600+
return (is_array($payable) && in_array($val, $payable));
591601
}
592602

593603
/**
594604
* make Not In condition
595605
*
596-
* @param $key string
597-
* @param $val mixed
606+
* @param $val string
607+
* @param $payable mixed
598608
* @return bool
599609
*/
600-
protected function condNotIn($key, $val)
610+
protected function condNotIn($val, $payable)
601611
{
602-
return (is_array($val) && !in_array($key, $val));
612+
return (is_array($val) && !in_array($val, $payable));
603613
}
604614

605615
/**
606616
* make Null condition
607617
*
608-
* @param $key string
609-
* @param $val mixed
618+
* @param $val string
619+
* @param $payable mixed
610620
* @return bool
611621
*/
612-
protected function condNull($key, $val)
622+
protected function condNull($val, $payable)
613623
{
614-
return (is_null($key) || $key == $val);
624+
return (is_null($val) || $val == $payable);
615625
}
616626

617627
/**
618628
* make Not Null condition
619629
*
620-
* @param $key string
621-
* @param $val mixed
630+
* @param $val string
631+
* @param $payable mixed
622632
* @return bool
623633
*/
624-
protected function condNotNull($key, $val)
634+
protected function condNotNull($val, $payable)
625635
{
626-
return (!is_null($key) && $key !== $val);
636+
return (!is_null($val) && $val !== $payable);
627637
}
628638

629639
/**
630640
* make Starts With condition
631641
*
632-
* @param $key string
633-
* @param $val mixed
642+
* @param $val string
643+
* @param $payable mixed
634644
* @return bool
635645
*/
636-
protected function condStartsWith($key, $val)
646+
protected function condStartsWith($val, $payable)
637647
{
638-
if (preg_match("/^$val/", $key)) {
648+
if (preg_match("/^$payable/", $val)) {
639649
return true;
640650
}
641651

@@ -645,17 +655,17 @@ protected function condStartsWith($key, $val)
645655
/**
646656
* make Match condition
647657
*
648-
* @param $key string
649-
* @param $val mixed
658+
* @param $val string
659+
* @param $payable mixed
650660
* @return bool
651661
*/
652-
protected function condMatch($key, $val)
662+
protected function condMatch($val, $payable)
653663
{
654-
$val = rtrim($val, '$/');
655-
$val = ltrim($val, '/^');
664+
$payable = rtrim($payable, '$/');
665+
$payable = ltrim($payable, '/^');
656666

657-
$pattern = '/^'.$val.'$/';
658-
if (preg_match($pattern, $key)) {
667+
$pattern = '/^'.$payable.'$/';
668+
if (preg_match($pattern, $val)) {
659669
return true;
660670
}
661671

@@ -665,28 +675,12 @@ protected function condMatch($key, $val)
665675
/**
666676
* make Contains condition
667677
*
668-
* @param $key string
669-
* @param $val mixed
670-
* @return bool
671-
*/
672-
protected function condContains($key, $val)
673-
{
674-
return (strpos($key, $val) !== false);
675-
}
676-
677-
/**
678-
* make Macro condition
679-
*
680-
* @param $key string
681-
* @param $fn callable
678+
* @param $val string
679+
* @param $payable mixed
682680
* @return bool
683681
*/
684-
protected function condMacro($key, callable $fn)
682+
protected function condContains($val, $payable)
685683
{
686-
if (is_callable($fn)) {
687-
return $fn($key);
688-
}
689-
690-
return false;
684+
return (strpos($val, $payable) !== false);
691685
}
692686
}

0 commit comments

Comments
 (0)