Skip to content

Commit

Permalink
Version 0.33
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronef committed Apr 28, 2020
2 parents c87b116 + 0d74768 commit 01b8de8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 20 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# (MODX)EvolutionCMS.libraries.ddTools changelog


## Version 0.33 (2020-04-28)
* \+ `\DDTools\ObjectTools::extend`: Arrays can also be extended.
* \* CHANGELOG: Fixed misprints.


## Version 0.32 (2020-04-25)
* \* `\ddTools::verifyRenamedParams`:
* \* Pass-by-name style is used (with backward compatibility).
Expand Down Expand Up @@ -122,7 +127,7 @@
## Version 0.15.4 (2016-06-17)
* \* `\ddTools::verifyRenamedParams`: Updated to 1.1.1:
* \+ An ability to use multiple old names was added (see the `$compliance` parameter).
* \* Minor refactoring, code style and decsription changes.
* \* Minor refactoring, code style and description changes.


## Version 0.15.3 (2016-01-12)
Expand Down Expand Up @@ -179,7 +184,7 @@

## Version 0.13.1 (2015-08-17)
* \* `\ddTools::sendMail`: The method was slightly changed to eliminate errors in PHP 5.4−5.6 during headers validation:
* \* All double `\r\n` and singe `\r\n` were replaced with single `PHP_EO`L.
* \* All double `\r\n` and singe `\r\n` were replaced with single `PHP_EOL`.
* \* Leading or trailing `PHP_EOL`'s are now trimmed in email content.


Expand Down
54 changes: 48 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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([
Expand All @@ -208,7 +212,7 @@ var_export(\DDTools\ObjectTools::extend([

Returns:

```
```php
stdClass::__set_state(array(
'cat' => 'mew',
'dog' => stdClass::__set_state(array(
Expand All @@ -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)


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/modxevo-library-ddtools",
"type": "modxevo-library-ddtools",
"version": "0.32.0",
"version": "0.33.0",
"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.32 (2020-04-25)
* @version 0.33 (2020-04-28)
*
* @see README.md
*
Expand Down
38 changes: 28 additions & 10 deletions src/ObjectTools/ObjectTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =>
Expand All @@ -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;
}
}
}
Expand Down

0 comments on commit 01b8de8

Please sign in to comment.