Skip to content

Commit 658859b

Browse files
1.5
1 parent 74f8aca commit 658859b

File tree

4 files changed

+58
-33
lines changed

4 files changed

+58
-33
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,8 @@ $this->makeRequestArrayByExample(['a'=1,'b'=>2]); // ['a'='post','b'=>'post'];
577577

578578

579579
## versions
580+
* 1.5 2023-04-07
581+
* [new] filtercondition() now allow conditions as array.
580582
* 1.4 2023-04-05
581583
* [fix] filtercondition() fixed a warning when the value is null.
582584
* [new] group() now allow to stack elements

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
"homepage": "https://github.com/EFTEC/"
1616
}
1717
],
18-
"minimum-stability": "beta",
1918
"license": "LGPL-3.0-only",
2019
"require": {
2120
"php": ">=7.2.5",
2221
"ext-ctype": "*",
2322
"ext-json": "*"
2423
},
2524
"require-dev": {
26-
"phpunit/phpunit": "^9.5.13"
25+
"phpunit/phpunit": "^8.5.13"
2726
},
2827
"autoload": {
2928
"psr-4": {

phpunit.xml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
<phpunit backupGlobals="false"
2-
backupStaticAttributes="false"
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
colors="true"
44
verbose="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
processIsolation="false"
9-
stopOnFailure="false"
10-
bootstrap="test/bootstrap.php">
11-
<testsuites>
12-
<testsuite name="ArrayOne Test Suite">
13-
<directory suffix="Test.php">./test</directory>
14-
</testsuite>
15-
</testsuites>
5+
stopOnFailure="true"
6+
bootstrap="tests/bootstrap.php"
7+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd">
168
<filter>
179
<whitelist processUncoveredFilesFromWhitelist="true">
1810
<directory suffix=".php">./lib</directory>
1911
</whitelist>
2012
</filter>
13+
<testsuites>
14+
<testsuite name="ArrayOne Test Suite">
15+
<directory>./test</directory>
16+
</testsuite>
17+
</testsuites>
2118
</phpunit>

src/ArrayOne.php

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
class ArrayOne
1515
{
16-
public const VERSION = "1.4";
16+
public const VERSION = "1.5";
1717
/** @var array|null */
1818
protected $array;
1919
protected $serviceObject;
@@ -426,17 +426,23 @@ public function join(?array $arrayToJoin, $column1, $column2): ArrayOne
426426

427427
/**
428428
* 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>
430430
* <b>Example:</b><br/>
431431
* <pre>
432432
* $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:
434435
* $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:
436437
* $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();
437442
* </pre>
438443
* @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>
440446
* @param bool $flat
441447
* @return $this
442448
* @see ArrayOne::validate to see the definition of comparison array
@@ -463,9 +469,11 @@ function($row, $index) use ($condition) {
463469
}
464470

465471
/**
472+
* It is used internally to filter rows unsing a specific condition
466473
* @param mixed $row
467474
* @param mixed $index
468-
* @param array $condition
475+
* @param array $condition as text:['col'=>'eq;2']<br/>
476+
* as array:['col'=>['eq',2]]</br>
469477
* @return bool
470478
* @noinspection PhpUnusedParameterInspection
471479
*/
@@ -477,18 +485,35 @@ protected function filterCondition($row, $index, array $condition): bool
477485
$fail = false;
478486
foreach ($row as $k => $r) {
479487
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']]
487493
}
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+
}
492517
}
493518
}
494519
}
@@ -528,10 +553,11 @@ public function flat(): ArrayOne
528553
return $this;
529554
}
530555

556+
531557
/** @noinspection TypeUnsafeArraySearchInspection
532558
* @noinspection TypeUnsafeComparisonInspection
533559
*/
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
535561
{
536562
if (strpos($compareType, 'f:') === 0) {
537563
if ($this->serviceObject === null) {
@@ -658,6 +684,7 @@ private function runCondition($r, $compareValue, $compareType, bool &$fail, ?str
658684
break;
659685
case 'eq':
660686
case '==':
687+
661688
if (is_array($compareValue)) {
662689
/** @noinspection TypeUnsafeArraySearchInspection */
663690
if (in_array($r, $compareValue) === $negation) {

0 commit comments

Comments
 (0)