diff --git a/README.md b/README.md index 0a7e20c..7391d1d 100644 --- a/README.md +++ b/README.md @@ -106,20 +106,23 @@ Merge the contents of two or more objects together into the first object. * **Required** * `$params->objects` - * Desctription: Objects to merge. + * Desctription: Objects or arrays to merge. * Valid values: `array` * **Required** * `$params->objects[0]` - * Desctription: The object to extend. It will receive the new properties. + * Desctription: The object or array to extend. It will receive the new properties. * Valid values: * `object` + * `array` * `NULL` — pass `NULL` to create the new StdClass. * **Required** * `$params->objects[i]` - * Desctription: An object containing additional properties to merge in. - * Valid values: `object` + * Desctription: An object or array containing additional properties to merge in. + * Valid values: + * `object` + * `array` * **Required** * `$params->deep` @@ -183,7 +186,8 @@ extract(\ddTools::verifyRenamedParams([ #### `\DDTools\ObjectTools::extend($params)` -Merge two objects, modifying the first. + +##### Merge two objects, modifying the first ```php var_export(\DDTools\ObjectTools::extend([ @@ -208,7 +212,7 @@ var_export(\DDTools\ObjectTools::extend([ Returns: -``` +```php stdClass::__set_state(array( 'cat' => 'mew', 'dog' => stdClass::__set_state(array( @@ -221,6 +225,44 @@ stdClass::__set_state(array( ``` +##### Also you can extend arrays + +```php +var_export(\DDTools\ObjectTools::extend([ + 'objects' => [ + [ + 'cat' => 'mew', + 'dog' => [ + 'name' => 'Floyd', + 'weight' => 6 + ], + 'rabbit' => 42 + ], + [ + 'dog' => (object) [ + 'weight' => 10 + ], + 'bird' => 0 + ] + ] +])); +``` + +Returns: + +```php +array( + 'cat' => 'mew', + 'dog' => array( + 'name' => 'Floyd', + 'weight' => 10, + ), + 'rabbit' => 42, + 'bird' => 0, +) +``` + + ## [Home page →](https://code.divandesign.biz/modx/ddtools) diff --git a/src/ObjectTools/ObjectTools.php b/src/ObjectTools/ObjectTools.php index 806dea6..00da23a 100644 --- a/src/ObjectTools/ObjectTools.php +++ b/src/ObjectTools/ObjectTools.php @@ -4,11 +4,11 @@ class ObjectTools { /** * extend - * @version 1.0 (2020-04-23) + * @version 1.1 (2020-04-28) * * @see README.md * - * @return {object} + * @return {object|array} */ public static function extend($params){ //Defaults @@ -19,19 +19,29 @@ public static function extend($params){ (array) $params ); - //The first object is the target + //The first item is the target $result = array_shift($params->objects); //Empty or invalid target - if (!is_object($result)){ + if ( + !is_object($result) && + !is_array($result) + ){ $result = new \stdClass(); } + $isResultObject = is_object($result); + $checkFunction = + $isResultObject ? + 'is_object' : + 'is_array' + ; + foreach ( $params->objects as $additionalProps ){ //Invalid objects will not be used - if (is_object($additionalProps)){ + if ($checkFunction($additionalProps)){ foreach ( $additionalProps as $additionalPropName => @@ -41,19 +51,27 @@ public static function extend($params){ //If recursive merging is needed $params->deep && //And the value is an object - is_object($additionalPropValue) + $checkFunction($additionalPropValue) ){ //Start recursion - $result->{$additionalPropName} = self::extend([ + $additionalPropValue = self::extend([ 'objects' => [ - $result->{$additionalPropName}, + ( + $isResultObject ? + $result->{$additionalPropName} : + $result[$additionalPropName] + ), $additionalPropValue ], 'deep' => true ]); - }else{ - //Save the new value (replace preverious or create the new property) + } + + //Save the new value (replace preverious or create the new property) + if ($isResultObject){ $result->{$additionalPropName} = $additionalPropValue; + }else{ + $result[$additionalPropName] = $additionalPropValue; } } }