13
13
*/
14
14
class ArrayOne
15
15
{
16
- public const VERSION = "1.4 " ;
16
+ public const VERSION = "1.5 " ;
17
17
/** @var array|null */
18
18
protected $ array ;
19
19
protected $ serviceObject ;
@@ -426,17 +426,23 @@ public function join(?array $arrayToJoin, $column1, $column2): ArrayOne
426
426
427
427
/**
428
428
* It filters the values. If the condition is false, then the row is deleted. It uses array_filter()<br/>
429
- * The indexes are not rebuilt.
429
+ * The indexes are not rebuilt.<br>
430
430
* <b>Example:</b><br/>
431
431
* <pre>
432
432
* $array = [['id' => 1, 'name' => 'chile'], ['id' => 2, 'name' => 'argentina'], ['id' => 3, 'name' => 'peru']];
433
- * // ['id' => 2, 'name' => 'argentina']
433
+ * // get the row #2 "argentina":
434
+ * // using a function:
434
435
* $r = ArrayOne::set($array)->filter(function($row, $id) {return $row['id'] === 2;}, true)->result();
435
- * // [1=>['id' => 2, 'name' => 'argentina']]
436
+ * // using a function a returning a flat result:
436
437
* $r = ArrayOne::set($array)->filter(function($row, $id) {return $row['id'] === 2;}, false)->result();
438
+ * // using an associative array:
439
+ * $r = ArrayOne::set($array)->filter(['id'=>'eq;2'], false)->result();
440
+ * // using an associative array that contains an array:
441
+ * $r = ArrayOne::set($array)->filter(['id'=>['eq,2], false)->result();
437
442
* </pre>
438
443
* @param callable|null|array $condition you can use a callable function ($row,$id)<br/>
439
- * or a comparison array ['id'=>'eq;2']
444
+ * or a comparison array ['id'=>'eq;2|lt;3'] "|" adds more comparisons<br>
445
+ * or a comparison array ['id'=>[['eq',2],['lt',3]]<br>
440
446
* @param bool $flat
441
447
* @return $this
442
448
* @see ArrayOne::validate to see the definition of comparison array
@@ -463,9 +469,11 @@ function($row, $index) use ($condition) {
463
469
}
464
470
465
471
/**
472
+ * It is used internally to filter rows unsing a specific condition
466
473
* @param mixed $row
467
474
* @param mixed $index
468
- * @param array $condition
475
+ * @param array $condition as text:['col'=>'eq;2']<br/>
476
+ * as array:['col'=>['eq',2]]</br>
469
477
* @return bool
470
478
* @noinspection PhpUnusedParameterInspection
471
479
*/
@@ -477,18 +485,35 @@ protected function filterCondition($row, $index, array $condition): bool
477
485
$ fail = false ;
478
486
foreach ($ row as $ k => $ r ) {
479
487
if (isset ($ condition [$ k ])) {
480
- $ vparts = explode ('| ' , $ condition [$ k ]);
481
- foreach ($ vparts as $ vpart ) {
482
- $ fragment = explode ('; ' , $ vpart , 3 );
483
- $ type = $ fragment [0 ];
484
- $ compValue = $ fragment [1 ] ?? null ;
485
- if ($ compValue !==null && strpos ($ compValue , ', ' ) !== false ) {
486
- $ compValue = explode (', ' , $ compValue );
488
+ if (is_array ($ condition [$ k ])) {
489
+ if (is_array ($ condition [$ k ][0 ])) {
490
+ $ conds = $ condition [$ k ]; // [['eq',2],['eq',3]]
491
+ } else {
492
+ $ conds =[$ condition [$ k ]]; // // ['eq',2] =>[['eq',2']]
487
493
}
488
- $ msg = '' ;
489
- $ this ->runCondition ($ r , $ compValue , $ type , $ fail , $ msg );
490
- if ($ fail ) {
491
- break 2 ;
494
+ foreach ($ conds as $ cond ) {
495
+ $ compValue = $ cond [1 ] ?? null ;
496
+ $ type = $ cond [0 ] ?? '' ;
497
+ $ msg = '' ;
498
+ $ this ->runCondition ($ r , $ compValue , $ type , $ fail , $ msg );
499
+ if ($ fail ) {
500
+ break 2 ;
501
+ }
502
+ }
503
+ } else {
504
+ $ vparts = explode ('| ' , $ condition [$ k ]);
505
+ foreach ($ vparts as $ vpart ) {
506
+ $ fragment = explode ('; ' , $ vpart , 3 );
507
+ $ type = $ fragment [0 ];
508
+ $ compValue = $ fragment [1 ] ?? null ;
509
+ if ($ compValue !==null && strpos ($ compValue , ', ' ) !== false ) {
510
+ $ compValue = explode (', ' , $ compValue );
511
+ }
512
+ $ msg = '' ;
513
+ $ this ->runCondition ($ r , $ compValue , $ type , $ fail , $ msg );
514
+ if ($ fail ) {
515
+ break 2 ;
516
+ }
492
517
}
493
518
}
494
519
}
@@ -528,10 +553,11 @@ public function flat(): ArrayOne
528
553
return $ this ;
529
554
}
530
555
556
+
531
557
/** @noinspection TypeUnsafeArraySearchInspection
532
558
* @noinspection TypeUnsafeComparisonInspection
533
559
*/
534
- private function runCondition ($ r , $ compareValue , $ compareType , bool &$ fail , ?string &$ genMsg ): void
560
+ private function runCondition ($ r , $ compareValue ,string $ compareType , bool &$ fail , ?string &$ genMsg ): void
535
561
{
536
562
if (strpos ($ compareType , 'f: ' ) === 0 ) {
537
563
if ($ this ->serviceObject === null ) {
@@ -658,6 +684,7 @@ private function runCondition($r, $compareValue, $compareType, bool &$fail, ?str
658
684
break ;
659
685
case 'eq ' :
660
686
case '== ' :
687
+
661
688
if (is_array ($ compareValue )) {
662
689
/** @noinspection TypeUnsafeArraySearchInspection */
663
690
if (in_array ($ r , $ compareValue ) === $ negation ) {
0 commit comments