From 5c17e6200472737cd1eb00b6b31d806b510e3cce Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Fri, 23 Dec 2022 18:01:13 +0400 Subject: [PATCH 01/10] * `\ddTools::sort2dArray`: Refactoring. --- modx.ddtools.class.php | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/modx.ddtools.class.php b/modx.ddtools.class.php index 95a8703..3b86983 100644 --- a/modx.ddtools.class.php +++ b/modx.ddtools.class.php @@ -288,7 +288,7 @@ public static function explodeAssoc( /** * sort2dArray - * @version 1.2.1 (2021-03-09) + * @version 1.2.2 (2022-12-23) * * @desc Sorts 2-dimensional array by multiple columns (like in SQL) using Hoare's method, also referred to as quicksort. The sorting is stable. * @@ -306,8 +306,8 @@ public static function sort2dArray( $i = 0 ){ //В качестве эталона получаем сортируемое значение (по первому условию сортировки) первого элемента - $currentRow = array_values($array)[0][$sortBy[$i]]; - $isCurrentRowNumeric = is_numeric($currentRow); + $currentItem_comparisonValue = array_values($array)[0][$sortBy[$i]]; + $isCurrentItemComparisonValueNumeric = is_numeric($currentItem_comparisonValue); $isArrayAssociative = count(array_filter( @@ -324,20 +324,22 @@ public static function sort2dArray( //Перебираем массив foreach ( $array as - $rowKey => - $rowValue + $arrayItemKey => + $arrayItem ){ + $arrayItem_comparisonValue = $arrayItem[$sortBy[$i]]; + //Если эталон и текущее значение — числа if ( - $isCurrentRowNumeric && - is_numeric($rowValue[$sortBy[$i]]) + $isCurrentItemComparisonValueNumeric && + is_numeric($arrayItem_comparisonValue) ){ //Получаем нужную циферку $cmpRes = - $rowValue[$sortBy[$i]] == $currentRow ? + $arrayItem_comparisonValue == $currentItem_comparisonValue ? 0 : ( - $rowValue[$sortBy[$i]] > $currentRow ? + $arrayItem_comparisonValue > $currentItem_comparisonValue ? 1 : -1 ) @@ -346,8 +348,8 @@ public static function sort2dArray( }else{ //Сравниваем текущее значение со значением эталонного $cmpRes = strcmp( - $rowValue[$sortBy[$i]], - $currentRow + $arrayItem_comparisonValue, + $currentItem_comparisonValue ); } @@ -363,9 +365,9 @@ public static function sort2dArray( } if ($isArrayAssociative){ - $resultArray[$rowKey] = $rowValue; + $resultArray[$arrayItemKey] = $arrayItem; }else{ - $resultArray[] = $rowValue; + $resultArray[] = $arrayItem; } } From e2aacccf3c735c04ec5f90db78a7c27d0cc4b382 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Fri, 23 Dec 2022 18:04:12 +0400 Subject: [PATCH 02/10] =?UTF-8?q?+=20`\ddTools::sort2dArray`=20=E2=86=92?= =?UTF-8?q?=20Parameters=20=E2=86=92=C2=A0`$array[$i]`:=20Can=20also=20be?= =?UTF-8?q?=20set=20as=20object.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modx.ddtools.class.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/modx.ddtools.class.php b/modx.ddtools.class.php index 3b86983..0ea7496 100644 --- a/modx.ddtools.class.php +++ b/modx.ddtools.class.php @@ -288,11 +288,12 @@ public static function explodeAssoc( /** * sort2dArray - * @version 1.2.2 (2022-12-23) + * @version 1.3 (2022-12-23) * * @desc Sorts 2-dimensional array by multiple columns (like in SQL) using Hoare's method, also referred to as quicksort. The sorting is stable. * * @param $array {array} — Array to sort. Associative arrays are also supported. @required + * @param $array[$i] {array|object} — Array to sort. Associative arrays are also supported. @required * @param $sortBy {array} — Columns (second level keys) by which the array is sorted. @required * @param $sortDir {1|-1} — Sort direction (1 == ASC; -1 == DESC). Default: 1. * @param $i {integer} — Count, an internal variable used during recursive calls. Default: 0. @@ -306,7 +307,10 @@ public static function sort2dArray( $i = 0 ){ //В качестве эталона получаем сортируемое значение (по первому условию сортировки) первого элемента - $currentItem_comparisonValue = array_values($array)[0][$sortBy[$i]]; + $currentItem_comparisonValue = \DDTools\ObjectTools::getPropValue([ + 'object' => array_values($array)[0], + 'propName' => $sortBy[$i] + ]); $isCurrentItemComparisonValueNumeric = is_numeric($currentItem_comparisonValue); $isArrayAssociative = @@ -327,7 +331,10 @@ public static function sort2dArray( $arrayItemKey => $arrayItem ){ - $arrayItem_comparisonValue = $arrayItem[$sortBy[$i]]; + $arrayItem_comparisonValue = \DDTools\ObjectTools::getPropValue([ + 'object' => $arrayItem, + 'propName' => $sortBy[$i] + ]); //Если эталон и текущее значение — числа if ( From 3ed0117ce4fafc9727e790a834e7d4b1bf864793 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 26 Dec 2022 18:12:41 +0400 Subject: [PATCH 03/10] * `\DDTools\BaseClass`: The class has become abstract. --- README.md | 2 +- src/BaseClass/BaseClass.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2ca61c5..774d4be 100644 --- a/README.md +++ b/README.md @@ -762,7 +762,7 @@ Deletes required items from collection. ### `\DDTools\BaseClass` -Simple class with some small methods facilitating your work. +Simple abstract class with some small methods facilitating your work. It is convenient to inherit your classes from this. You can see an example of how it works in the [(MODX)EvolutionCMS.snippets.ddGetDocumentField](https://code.divandesign.biz/modx/ddgetdocumentfield) code. diff --git a/src/BaseClass/BaseClass.php b/src/BaseClass/BaseClass.php index ffe4a49..f53410f 100644 --- a/src/BaseClass/BaseClass.php +++ b/src/BaseClass/BaseClass.php @@ -1,7 +1,7 @@ Date: Mon, 26 Dec 2022 19:18:20 +0400 Subject: [PATCH 04/10] * `\DDTools\Base\Base`: The class has been renamed from `\DDTools\BaseClass`. Backward compatibility is maintained (you can still use `\DDTools\BaseClass`, but it is not recommended). --- README.md | 14 +++++++------- require.php | 4 +++- src/{BaseClass/BaseClass.php => Base/Base.php} | 4 ++-- src/Base/BaseClass.php | 5 +++++ 4 files changed, 17 insertions(+), 10 deletions(-) rename src/{BaseClass/BaseClass.php => Base/Base.php} (99%) create mode 100644 src/Base/BaseClass.php diff --git a/README.md b/README.md index 774d4be..5a20ae6 100644 --- a/README.md +++ b/README.md @@ -760,7 +760,7 @@ Deletes required items from collection. * Default value: `0` -### `\DDTools\BaseClass` +### `\DDTools\Base\Base` Simple abstract class with some small methods facilitating your work. It is convenient to inherit your classes from this. @@ -768,7 +768,7 @@ It is convenient to inherit your classes from this. You can see an example of how it works in the [(MODX)EvolutionCMS.snippets.ddGetDocumentField](https://code.divandesign.biz/modx/ddgetdocumentfield) code. -#### `\DDTools\BaseClass::setExistingProps($props)` +#### `\DDTools\Base\Base::setExistingProps($props)` Sets existing object properties. @@ -791,7 +791,7 @@ Sets existing object properties. * **Required** -#### `\DDTools\BaseClass::toArray()` +#### `\DDTools\Base\Base::toArray()` Returns all properties of this object as an associative array independent of their visibility. @@ -808,7 +808,7 @@ Returns all properties of this object as an associative array independent of the * Valid values: `mixed` -#### `\DDTools\BaseClass::toJSON()` +#### `\DDTools\Base\Base::toJSON()` Returns all properties of this object as an JSON string independent of their visibility. @@ -825,12 +825,12 @@ Returns all properties of this object as an JSON string independent of their vis * Valid values: `mixed` -#### `\DDTools\BaseClass::__toString()` +#### `\DDTools\Base\Base::__toString()` -The same as `\DDTools\BaseClass::toJSON()`. +The same as `\DDTools\Base\Base::toJSON()`. -#### `\DDTools\BaseClass::createChildInstance($params)` +#### `\DDTools\Base\Base::createChildInstance($params)` * `$params` * Desctription: Parameters, the pass-by-name style is used. diff --git a/require.php b/require.php index ba46219..4e2a380 100644 --- a/require.php +++ b/require.php @@ -1,5 +1,7 @@ Date: Mon, 26 Dec 2022 22:07:52 +0400 Subject: [PATCH 05/10] + `\DDTools\Base\AncestorTrait`: The new trait. - `\DDTools\Base\Base::createChildInstance`: The method has been removed, use `\DDTools\Base\AncestorTrait::createChildInstance` instead. Backward compatibility is maintained (you can still use `\DDTools\BaseClass::createChildInstance`, but it is not recommended). --- README.md | 9 ++++- require.php | 1 + src/Base/AncestorTrait.php | 77 ++++++++++++++++++++++++++++++++++++++ src/Base/Base.php | 73 ------------------------------------ src/Base/BaseClass.php | 4 +- 5 files changed, 89 insertions(+), 75 deletions(-) create mode 100644 src/Base/AncestorTrait.php diff --git a/README.md b/README.md index 5a20ae6..7ebffd8 100644 --- a/README.md +++ b/README.md @@ -830,7 +830,14 @@ Returns all properties of this object as an JSON string independent of their vis The same as `\DDTools\Base\Base::toJSON()`. -#### `\DDTools\Base\Base::createChildInstance($params)` +### `\DDTools\Base\AncestorTrait` + +Simple trait for ancestors with some small methods facilitating your work. + +You can see an example of how it works in the [(MODX)EvolutionCMS.snippets.ddGetDocumentField](https://code.divandesign.biz/modx/ddgetdocumentfield) code. + + +#### `\DDTools\Base\AncestorTrait::createChildInstance($params)` * `$params` * Desctription: Parameters, the pass-by-name style is used. diff --git a/require.php b/require.php index 4e2a380..c64e503 100644 --- a/require.php +++ b/require.php @@ -1,5 +1,6 @@ [], + 'capitalizeName' => true + ], + (array) $params + ); + + $thisClassName = get_called_class(); + + $thisNameSpace = substr( + $thisClassName, + 0, + strrpos( + $thisClassName, + '\\' + ) + ); + + //Current classname without namespace + $thisClassName = substr( + $thisClassName, + strrpos( + $thisClassName, + '\\' + ) + 1 + ); + + //Capitalize child name if needed + if ($params->capitalizeName){ + $params->name = ucfirst(strtolower($params->name)); + } + + $filePath = + $params->parentDir . + DIRECTORY_SEPARATOR . + $params->name . + DIRECTORY_SEPARATOR . + $thisClassName . + '.php' + ; + + if(is_file($filePath)){ + require_once($filePath); + + $objectClass = + '\\' . + $thisNameSpace . + '\\' . + $params->name . + '\\' . + $thisClassName + ; + + return new $objectClass($params->params); + }else{ + throw new \Exception( + $thisClassName . ' “' . $params->name . '” not found.', + 500 + ); + } + } +} \ No newline at end of file diff --git a/src/Base/Base.php b/src/Base/Base.php index a33796e..de251f3 100644 --- a/src/Base/Base.php +++ b/src/Base/Base.php @@ -131,79 +131,6 @@ private function ddGetPropValues($class = null){ return $result; } - /** - * createChildInstance - * @version 1.1.1 (2019-08-22) - * - * @see README.md - * - * @throws \Exception - */ - public static final function createChildInstance($params){ - //Defaults - $params = (object) array_merge( - [ - 'params' => [], - 'capitalizeName' => true - ], - (array) $params - ); - - $thisClassName = get_called_class(); - - $thisNameSpace = substr( - $thisClassName, - 0, - strrpos( - $thisClassName, - '\\' - ) - ); - - //Current classname without namespace - $thisClassName = substr( - $thisClassName, - strrpos( - $thisClassName, - '\\' - ) + 1 - ); - - //Capitalize child name if needed - if ($params->capitalizeName){ - $params->name = ucfirst(strtolower($params->name)); - } - - $filePath = - $params->parentDir . - DIRECTORY_SEPARATOR . - $params->name . - DIRECTORY_SEPARATOR . - $thisClassName . - '.php' - ; - - if(is_file($filePath)){ - require_once($filePath); - - $objectClass = - '\\' . - $thisNameSpace . - '\\' . - $params->name . - '\\' . - $thisClassName - ; - - return new $objectClass($params->params); - }else{ - throw new \Exception( - $thisClassName . ' “' . $params->name . '” not found.', - 500 - ); - } - } - /** * toArray * @version 1.0 (2020-05-06) diff --git a/src/Base/BaseClass.php b/src/Base/BaseClass.php index 1a9ac18..b41feb3 100644 --- a/src/Base/BaseClass.php +++ b/src/Base/BaseClass.php @@ -2,4 +2,6 @@ namespace DDTools; //Backward compatibility -abstract class BaseClass extends \DDTools\Base\Base {} \ No newline at end of file +abstract class BaseClass extends \DDTools\Base\Base { + use \DDTools\Base\AncestorTrait; +} \ No newline at end of file From 1ed9a1aebcec295cd22eb5f38c186f8ee5565df3 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 26 Dec 2022 23:39:53 +0400 Subject: [PATCH 06/10] + `\DDTools\Base\Base::toJSON`: Returns JSON-array if `$this->toArray` returns indexed array. --- README.md | 4 +++- src/Base/Base.php | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7ebffd8..0429d6b 100644 --- a/README.md +++ b/README.md @@ -818,7 +818,9 @@ Returns all properties of this object as an JSON string independent of their vis * `$result` * Desctription: An JSON string representation of this object. The method returns all existing properties: public, private and protected. - * Valid values: `stringJsonObject` + * Valid values: + * `stringJsonObject` + * `stringJsonArray` — if `$this->toArray` returns indexed array * `$result->{$propName}` * Desctription: The key is the object field name and the value is the object field value. diff --git a/src/Base/Base.php b/src/Base/Base.php index de251f3..8f60c05 100644 --- a/src/Base/Base.php +++ b/src/Base/Base.php @@ -143,7 +143,7 @@ public function toArray(){ /** * toJSON - * @version 1.0.1 (2021-03-10) + * @version 1.1 (2022-12-26) * * @see README.md * @@ -152,7 +152,7 @@ public function toArray(){ public function toJSON(){ return \DDTools\ObjectTools::convertType([ 'object' => $this->toArray(), - 'type' => 'stringJsonObject' + 'type' => 'stringJsonAuto' ]); } From e154d7cc25592e2b96c1b01075f0e92df697d0dc Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Tue, 27 Dec 2022 00:43:27 +0400 Subject: [PATCH 07/10] + `\DDTools\ObjectCollection::toJSON()`, `\DDTools\ObjectCollection::__toString()`: The new methods. Get an JSON-array of all collection items. --- README.md | 12 ++++++++++++ src/ObjectCollection/ObjectCollection.php | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0429d6b..a768871 100644 --- a/README.md +++ b/README.md @@ -760,6 +760,18 @@ Deletes required items from collection. * Default value: `0` +#### `\DDTools\ObjectCollection::toJSON()`, `\DDTools\ObjectCollection::__toString()` + +Gets an JSON-array of all collection items. + + +##### Returns + +* `$result` + * Desctription: An JSON-array of items. + * Valid values: `stringJsonArray` + + ### `\DDTools\Base\Base` Simple abstract class with some small methods facilitating your work. diff --git a/src/ObjectCollection/ObjectCollection.php b/src/ObjectCollection/ObjectCollection.php index 12498ec..e3b4071 100644 --- a/src/ObjectCollection/ObjectCollection.php +++ b/src/ObjectCollection/ObjectCollection.php @@ -1,7 +1,7 @@ items); } + + /** + * toArray + * @version 1.0 (2022-12-26) + * + * @desc Simple implementation of $this->getItems. Used for correct working of $this->toJSON, $this->__toString. + */ + public function toArray(){ + return $this->items; + } } ?> \ No newline at end of file From 194393eddb013dfdd28dc8706dd5320b55ed9983 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Wed, 28 Dec 2022 13:14:59 +0400 Subject: [PATCH 08/10] =?UTF-8?q?*=20`\DDTools\ObjectCollection::isItemMat?= =?UTF-8?q?chFilter`=20=E2=86=92=20Parameters=20=E2=86=92=C2=A0`$params->i?= =?UTF-8?q?temObject`:=20Has=20been=20renamed=20from=20`$params->item`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ObjectCollection/ObjectCollection.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ObjectCollection/ObjectCollection.php b/src/ObjectCollection/ObjectCollection.php index e3b4071..157bd74 100644 --- a/src/ObjectCollection/ObjectCollection.php +++ b/src/ObjectCollection/ObjectCollection.php @@ -90,7 +90,7 @@ public function addItems($params = []){ /** * convertItemsType - * @version 1.0 (2021-12-02) + * @version 1.0.1 (2022-12-28) * * @see README.md */ @@ -119,7 +119,7 @@ public function convertItemsType($params = []){ if ( //If item is matched to filter $this->isItemMatchFilter([ - 'item' => $itemObject, + 'itemObject' => $itemObject, 'filter' => $params->filter ]) ){ @@ -133,7 +133,7 @@ public function convertItemsType($params = []){ /** * updateItems - * @version 1.0 (2021-12-02) + * @version 1.0.1 (2022-12-28) * * @see README.md */ @@ -165,7 +165,7 @@ public function updateItems($params = []){ if ( //If item is matched to filter $this->isItemMatchFilter([ - 'item' => $itemObject, + 'itemObject' => $itemObject, 'filter' => $params->filter ]) ){ @@ -190,7 +190,7 @@ public function updateItems($params = []){ /** * getItems - * @version 2.0 (2021-12-02) + * @version 2.0.1 (2022-12-28) * * @see README.md */ @@ -224,7 +224,7 @@ public function getItems($params = []){ if ( //If item is matched to filter $this->isItemMatchFilter([ - 'item' => $itemObject, + 'itemObject' => $itemObject, 'filter' => $params->filter ]) ){ @@ -301,7 +301,7 @@ public function getOneItem($params = []){ /** * deleteItems - * @version 1.0 (2021-12-02) + * @version 1.0.1 (2022-12-28) * * @see README.md */ @@ -332,7 +332,7 @@ public function deleteItems($params = []){ if ( //If item is matched to filter $this->isItemMatchFilter([ - 'item' => $itemObject, + 'itemObject' => $itemObject, 'filter' => $params->filter ]) ){ @@ -356,10 +356,10 @@ public function deleteItems($params = []){ /** * isItemMatchFilter - * @version 1.0 (2021-12-01) + * @version 2.0 (2022-12-28) * * @param $params {array} — Parameters, the pass-by-name style is used. @required - * @param $params->item {array|object} — An item to test. @required + * @param $params->itemObject {array|object} — An item to test. @required * @param $params->filter {array} — Result of $this->prepareItemsFilter. @required * * @return $result {boolean} From 4fa8ef9295691baacf24d16a5c86fb29851b0d80 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Wed, 28 Dec 2022 15:41:36 +0400 Subject: [PATCH 09/10] + `\DDTools\ObjectCollection::setOneItemData`, `getOneItemData`: The new protected methods. All setting/getting of an item data inside the class must use these methods. Its convenient to override these methods in child classes if items are not plain objects. --- README.md | 54 ++++++++++++++ src/ObjectCollection/ObjectCollection.php | 89 ++++++++++++++++++----- 2 files changed, 125 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a768871..308ad4c 100644 --- a/README.md +++ b/README.md @@ -772,6 +772,60 @@ Gets an JSON-array of all collection items. * Valid values: `stringJsonArray` +#### `\DDTools\ObjectCollection::setOneItemData` (protected) + +Sets data of an item object. All setting of an item data inside the class must be use this method. +It's convenient to override this method in child classes if items are not plain objects. + +* `$params` + * Desctription: Parameters, the pass-by-name style is used. + * Valid values: + * `stdClass` + * `arrayAssociative` + * **Required** + +* `$params->itemIndex` + * Desctription: Item index which data will be set. + * Valid values: `integer` + * **Required** + +* `$params->itemData` + * Desctription: New item data. + * Valid values: + * `array` — indexed arrays are supported as well as associative + * `object` + * **Required** + + +#### `\DDTools\ObjectCollection::getOneItemData` (protected) + +Returns data of an item object. All getting of an item data inside the class must use this method. +It's convenient to override this method in child classes if items are not plain objects. + +* `$params` + * Desctription: Parameters, the pass-by-name style is used. + * Valid values: + * `stdClass` + * `arrayAssociative` + * **Required** + +* `$params->itemObject` + * Desctription: An item object which data will be returned. + * Valid values: + * `array` — indexed arrays are supported as well as associative + * `object` + * **Required** + + +##### Returns + +* `$result` + * Desctription: Data of an item object. + * Valid values: + * `array` + * `object` + + ### `\DDTools\Base\Base` Simple abstract class with some small methods facilitating your work. diff --git a/src/ObjectCollection/ObjectCollection.php b/src/ObjectCollection/ObjectCollection.php index 157bd74..199c453 100644 --- a/src/ObjectCollection/ObjectCollection.php +++ b/src/ObjectCollection/ObjectCollection.php @@ -90,7 +90,7 @@ public function addItems($params = []){ /** * convertItemsType - * @version 1.0.1 (2022-12-28) + * @version 1.0.2 (2022-12-28) * * @see README.md */ @@ -123,9 +123,14 @@ public function convertItemsType($params = []){ 'filter' => $params->filter ]) ){ - $this->items[$itemIndex] = \DDTools\ObjectTools::convertType([ - 'object' => $itemObject, - 'type' => $params->itemType + $this->setOneItemData([ + 'itemIndex' => $itemIndex, + 'itemData' => \DDTools\ObjectTools::convertType([ + 'object' => $this->getOneItemData([ + 'itemObject' => $itemObject + ]), + 'type' => $params->itemType + ]) ]); } } @@ -133,7 +138,7 @@ public function convertItemsType($params = []){ /** * updateItems - * @version 1.0.1 (2022-12-28) + * @version 1.0.2 (2022-12-28) * * @see README.md */ @@ -169,11 +174,16 @@ public function updateItems($params = []){ 'filter' => $params->filter ]) ){ - $this->items[$itemIndex] = \DDTools\ObjectTools::extend([ - 'objects' => [ - $itemObject, - $params->data - ] + $this->setOneItemData([ + 'itemIndex' => $itemIndex, + 'itemData' => \DDTools\ObjectTools::extend([ + 'objects' => [ + $this->getOneItemData([ + 'itemObject' => $itemObject + ]), + $params->data + ] + ]) ]); //Increment result count @@ -190,7 +200,7 @@ public function updateItems($params = []){ /** * getItems - * @version 2.0.1 (2022-12-28) + * @version 2.0.2 (2022-12-28) * * @see README.md */ @@ -228,10 +238,14 @@ public function getItems($params = []){ 'filter' => $params->filter ]) ){ + $itemData = $this->getOneItemData([ + 'itemObject' => $itemObject + ]); + //Save only field value instead of object if needed if (!is_null($params->propAsResultValue)){ $resultItemObject = \DDTools\ObjectTools::getPropValue([ - 'object' => $itemObject, + 'object' => $itemData, 'propName' => $params->propAsResultValue ]); }else{ @@ -242,7 +256,7 @@ public function getItems($params = []){ if (!is_null($params->propAsResultKey)){ $result[ \DDTools\ObjectTools::getPropValue([ - 'object' => $itemObject, + 'object' => $itemData, 'propName' => $params->propAsResultKey ]) ] = $resultItemObject; @@ -354,11 +368,46 @@ public function deleteItems($params = []){ } } + /** + * setOneItemData + * @version 1.0 (2022-12-27) + * + * @desc Sets data of an item object. All setting of an item data inside the class must be use this method. It's convenient to override this method in child classes if items are not plain objects. + * + * @param $params {stdClass|arrayAssociative} — Parameters, the pass-by-name style is used. @required + * @param $params->itemIndex {integer} — Item index which data will be set. @required + * @param $params->itemData {array|object} — New item data. @required + * + * @return {void} + */ + protected function setOneItemData($params){ + $params = (object) $params; + + $this->items[$params->itemIndex] = $params->itemData; + } + + /** + * getOneItemData + * @version 1.0 (2022-12-27) + * + * @desc Returns data of an item object. All getting of an item data inside the class must use this method. It's convenient to override this method in child classes if items are not plain objects. + * + * @param $params {stdClass|arrayAssociative} — Parameters, the pass-by-name style is used. @required + * @param $params->itemObject {array|object} — An item object which data will be returned. @required + * + * @return $result {object|arrayAssociative} + */ + protected function getOneItemData($params){ + $params = (object) $params; + + return $params->itemObject; + } + /** * isItemMatchFilter - * @version 2.0 (2022-12-28) + * @version 2.0.1 (2022-12-28) * - * @param $params {array} — Parameters, the pass-by-name style is used. @required + * @param $params {stdClass|arrayAssociative} — Parameters, the pass-by-name style is used. @required * @param $params->itemObject {array|object} — An item to test. @required * @param $params->filter {array} — Result of $this->prepareItemsFilter. @required * @@ -370,6 +419,10 @@ private function isItemMatchFilter($params){ //By default assume that item is matched $result = true; + $itemData = $this->getOneItemData([ + 'itemObject' => $params->itemObject + ]); + //Iterate over “or” conditions foreach ( $params->filter as @@ -383,7 +436,7 @@ private function isItemMatchFilter($params){ //If the item has no the property if ( !\DDTools\ObjectTools::isPropExists([ - 'object' => $params->item, + 'object' => $itemData, 'propName' => $andCondition->propName ]) ){ @@ -395,7 +448,7 @@ private function isItemMatchFilter($params){ }elseif ($andCondition->operator == '=='){ $result = \DDTools\ObjectTools::getPropValue([ - 'object' => $params->item, + 'object' => $itemData, 'propName' => $andCondition->propName ]) == $andCondition->propValue @@ -404,7 +457,7 @@ private function isItemMatchFilter($params){ }else{ $result = \DDTools\ObjectTools::getPropValue([ - 'object' => $params->item, + 'object' => $itemData, 'propName' => $andCondition->propName ]) != $andCondition->propValue From 2763c663f79419eecf2f9816cf45fab9b80422c0 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sun, 29 Jan 2023 16:28:06 +0400 Subject: [PATCH 10/10] Prerelease --- CHANGELOG.md | 13 +++++++++++++ CHANGELOG_ru.md | 13 +++++++++++++ composer.json | 2 +- modx.ddtools.class.php | 4 ++-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a275cb9..b3a90ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,19 @@ # (MODX)EvolutionCMS.libraries.ddTools changelog +## Version 0.56 (2023-01-29) +* \+ `\ddTools::sort2dArray` → Parameters → `$array[$i]`: Can also be set as object. +* \* `\DDTools\Base\Base`: + * \* The class has been renamed from `\DDTools\BaseClass`. Backward compatibility is maintained (you can still use `\DDTools\BaseClass`, but it is not recommended). + * \* The class has become abstract. + * \- `createChildInstance`: The method has been removed, use `\DDTools\Base\AncestorTrait::createChildInstance` instead. Backward compatibility is maintained (you can still use `\DDTools\BaseClass::createChildInstance`, but it is not recommended). + * \+ `toJSON`: Returns JSON-array if `$this->toArray` returns indexed array. +* \+ `\DDTools\Base\AncestorTrait`: The new trait. +* \+ `\DDTools\ObjectCollection`: + * \+ `toJSON`, `__toString`: The new public methods. Get an JSON-array of all collection items. + * \+ `\DDTools\ObjectCollection::setOneItemData`, `getOneItemData`: The new protected methods. + + ## Version 0.55.1 (2022-12-03) * \* `\DDTools\FilesTools::modifyImage`: Included PHP.libraries.phpThumb has been updated from 1.7.15-202004301145 to 1.7.19-202210110924 (now supports WebP, PHP8, etc). diff --git a/CHANGELOG_ru.md b/CHANGELOG_ru.md index 6ec0653..cb625b7 100644 --- a/CHANGELOG_ru.md +++ b/CHANGELOG_ru.md @@ -1,6 +1,19 @@ # (MODX)EvolutionCMS.libraries.ddTools changelog +## Версия 0.56 (2023-01-29) +* \+ `\ddTools::sort2dArray` → Параметры → `$array[$i]`: Также может быть задан, как объект. +* \* `\DDTools\Base\Base`: + * \* Класс переименован из `\DDTools\BaseClass`. Обратная совместимость сохранена (множно использовать `\DDTools\BaseClass`, но не рекомендуется). + * \* Класс стал абстрактным. + * \- `createChildInstance`: Метод удалён, используйте вместо него `\DDTools\Base\AncestorTrait::createChildInstance`. Обратная совместимость сохранена (можно использовать `\DDTools\BaseClass::createChildInstance`, но не рекомендуется). + * \+ `toJSON`: Возвращает JSON-массив если `$this->toArray` возвращает индексированный массив. +* \+ `\DDTools\Base\AncestorTrait`: Новый trait. +* \+ `\DDTools\ObjectCollection`: + * \+ `toJSON`, `__toString`: Новые публичные методы. Возвращают JSON-массив всех элементов коллекции. + * \+ `\DDTools\ObjectCollection::setOneItemData`, `getOneItemData`: Новые protected методы. + + ## Версия 0.55.1 (2022-12-03) * \* `\DDTools\FilesTools::modifyImage`: PHP.libraries.phpThumb, включённая в репозиторий, обновлена с 1.7.15-202004301145 до 1.7.19-202210110924 (теперь поддерживает WebP, PHP8, etc). diff --git a/composer.json b/composer.json index d1caacc..2bb3bd4 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "dd/evolutioncms-libraries-ddtools", "type": "modxevo-library-ddtools", - "version": "0.55.1", + "version": "0.56.0", "description": "A library with various tools facilitating your work.", "keywords": [ "modx", diff --git a/modx.ddtools.class.php b/modx.ddtools.class.php index 0ea7496..87ada1b 100644 --- a/modx.ddtools.class.php +++ b/modx.ddtools.class.php @@ -1,11 +1,11 @@