Skip to content

Commit

Permalink
1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecc-business-account committed Mar 2, 2024
1 parent 547f9aa commit 2b981aa
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 25 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@ $this->makeRequestArrayByExample(['a'=1,'b'=>2]); // ['a'='post','b'=>'post'];


## versions
* 1.12 2024-03-01
* Updating dependency to PHP 7.4. The extended support of PHP 7.2 ended 3 years ago.
* 1.11 2024-03-01
* added method find()
* aedded method isIndexArray() and isIndexTableArray()
Expand Down Expand Up @@ -733,3 +735,4 @@ Licensed under dual license: LGPL-3.0 and commercial license.
In short:
- [x] Can I use in a close source application for free? Yes if you don't modify this library.
- [x] If you modify it, then you must share the source code.
- [x] If you want to modify privately, then you must buy a license.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"license": "LGPL-3.0-only",
"require": {
"php": ">=7.2.5",
"php": ">=7.4",
"ext-ctype": "*",
"ext-json": "*"
},
Expand Down
20 changes: 12 additions & 8 deletions examples/ex1.php → examples/example1.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use eftec\ArrayOne;

include __DIR__.'/../vendor/autoload.php';
include __DIR__.'/libexample.php';

$array=[1,2,3
,'products'=>[
Expand All @@ -19,25 +20,28 @@
->removeCol(['price'])
->col('name')
->last()
->current();

->getCurrent();
echo "<h1>Example1 array:</h1>";
var_dump2($array);
echo "<h1>nav to product, creating a new column, filtering, removing column price, returning a column and getting the last value</h1>";
var_dump($r);
var_dump(ArrayOne::$error);

echo "<h1>Showing errors</h1>";
var_dump2(ArrayOne::$error);
echo "<h1>reducing products</h1>";
$r=ArrayOne::set($array)
->nav('products')
->reduce(function($row,$index,$previous) {
return ['price'=>$previous['price']+$row['price'],
'quantity'=>$previous['quantity']+$row['quantity'],'counter'=>@$previous['counter']+1];
})
->all();
var_dump('--------');
var_dump($r);

var_dump2($r);
echo "<h1>reducing products</h1>";
$r=ArrayOne::set($array)
->nav('products')
->reduce(['price'=>'sum','quantity'=>'sum'])
->all();
var_dump('--------');
var_dump($r);

var_dump2($r);

14 changes: 11 additions & 3 deletions examples/example2.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,16 @@

use eftec\ArrayOne;


include __DIR__.'/../vendor/autoload.php';
include __DIR__.'/libexample.php';

echo "<h1>Test2 array</h1>";
var_dump2($top);
echo "<h1>navigation to topping</h1>";
$result= ArrayOne::set($top)->nav('topping')->getCurrent();
var_dump2($result);
echo "<h1>And reducing the values (counting)</h1>";
$result= (new ArrayOne($top))->nav('topping')->reduce(['id'=>'count'])->getCurrent();
var_dump($result);
$result= ArrayOne::set($top)->nav('topping')->reduce(['id'=>'count'])->getCurrent();
var_dump($result);
var_dump2($result);

17 changes: 10 additions & 7 deletions examples/exampleFindandFilter.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php /** @noinspection ForgottenDebugOutputInspection
<?php /**
* @noinspection UnknownInspectionInspection
*/

use eftec\ArrayOne;

include __DIR__.'/../vendor/autoload.php';
include __DIR__.'/libexample.php';

$values=[
["id"=>1,"product"=>"apple","type"=>"fruit",'price'=>100],
Expand All @@ -14,19 +15,21 @@
["id"=>5,"product"=>"fanta","type"=>"drink",'price'=>200],
];
echo "<h1>Returns true if it is an indexed table</h1>";
var_dump(ArrayOne::isIndexTableArray($values));
var_dump2(ArrayOne::isIndexTableArray($values));

echo "<h1>returning all the fruits with price greater or equals than 200</h1>";
var_dump(ArrayOne::set($values)->filter([['type'=>'eq;fruit'],['price'=>'ge;200']])->getCurrent());
var_dump2(ArrayOne::set($values)->filter([['type'=>'eq;fruit'],['price'=>'ge;200']])->getCurrent());
echo "<h1>returning all the fruits with price greater or equals than 200 using a function<h1></h1>";
var_dump(ArrayOne::set($values)->filter(static function($row) {
var_dump2(ArrayOne::set($values)->filter(static function($row) {
return $row['type']==='fruit' && $row['price']>=200;
})->getCurrent());
echo "<h1>returning all the fruits with price greater or equals than 200 using a function (lambda)<h1></h1>";
var_dump2(ArrayOne::set($values)->filter(fn($row) => $row['type']==='fruit' && $row['price']>=200)->getCurrent());

echo "<h1>returning all the fruits and drink with price greater or equals than 200</h1>";
var_dump(ArrayOne::set($values)->filter([['type'=>'in;fruit,drink'],['price'=>'ge;200']])->getCurrent());
var_dump2(ArrayOne::set($values)->filter([['type'=>'in;fruit,drink'],['price'=>'ge;200']])->getCurrent());

echo "<h1>returning all the indexes where the condition is located</h1>";
var_dump(ArrayOne::set($values)->find([['type'=>'eq;fruit'],['price'=>'ge;200']],false,'key')->getCurrent());
var_dump2(ArrayOne::set($values)->find([['type'=>'eq;fruit'],['price'=>'ge;200']],false,'key')->getCurrent());
echo "<h1>return the first value who matches the condition</h1>";
var_dump(ArrayOne::set($values)->find([['type'=>'eq;fruit'],['price'=>'ge;200']],true,'value')->getCurrent());
var_dump2(ArrayOne::set($values)->find([['type'=>'eq;fruit'],['price'=>'ge;200']],true,'value')->getCurrent());
7 changes: 7 additions & 0 deletions examples/libexample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/** @noinspection ForgottenDebugOutputInspection */
function var_dump2($value) {
echo "<pre>";
var_export($value);
echo "</pre>";
}
16 changes: 10 additions & 6 deletions src/ArrayOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ArrayAccess;
use Closure;
use Exception;
use JsonException;
use RuntimeException;

/**
Expand All @@ -16,15 +17,16 @@
*/
class ArrayOne implements ArrayAccess
{
public const VERSION = "1.11";
public const VERSION = "1.12";
/** @var array|null */
protected $array;
protected $serviceObject;
protected ?array $array;
protected ?object $serviceObject;
/** @var mixed */
protected $currentArray;
/** @var mixed */
protected $curNav;
public static $error = '';
public $errorStack = [];
public static string $error = '';
public array $errorStack = [];

/**
* Constructor<br/>
Expand Down Expand Up @@ -167,10 +169,11 @@ public static function setRequestRec(&$req, $defaultValueAll, $separator, $prefi
* ```
* @param string $json
* @return ArrayOne
* @throws JsonException
*/
public static function setJson(string $json): ArrayOne
{
$json = json_decode($json, true);
$json = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
return self::set($json);
}

Expand Down Expand Up @@ -488,6 +491,7 @@ public function join(?array $arrayToJoin, $column1, $column2): ArrayOne
* // get the row #2 "argentina":
* // using a function:
* $r = ArrayOne::set($array)->filter(function($row, $id) {return $row['id'] === 2;}, true)->result();
* $r = ArrayOne::set($array)->filter(fn($row, $id) => $row['id'] === 2, true)->result();
* // using a function a returning a flat result:
* $r = ArrayOne::set($array)->filter(function($row, $id) {return $row['id'] === 2;}, false)->result();
* // using an associative array:
Expand Down

0 comments on commit 2b981aa

Please sign in to comment.