From 2b981aa36cbd675569fb8dcafc2a61b9d89638fb Mon Sep 17 00:00:00 2001 From: jorgecc Date: Sat, 2 Mar 2024 09:03:36 -0300 Subject: [PATCH] 1.12 --- README.md | 3 +++ composer.json | 2 +- examples/{ex1.php => example1.php} | 20 ++++++++++++-------- examples/example2.php | 14 +++++++++++--- examples/exampleFindandFilter.php | 17 ++++++++++------- examples/libexample.php | 7 +++++++ src/ArrayOne.php | 16 ++++++++++------ 7 files changed, 54 insertions(+), 25 deletions(-) rename examples/{ex1.php => example1.php} (70%) create mode 100644 examples/libexample.php diff --git a/README.md b/README.md index 4047a71..00268b3 100644 --- a/README.md +++ b/README.md @@ -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() @@ -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. diff --git a/composer.json b/composer.json index 1b226a4..1675498 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "license": "LGPL-3.0-only", "require": { - "php": ">=7.2.5", + "php": ">=7.4", "ext-ctype": "*", "ext-json": "*" }, diff --git a/examples/ex1.php b/examples/example1.php similarity index 70% rename from examples/ex1.php rename to examples/example1.php index eaa772e..8c3055f 100644 --- a/examples/ex1.php +++ b/examples/example1.php @@ -3,6 +3,7 @@ use eftec\ArrayOne; include __DIR__.'/../vendor/autoload.php'; +include __DIR__.'/libexample.php'; $array=[1,2,3 ,'products'=>[ @@ -19,11 +20,14 @@ ->removeCol(['price']) ->col('name') ->last() - ->current(); - + ->getCurrent(); +echo "

Example1 array:

"; +var_dump2($array); +echo "

nav to product, creating a new column, filtering, removing column price, returning a column and getting the last value

"; var_dump($r); -var_dump(ArrayOne::$error); - +echo "

Showing errors

"; +var_dump2(ArrayOne::$error); +echo "

reducing products

"; $r=ArrayOne::set($array) ->nav('products') ->reduce(function($row,$index,$previous) { @@ -31,13 +35,13 @@ 'quantity'=>$previous['quantity']+$row['quantity'],'counter'=>@$previous['counter']+1]; }) ->all(); -var_dump('--------'); -var_dump($r); +var_dump2($r); +echo "

reducing products

"; $r=ArrayOne::set($array) ->nav('products') ->reduce(['price'=>'sum','quantity'=>'sum']) ->all(); -var_dump('--------'); -var_dump($r); + +var_dump2($r); diff --git a/examples/example2.php b/examples/example2.php index 0d652da..e626376 100644 --- a/examples/example2.php +++ b/examples/example2.php @@ -59,8 +59,16 @@ use eftec\ArrayOne; + include __DIR__.'/../vendor/autoload.php'; +include __DIR__.'/libexample.php'; + +echo "

Test2 array

"; +var_dump2($top); +echo "

navigation to topping

"; +$result= ArrayOne::set($top)->nav('topping')->getCurrent(); +var_dump2($result); +echo "

And reducing the values (counting)

"; $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); + diff --git a/examples/exampleFindandFilter.php b/examples/exampleFindandFilter.php index 3f90d42..b46eb64 100644 --- a/examples/exampleFindandFilter.php +++ b/examples/exampleFindandFilter.php @@ -1,10 +1,11 @@ -1,"product"=>"apple","type"=>"fruit",'price'=>100], @@ -14,19 +15,21 @@ ["id"=>5,"product"=>"fanta","type"=>"drink",'price'=>200], ]; echo "

Returns true if it is an indexed table

"; -var_dump(ArrayOne::isIndexTableArray($values)); +var_dump2(ArrayOne::isIndexTableArray($values)); echo "

returning all the fruits with price greater or equals than 200

"; -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 "

returning all the fruits with price greater or equals than 200 using a function

"; -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 "

returning all the fruits with price greater or equals than 200 using a function (lambda)

"; +var_dump2(ArrayOne::set($values)->filter(fn($row) => $row['type']==='fruit' && $row['price']>=200)->getCurrent()); echo "

returning all the fruits and drink with price greater or equals than 200

"; -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 "

returning all the indexes where the condition is located

"; -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 "

return the first value who matches the condition

"; -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()); diff --git a/examples/libexample.php b/examples/libexample.php new file mode 100644 index 0000000..f7ac1d3 --- /dev/null +++ b/examples/libexample.php @@ -0,0 +1,7 @@ +"; + var_export($value); + echo ""; +} diff --git a/src/ArrayOne.php b/src/ArrayOne.php index 22cf3af..11433d9 100644 --- a/src/ArrayOne.php +++ b/src/ArrayOne.php @@ -7,6 +7,7 @@ use ArrayAccess; use Closure; use Exception; +use JsonException; use RuntimeException; /** @@ -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
@@ -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); } @@ -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: