From abfe83ca6bef9a0564bdf206b9bc654beb96574d Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sun, 26 Apr 2020 18:28:21 +0300 Subject: [PATCH 1/7] * Minor changes. --- ddStash_snippet.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ddStash_snippet.php b/ddStash_snippet.php index 3cbbed3..d638070 100644 --- a/ddStash_snippet.php +++ b/ddStash_snippet.php @@ -9,7 +9,10 @@ */ //Include (MODX)EvolutionCMS.libraries.ddTools -require_once($modx->getConfig('base_path') . 'assets/libs/ddTools/modx.ddtools.class.php'); +require_once( + $modx->getConfig('base_path') . + 'assets/libs/ddTools/modx.ddtools.class.php' +); //The snippet must return an empty string even if result is absent $snippetResult = ''; From fac53dbeea3c15da8fd37d38cd20858d93cd86d4 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sun, 26 Apr 2020 18:37:13 +0300 Subject: [PATCH 2/7] + README, CHANGELOG: Style improvements. --- CHANGELOG.md | 1 + README.md | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4ef5ea..866446a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,4 +5,5 @@ * \+ The first release. + \ No newline at end of file diff --git a/README.md b/README.md index 947ae1c..0286f70 100644 --- a/README.md +++ b/README.md @@ -3,16 +3,17 @@ Сохранение данных для последующего использования. -## # Requires +## Requires + * 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 -## # Documentation +## Documentation -### ## Installation +### Installation Elements → Snippets: Create a new snippet with the following data: @@ -23,7 +24,7 @@ Elements → Snippets: Create a new snippet with the following data: 5. Snippet code (php): Insert content of the `ddStash_snippet.php` file from the archive. -### ## Parameters description +### Parameters description * `save` * Desctription: Data to save in stash. Arrays are supported too: `some[a]=one&some[b]=two` => `[+some.a+]`, `[+some.b+]`; `some[]=one&some[]=two` => `[+some.0+]`, `[some.1]`. @@ -56,10 +57,10 @@ Elements → Snippets: Create a new snippet with the following data: * Default value: `'post'` -### ## Examples +### Examples -#### ### Save some data +#### Save some data ``` [[ddStash? @@ -77,10 +78,13 @@ Elements → Snippets: Create a new snippet with the following data: ``` -#### ### Get saved data +#### Get saved data ``` [[ddStash? &get=`userData.name`]] ``` -Returns `John`. \ No newline at end of file +Returns `John`. + + + \ No newline at end of file From 770ff65e7ae3d294ae8becab701c55ca8a0ab67e Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sun, 26 Apr 2020 18:46:12 +0300 Subject: [PATCH 3/7] =?UTF-8?q?*=20Composer.json:=20=09*=20`version`:=20Fi?= =?UTF-8?q?xed=20format.=20=09+=20`keywords`=20=E2=86=92=20`stash`.=20=09+?= =?UTF-8?q?=20`require`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 0755cab..4801255 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "dd/evolutioncms-snippets-ddstash", "type": "modxevo-snippet", - "version": "1.0", + "version": "1.0.0", "description": "Сохранение данных для последующего использования.", "keywords": [ "modx", @@ -10,6 +10,11 @@ "evo", "evo cms", "evolution cms", - "ddstash" - ] + "ddstash", + "stash" + ], + "require": { + "php": ">=5.4.0", + "dd/modxevo-library-ddtools": ">=0.28.0" + } } \ No newline at end of file From 6399d6cc334fd41338cdc553ef0d2fed95d20b62 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sun, 26 Apr 2020 18:50:28 +0300 Subject: [PATCH 4/7] =?UTF-8?q?*=20REAMDE=20=E2=86=92=C2=A0Parameters=20de?= =?UTF-8?q?scription=20=E2=86=92=C2=A0`save`:=20Small=20improvements.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0286f70..9b00164 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,10 @@ Elements → Snippets: Create a new snippet with the following data: ### Parameters description * `save` - * Desctription: Data to save in stash. Arrays are supported too: `some[a]=one&some[b]=two` => `[+some.a+]`, `[+some.b+]`; `some[]=one&some[]=two` => `[+some.0+]`, `[some.1]`. + * Desctription: Data to save in stash. Nested objects are supported too, see examples below. * Valid values: - * `stirng_json` — as [JSON](https://en.wikipedia.org/wiki/JSON) - * `string_queryFormated` — as [Query string](https://en.wikipedia.org/wiki/Query_string) + * `stirngJsonObject` — as [JSON](https://en.wikipedia.org/wiki/JSON) + * `stringQueryFormated` — as [Query string](https://en.wikipedia.org/wiki/Query_string) * Default value: — * `get` From d2ce5306bc008bd46c74aaf21965765af034789c Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 27 Apr 2020 11:43:52 +0300 Subject: [PATCH 5/7] + Added the ability to return objects and arrays in JSON format (see README.md). --- README.md | 97 ++++++++++++++++++++++++++++++++++++++++++--- ddStash_snippet.php | 52 ++++++++++++++++++++---- 2 files changed, 137 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 9b00164..dc71020 100644 --- a/README.md +++ b/README.md @@ -66,11 +66,19 @@ Elements → Snippets: Create a new snippet with the following data: [[ddStash? &save=`{ "userData": { - "name": "John", - "photo": "[[ddGetDocumentField? + "firstName": "John", + "lastName": "[[ddGetDocumentField? &docId=`1` - &docField=`photo` - ]]" + &docField=`lastName` + ]]", + "children": [ + { + "firstName": "Alice" + }, + { + "firstName": "Robert" + } + ] }, "someData": "someValue" }` @@ -80,11 +88,90 @@ Elements → Snippets: Create a new snippet with the following data: #### Get saved data + +##### You can get field value in any depth + +``` +[[ddStash? &get=`someData`]] +``` + +Returns `someValue`. + ``` -[[ddStash? &get=`userData.name`]] +[[ddStash? &get=`userData.firstName`]] ``` Returns `John`. +``` +[[ddStash? &get=`userData.children.0.firstName`]] +``` + +Returns `Alice`. + + +##### Also you can get objects in JSON + +If field value is object or array, it will be returned in JSON format. + + +###### Get first John child + +``` +[[ddStash? &get=`userData.children.0`]] +``` + +Returns: + +```json +{ + "firstName": "Alice" +} +``` + + +###### Get all John children: + +``` +[[ddStash? &get=`userData.children`]] +``` + +Returns: + +```json +[ + { + "firstName": "Alice" + }, + { + "firstName": "Robert" + } +] +``` + + +###### Get all data about John + +``` +[[ddStash? &get=`userData`]] +``` + +Returns: + +```json +{ + "firstName": "John", + "lastName": "Doe", + "children": [ + { + "firstName": "Alice" + }, + { + "firstName": "Robert" + } + ] +} +``` + \ No newline at end of file diff --git a/ddStash_snippet.php b/ddStash_snippet.php index d638070..4dfe6a1 100644 --- a/ddStash_snippet.php +++ b/ddStash_snippet.php @@ -37,7 +37,7 @@ //Save to stash if (isset($save)){ - $save = \ddTools::unfoldArray(\ddTools::encodedStringToArray($save)); + $save = \ddTools::encodedStringToArray($save); foreach ( $save as @@ -48,13 +48,51 @@ } } - //Get from stash -if ( - isset($get) && - isset($storage['ddStash.' . $get]) -){ - $snippetResult = $storage['ddStash.' . $get]; +if (isset($get)){ + //Unfolding support (e. g. `parentKey.someKey.0`) + $keys = explode( + '.', + $get + ); + + //Correct parent key + $keys[0] = + 'ddStash.' . + $keys[0] + ; + + //If parent exists + if (isset($storage[$keys[0]])){ + $snippetResult = $storage; + + //Find needed value + foreach ( + $keys as + $key + ){ + //If need to see deeper + if ( + is_array($snippetResult) && + isset($snippetResult[$key]) + ){ + $snippetResult = $snippetResult[$key]; + }else{ + break; + } + } + } + + if ( + is_object($snippetResult) || + is_array($snippetResult) + ){ + $snippetResult = json_encode( + $snippetResult, + //JSON_UNESCAPED_UNICODE — Не кодировать многобайтные символы Unicode | JSON_UNESCAPED_SLASHES — Не экранировать / + JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES + ); + } if ( //If template is used From 8526441f331a8b81410704da9a84105e30939273 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Tue, 28 Apr 2020 15:06:02 +0300 Subject: [PATCH 6/7] + Added the ability to extend an existing object instead of overwriting it (see `save_extendExisting`). --- README.md | 99 ++++++++++++++++++++++++++++++++++++++++++++- composer.json | 2 +- ddStash_snippet.php | 26 +++++++++++- 3 files changed, 124 insertions(+), 3 deletions(-) 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; + } } } From 677b62a2fde85bd086a3e35678657a8d7db5080f Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Wed, 29 Apr 2020 15:12:42 +0300 Subject: [PATCH 7/7] Prerelease --- CHANGELOG.md | 11 +++++++++++ README.md | 4 ++-- composer.json | 4 ++-- ddStash_snippet.php | 4 ++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 866446a..73a755f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,17 @@ # (MODX)EvolutionCMS.snippets.ddStash changelog +## Version 1.1 (2020-04-29) +* \+ Added the ability to return objects and arrays in JSON format (see README.md). +* \+ Added the ability to extend an existing object instead of overwriting it (see `save_extendExisting`). +* \+ README, CHANGELOG: Style improvements. +* \* REAMDE → Parameters description → `save`: Small improvements. +* \* Composer.json: + * \* `version`: Fixed format. + * \+ `keywords` → `stash`. + * \+ `require`. + + ## Version 1.0 (2019-10-31) * \+ The first release. diff --git a/README.md b/README.md index 0fa58bf..c0b9f4b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # (MODX)EvolutionCMS.snippets.ddStash -Сохранение данных для последующего использования. +Save data as JSON or QueryString, then extend if needed and use it later without database queries. ## Requires @@ -18,7 +18,7 @@ Elements → Snippets: Create a new snippet with the following data: 1. Snippet name: `ddStash`. -2. Description: `1.0 Сохранение данных для последующего использования.`. +2. Description: `1.1 Save data as JSON or QueryString, then extend if needed and use it later without database queries.`. 3. Category: `Core`. 4. Parse DocBlock: `no`. 5. Snippet code (php): Insert content of the `ddStash_snippet.php` file from the archive. diff --git a/composer.json b/composer.json index 961e0d2..eb3188e 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { "name": "dd/evolutioncms-snippets-ddstash", "type": "modxevo-snippet", - "version": "1.0.0", - "description": "Сохранение данных для последующего использования.", + "version": "1.1.0", + "description": "Save data as JSON or QueryString, then extend if needed and use it later without database queries.", "keywords": [ "modx", "modx evo", diff --git a/ddStash_snippet.php b/ddStash_snippet.php index 5373a46..ba76803 100644 --- a/ddStash_snippet.php +++ b/ddStash_snippet.php @@ -1,11 +1,11 @@