Skip to content

Commit

Permalink
Version 0.45.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronef committed Mar 12, 2021
2 parents 684736a + 8368806 commit 2036e97
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG_ru.md
Original file line number Diff line number Diff line change
@@ -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`: Новый публичный метод.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion modx.ddtools.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* EvolutionCMS.libraries.ddTools
* @version 0.45 (2021-03-11)
* @version 0.45.1 (2021-03-12)
*
* @see README.md
*
Expand Down
91 changes: 48 additions & 43 deletions src/ObjectTools/ObjectTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static function convertType($params){

/**
* extend
* @version 1.3 (2020-04-30)
* @version 1.3.7 (2021-03-12)
*
* @see README.md
*
Expand Down Expand Up @@ -200,41 +200,53 @@ 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([
//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:
Expand All @@ -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,
Expand Down

0 comments on commit 2036e97

Please sign in to comment.