diff --git a/README.md b/README.md index dc71020..0fa58bf 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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` @@ -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" + } + ] +} +``` + + \ No newline at end of file diff --git a/composer.json b/composer.json index 4801255..961e0d2 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,6 @@ ], "require": { "php": ">=5.4.0", - "dd/modxevo-library-ddtools": ">=0.28.0" + "dd/modxevo-library-ddtools": ">=0.33.1" } } \ No newline at end of file diff --git a/ddStash_snippet.php b/ddStash_snippet.php index 4dfe6a1..5373a46 100644 --- a/ddStash_snippet.php +++ b/ddStash_snippet.php @@ -37,6 +37,12 @@ //Save to stash if (isset($save)){ + $save_extendExisting = + isset($save_extendExisting) ? + boolval($save_extendExisting) : + false + ; + $save = \ddTools::encodedStringToArray($save); foreach ( @@ -44,7 +50,25 @@ $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; + } } }