Skip to content

Commit

Permalink
+ Added the ability to extend an existing object instead of overwriti…
Browse files Browse the repository at this point in the history
…ng it (see `save_extendExisting`).
  • Loading branch information
Ronef committed Apr 29, 2020
1 parent d2ce530 commit 8526441
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 3 deletions.
99 changes: 98 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

* PHP >= 5.4
* [(MODX)EvolutionCMS](https://github.com/evolution-cms/evolution) >= 1.1
* [(MODX)EvolutionCMS.libraries.ddTools](http://code.divandesign.biz/modx/ddtools) >= 0.28
* [(MODX)EvolutionCMS.libraries.ddTools](http://code.divandesign.biz/modx/ddtools) >= 0.33.1


## Documentation
Expand All @@ -33,6 +33,13 @@ Elements → Snippets: Create a new snippet with the following data:
* `stringQueryFormated` — as [Query string](https://en.wikipedia.org/wiki/Query_string)
* Default value: —

* `save_extendExisting`
* Desctription: Extend an existing object instead of overwriting it.
* Valid values:
* `0`
* `1`
* Default value: `0`

* `get`
* Desctription: Data key for getting from stash.
* Valid values: `string`
Expand Down Expand Up @@ -174,4 +181,94 @@ Returns:
```


#### Save: Extend an existing object instead of overwriting it (``&save_extendExisting=`1` ``)

First you save some object:

```
[[ddStash?
&save=`{
"userData": {
"firstName": "Chuck",
"lastName": "Doe",
"children": [
{
"firstName": "Alice"
},
{
"firstName": "Robert"
}
]
}
}`
]]
```

Then if you just save object with the same key (`userData`):

```
[[ddStash?
&save=`{
"userData": {
"middleName": "Ray",
"lastName": "Norris"
}
}`
]]
```

The snippet will overwrite previous saved object completely:

```
[[ddStash? &get=`userData`]]
```

Returns:

```json
{
"middleName": "Ray",
"lastName": "Norris"
}
```

So, if you want to extend the first object just use the `save_extendExisting` parameter:

```
[[ddStash?
&save=`{
"userData": {
"middleName": "Ray",
"lastName": "Norris"
}
}`
&save_extendExisting=`1`
]]
```

In this case the snippet will recursive extend the first object with the data from the second:

```
[[ddStash? &get=`userData`]]
```

Returns:

```json
{
"firstName": "Chuck",
"middleName": "Ray",
"lastName": "Norris",
"children": [
{
"firstName": "Alice"
},
{
"firstName": "Robert"
}
]
}
```


<link rel="stylesheet" type="text/css" href="https://DivanDesign.ru/assets/files/ddMarkdown.css" />
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
],
"require": {
"php": ">=5.4.0",
"dd/modxevo-library-ddtools": ">=0.28.0"
"dd/modxevo-library-ddtools": ">=0.33.1"
}
}
26 changes: 25 additions & 1 deletion ddStash_snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,38 @@

//Save to stash
if (isset($save)){
$save_extendExisting =
isset($save_extendExisting) ?
boolval($save_extendExisting) :
false
;

$save = \ddTools::encodedStringToArray($save);

foreach (
$save as
$dataName =>
$dataValue
){
$storage['ddStash.' . $dataName] = $dataValue;
$dataName =
'ddStash.' .
$dataName
;

//If need to extend existing
if (
$save_extendExisting &&
isset($storage[$dataName])
){
$storage[$dataName] = \DDTools\ObjectTools::extend([
'objects' => [
$storage[$dataName],
$dataValue
]
]);
}else{
$storage[$dataName] = $dataValue;
}
}
}

Expand Down

0 comments on commit 8526441

Please sign in to comment.