From 204698d94e73d2f648549103fb69b359bb9e44eb Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Thu, 11 Mar 2021 20:33:56 +0300 Subject: [PATCH 1/8] * `\DDTools\ObjectTools::extend`: Recursion is called only if source value is an object or array. --- src/ObjectTools/ObjectTools.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/ObjectTools/ObjectTools.php b/src/ObjectTools/ObjectTools.php index 044f1ec..41be189 100644 --- a/src/ObjectTools/ObjectTools.php +++ b/src/ObjectTools/ObjectTools.php @@ -164,7 +164,7 @@ public static function convertType($params){ /** * extend - * @version 1.3 (2020-04-30) + * @version 1.3.1 (2021-03-11) * * @see README.md * @@ -273,18 +273,11 @@ public static function extend($params){ if ( //If recursive merging is needed $params->deep && + //And we can extend original value + self::isObjectOrArray($originalPropValue) && //And the value is an object or array self::isObjectOrArray($additionalPropValue) ){ - //Init initial property value to extend - if (!$isOriginalPropExists){ - $originalPropValue = - gettype($additionalPropValue) == 'object' ? - new \stdClass() : - [] - ; - } - //Start recursion $additionalPropValue = self::extend([ 'objects' => [ From 8907a4c2440cbe6751445e592b7d26d5b1d9e9e2 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Thu, 11 Mar 2021 20:37:45 +0300 Subject: [PATCH 2/8] * `\DDTools\ObjectTools::extend`: Refactoring. --- src/ObjectTools/ObjectTools.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/ObjectTools/ObjectTools.php b/src/ObjectTools/ObjectTools.php index 41be189..490b234 100644 --- a/src/ObjectTools/ObjectTools.php +++ b/src/ObjectTools/ObjectTools.php @@ -164,7 +164,7 @@ public static function convertType($params){ /** * extend - * @version 1.3.1 (2021-03-11) + * @version 1.3.2 (2021-03-11) * * @see README.md * @@ -200,11 +200,6 @@ public static function extend($params){ $additionalPropName => $additionalPropValue ){ - //Is the original property exists - $isOriginalPropExists = self::isPropExists([ - 'object' => $result, - 'propName' => $additionalPropName - ]); //Original property value $originalPropValue = self::getPropValue([ 'object' => $result, @@ -218,7 +213,10 @@ public static function extend($params){ //Overwriting with empty value is disabled !$params->overwriteWithEmpty && //And original property exists. Because if not exists we must set it in anyway (an empty value is better than nothing, right?) - $isOriginalPropExists + self::isPropExists([ + 'object' => $result, + 'propName' => $additionalPropName + ]) ){ //Check if additional property value is empty $isAdditionalUsed = From a9fd177901d34a5347f9f174315cc85ad382f83d Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Thu, 11 Mar 2021 20:43:21 +0300 Subject: [PATCH 3/8] * `\DDTools\ObjectTools::extend`: Refactoring. --- src/ObjectTools/ObjectTools.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ObjectTools/ObjectTools.php b/src/ObjectTools/ObjectTools.php index 490b234..24fea84 100644 --- a/src/ObjectTools/ObjectTools.php +++ b/src/ObjectTools/ObjectTools.php @@ -164,7 +164,7 @@ public static function convertType($params){ /** * extend - * @version 1.3.2 (2021-03-11) + * @version 1.3.3 (2021-03-11) * * @see README.md * @@ -207,7 +207,7 @@ public static function extend($params){ ]); //The additional property value will be used by default - $isAdditionalUsed = true; + $isAdditionalPropUsed = true; if ( //Overwriting with empty value is disabled @@ -219,7 +219,7 @@ public static function extend($params){ ]) ){ //Check if additional property value is empty - $isAdditionalUsed = + $isAdditionalPropUsed = ( //Empty string ( @@ -242,7 +242,7 @@ public static function extend($params){ if ( //Additional property value is empty - !$isAdditionalUsed && + !$isAdditionalPropUsed && //And original property value is empty too ( //Empty string @@ -262,12 +262,12 @@ public static function extend($params){ $originalPropValue !== $additionalPropValue ){ //Okay, overwrite original in this case - $isAdditionalUsed = true; + $isAdditionalPropUsed = true; } } //If additional value must be used - if ($isAdditionalUsed){ + if ($isAdditionalPropUsed){ if ( //If recursive merging is needed $params->deep && From a2c6d710fbfa98145561edbe729d2b709fad097b Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Thu, 11 Mar 2021 21:13:32 +0300 Subject: [PATCH 4/8] * `\DDTools\ObjectTools::extend`: Optimization. --- src/ObjectTools/ObjectTools.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/ObjectTools/ObjectTools.php b/src/ObjectTools/ObjectTools.php index 24fea84..c81d2fe 100644 --- a/src/ObjectTools/ObjectTools.php +++ b/src/ObjectTools/ObjectTools.php @@ -164,7 +164,7 @@ public static function convertType($params){ /** * extend - * @version 1.3.3 (2021-03-11) + * @version 1.3.4 (2021-03-11) * * @see README.md * @@ -206,6 +206,11 @@ public static function extend($params){ 'propName' => $additionalPropName ]); + //Is the original property object or array + $isOriginalPropObjectOrArray = self::isObjectOrArray($originalPropValue); + //Is the additional property object or array + $isAdditionalPropObjectOrArray = self::isObjectOrArray($additionalPropValue); + //The additional property value will be used by default $isAdditionalPropUsed = true; @@ -230,7 +235,7 @@ public static function extend($params){ is_null($additionalPropValue) || //Empty object or array ( - self::isObjectOrArray($additionalPropValue) && + $isAdditionalPropObjectOrArray && count((array) $additionalPropValue) == 0 ) ) ? @@ -254,7 +259,7 @@ public static function extend($params){ is_null($originalPropValue) || //Empty object or array ( - self::isObjectOrArray($originalPropValue) && + $isOriginalPropObjectOrArray && count((array) $originalPropValue) == 0 ) ) && @@ -272,9 +277,9 @@ public static function extend($params){ //If recursive merging is needed $params->deep && //And we can extend original value - self::isObjectOrArray($originalPropValue) && + $isOriginalPropObjectOrArray && //And the value is an object or array - self::isObjectOrArray($additionalPropValue) + $isAdditionalPropObjectOrArray ){ //Start recursion $additionalPropValue = self::extend([ From 3848f5d227a8d702b96bc96c2f2399a3f2b1263c Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Thu, 11 Mar 2021 21:15:08 +0300 Subject: [PATCH 5/8] * `\DDTools\ObjectTools::extend`: Optimization. --- src/ObjectTools/ObjectTools.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/ObjectTools/ObjectTools.php b/src/ObjectTools/ObjectTools.php index c81d2fe..11a5b6c 100644 --- a/src/ObjectTools/ObjectTools.php +++ b/src/ObjectTools/ObjectTools.php @@ -164,7 +164,7 @@ public static function convertType($params){ /** * extend - * @version 1.3.4 (2021-03-11) + * @version 1.3.5 (2021-03-11) * * @see README.md * @@ -226,18 +226,18 @@ public static function extend($params){ //Check if additional property value is empty $isAdditionalPropUsed = ( + //Empty object or array + ( + $isAdditionalPropObjectOrArray && + count((array) $additionalPropValue) == 0 + ) || //Empty string ( is_string($additionalPropValue) && $additionalPropValue == '' ) || //NULL - is_null($additionalPropValue) || - //Empty object or array - ( - $isAdditionalPropObjectOrArray && - count((array) $additionalPropValue) == 0 - ) + is_null($additionalPropValue) ) ? //Additional is empty — don't use it false: @@ -250,18 +250,18 @@ public static function extend($params){ !$isAdditionalPropUsed && //And original property value is empty too ( + //Empty object or array + ( + $isOriginalPropObjectOrArray && + count((array) $originalPropValue) == 0 + ) || //Empty string ( is_string($originalPropValue) && $originalPropValue == '' ) || //NULL - is_null($originalPropValue) || - //Empty object or array - ( - $isOriginalPropObjectOrArray && - count((array) $originalPropValue) == 0 - ) + is_null($originalPropValue) ) && //But they have different types $originalPropValue !== $additionalPropValue From f72bc12d5b94d819e06b90557703f6dd5b4d5908 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Fri, 12 Mar 2021 11:29:48 +0300 Subject: [PATCH 6/8] * `\DDTools\ObjectTools::extend`: Refactoring. --- src/ObjectTools/ObjectTools.php | 34 ++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/ObjectTools/ObjectTools.php b/src/ObjectTools/ObjectTools.php index 11a5b6c..af2873c 100644 --- a/src/ObjectTools/ObjectTools.php +++ b/src/ObjectTools/ObjectTools.php @@ -164,7 +164,7 @@ public static function convertType($params){ /** * extend - * @version 1.3.5 (2021-03-11) + * @version 1.3.6 (2021-03-12) * * @see README.md * @@ -200,14 +200,14 @@ public static function extend($params){ $additionalPropName => $additionalPropValue ){ - //Original property value - $originalPropValue = self::getPropValue([ + //Source property value + $sourcePropValue = self::getPropValue([ 'object' => $result, 'propName' => $additionalPropName ]); - //Is the original property object or array - $isOriginalPropObjectOrArray = self::isObjectOrArray($originalPropValue); + //Is the source property object or array + $isSourcePropObjectOrArray = self::isObjectOrArray($sourcePropValue); //Is the additional property object or array $isAdditionalPropObjectOrArray = self::isObjectOrArray($additionalPropValue); @@ -217,7 +217,7 @@ public static function extend($params){ if ( //Overwriting with empty value is disabled !$params->overwriteWithEmpty && - //And original property exists. Because if not exists we must set it in anyway (an empty value is better than nothing, right?) + //And source property exists. Because if not exists we must set it in anyway (an empty value is better than nothing, right?) self::isPropExists([ 'object' => $result, 'propName' => $additionalPropName @@ -248,25 +248,25 @@ public static function extend($params){ if ( //Additional property value is empty !$isAdditionalPropUsed && - //And original property value is empty too + //And source property value is empty too ( //Empty object or array ( - $isOriginalPropObjectOrArray && - count((array) $originalPropValue) == 0 + $isSourcePropObjectOrArray && + count((array) $sourcePropValue) == 0 ) || //Empty string ( - is_string($originalPropValue) && - $originalPropValue == '' + is_string($sourcePropValue) && + $sourcePropValue == '' ) || //NULL - is_null($originalPropValue) + is_null($sourcePropValue) ) && //But they have different types - $originalPropValue !== $additionalPropValue + $sourcePropValue !== $additionalPropValue ){ - //Okay, overwrite original in this case + //Okay, overwrite source in this case $isAdditionalPropUsed = true; } } @@ -276,15 +276,15 @@ public static function extend($params){ if ( //If recursive merging is needed $params->deep && - //And we can extend original value - $isOriginalPropObjectOrArray && + //And we can extend source value + $isSourcePropObjectOrArray && //And the value is an object or array $isAdditionalPropObjectOrArray ){ //Start recursion $additionalPropValue = self::extend([ 'objects' => [ - $originalPropValue, + $sourcePropValue, $additionalPropValue ], 'deep' => true, From 7c4be6327bfdfa9576742a8bc7a95ac3093cef9e Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Fri, 12 Mar 2021 11:45:40 +0300 Subject: [PATCH 7/8] * `\DDTools\ObjectTools::extend`: Optimization. --- src/ObjectTools/ObjectTools.php | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/ObjectTools/ObjectTools.php b/src/ObjectTools/ObjectTools.php index af2873c..a687638 100644 --- a/src/ObjectTools/ObjectTools.php +++ b/src/ObjectTools/ObjectTools.php @@ -164,7 +164,7 @@ public static function convertType($params){ /** * extend - * @version 1.3.6 (2021-03-12) + * @version 1.3.7 (2021-03-12) * * @see README.md * @@ -200,14 +200,26 @@ public static function extend($params){ $additionalPropName => $additionalPropValue ){ - //Source property value - $sourcePropValue = self::getPropValue([ + //Is the source property exists + $isSourcePropExists = self::isPropExists([ 'object' => $result, 'propName' => $additionalPropName ]); - //Is the source property object or array - $isSourcePropObjectOrArray = self::isObjectOrArray($sourcePropValue); + if ($isSourcePropExists){ + //Source property value + $sourcePropValue = self::getPropValue([ + 'object' => $result, + 'propName' => $additionalPropName + ]); + + //Is the source property object or array + $isSourcePropObjectOrArray = self::isObjectOrArray($sourcePropValue); + }else{ + $sourcePropValue = null; + $isSourcePropObjectOrArray = false; + } + //Is the additional property object or array $isAdditionalPropObjectOrArray = self::isObjectOrArray($additionalPropValue); @@ -218,10 +230,7 @@ public static function extend($params){ //Overwriting with empty value is disabled !$params->overwriteWithEmpty && //And source property exists. Because if not exists we must set it in anyway (an empty value is better than nothing, right?) - self::isPropExists([ - 'object' => $result, - 'propName' => $additionalPropName - ]) + $isSourcePropExists ){ //Check if additional property value is empty $isAdditionalPropUsed = From 8368806d3c3633efc46e3a81e38ea71142207a51 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Fri, 12 Mar 2021 11:52:55 +0300 Subject: [PATCH 8/8] Prerelease --- CHANGELOG.md | 6 ++++++ CHANGELOG_ru.md | 6 ++++++ composer.json | 2 +- modx.ddtools.class.php | 2 +- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b46a050..ef9c232 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # (MODX)EvolutionCMS.libraries.ddTools changelog +## Version 0.45.1 (2021-03-12) +* \* `\DDTools\ObjectTools::extend`: + * \* Recursion is called only if source value is an object or array. + * \* Optimization. + + ## Version 0.45 (2021-03-11) * \+ `\DDTools\Response::isSuccess`: The new public method. diff --git a/CHANGELOG_ru.md b/CHANGELOG_ru.md index 5d6b094..c6ec1f2 100644 --- a/CHANGELOG_ru.md +++ b/CHANGELOG_ru.md @@ -1,6 +1,12 @@ # (MODX)EvolutionCMS.libraries.ddTools changelog +## Версия 0.45.1 (2021-03-12) +* \* `\DDTools\ObjectTools::extend`: + * \* Рекурсия вызывается только в том случае, если исходным значением является объект или массив. + * \* Оптимизация. + + ## Версия 0.45 (2021-03-11) * \+ `\DDTools\Response::isSuccess`: Новый публичный метод. diff --git a/composer.json b/composer.json index 53b54b0..e43bca7 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "dd/evolutioncms-libraries-ddtools", "type": "modxevo-library-ddtools", - "version": "0.45.0", + "version": "0.45.1", "description": "A library with various tools facilitating your work.", "keywords": [ "modx", diff --git a/modx.ddtools.class.php b/modx.ddtools.class.php index d712d68..530322b 100644 --- a/modx.ddtools.class.php +++ b/modx.ddtools.class.php @@ -1,7 +1,7 @@