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 @@ $additionalPropValue ){ - //Is the original property exists - $isOriginalPropExists = self::isPropExists([ - 'object' => $result, - 'propName' => $additionalPropName - ]); - //Original property value - $originalPropValue = self::getPropValue([ + //Is the source property exists + $isSourcePropExists = self::isPropExists([ 'object' => $result, 'propName' => $additionalPropName ]); + 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); + //The additional property value will be used by default - $isAdditionalUsed = true; + $isAdditionalPropUsed = true; 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?) - $isOriginalPropExists + //And source property exists. Because if not exists we must set it in anyway (an empty value is better than nothing, right?) + $isSourcePropExists ){ //Check if additional property value is empty - $isAdditionalUsed = + $isAdditionalPropUsed = ( + //Empty object or array + ( + $isAdditionalPropObjectOrArray && + count((array) $additionalPropValue) == 0 + ) || //Empty string ( is_string($additionalPropValue) && $additionalPropValue == '' ) || //NULL - is_null($additionalPropValue) || - //Empty object or array - ( - self::isObjectOrArray($additionalPropValue) && - count((array) $additionalPropValue) == 0 - ) + is_null($additionalPropValue) ) ? //Additional is empty — don't use it false: @@ -244,51 +256,44 @@ public static function extend($params){ if ( //Additional property value is empty - !$isAdditionalUsed && - //And original property value is empty too + !$isAdditionalPropUsed && + //And source property value is empty too ( + //Empty object or array + ( + $isSourcePropObjectOrArray && + count((array) $sourcePropValue) == 0 + ) || //Empty string ( - is_string($originalPropValue) && - $originalPropValue == '' + is_string($sourcePropValue) && + $sourcePropValue == '' ) || //NULL - is_null($originalPropValue) || - //Empty object or array - ( - self::isObjectOrArray($originalPropValue) && - count((array) $originalPropValue) == 0 - ) + is_null($sourcePropValue) ) && //But they have different types - $originalPropValue !== $additionalPropValue + $sourcePropValue !== $additionalPropValue ){ - //Okay, overwrite original in this case - $isAdditionalUsed = true; + //Okay, overwrite source in this case + $isAdditionalPropUsed = true; } } //If additional value must be used - if ($isAdditionalUsed){ + if ($isAdditionalPropUsed){ if ( //If recursive merging is needed $params->deep && + //And we can extend source value + $isSourcePropObjectOrArray && //And the value is an object or array - self::isObjectOrArray($additionalPropValue) + $isAdditionalPropObjectOrArray ){ - //Init initial property value to extend - if (!$isOriginalPropExists){ - $originalPropValue = - gettype($additionalPropValue) == 'object' ? - new \stdClass() : - [] - ; - } - //Start recursion $additionalPropValue = self::extend([ 'objects' => [ - $originalPropValue, + $sourcePropValue, $additionalPropValue ], 'deep' => true,