From f7ba2a28ab7dcb7722d6895112252175cb9a86aa Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 4 Jan 2021 16:16:31 +0300 Subject: [PATCH 01/42] * `\ddGetDocuments\Extender\Search\Extender::applyToSnippetParams`: Refactoring. --- src/Extender/Search/Extender.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Extender/Search/Extender.php b/src/Extender/Search/Extender.php index 5e8506b..01ce5d8 100644 --- a/src/Extender/Search/Extender.php +++ b/src/Extender/Search/Extender.php @@ -39,7 +39,7 @@ public function __construct($params = []){ /** * applyToSnippetParams - * @version 2.0 (2020-03-11) + * @version 2.0.1 (2021-01-04) * * @param $snippetParams {stdClass} * @@ -48,15 +48,6 @@ public function __construct($params = []){ public function applyToSnippetParams($snippetParams){ //If URL contains tags if (!empty($this->currentQuery)){ - if( - isset($snippetParams->filter) && - trim($snippetParams->filter) != '' - ){ - $snippetParams->filter .= ' AND'; - }else{ - $snippetParams->filter = ''; - } - $searchQueries = []; foreach ( @@ -72,6 +63,15 @@ public function applyToSnippetParams($snippetParams){ ; } + if( + isset($snippetParams->filter) && + trim($snippetParams->filter) != '' + ){ + $snippetParams->filter .= ' AND'; + }else{ + $snippetParams->filter = ''; + } + $snippetParams->filter .= ' (' . implode( From 8a526cef27e085d7ea28c12b75d1749d0baa0ed6 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 04:39:58 +0300 Subject: [PATCH 02/42] * Minor changes. --- src/Outputter/Yandexmarket/Outputter.php | 53 +++++++++++++++--------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 6ea556e..a878b2f 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -150,7 +150,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { /** * __construct - * @version 1.3 (2020-03-10) + * @version 1.3.1 (2021-02-08) * * @note @link https://yandex.ru/support/partnermarket/export/yml.html * @@ -161,7 +161,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { * @param $params->shopData_currencyId {string} — Currency code (https://yandex.ru/support/partnermarket/currencies.html). Default: 'RUR'. * @param $params->shopData_platform {string} — Содержимое тега ``. Default: '(MODX) Evolution CMS'. * @param $params->shopData_version {string} — Содержимое тега ``. Default: '[(settings_version)]'. - * @param $params->categoryIds_last {string_commaSepareted} — id конечных категорий(parent). Если пусто то выводятся только непосредственный родитель товара. Defalut: —. + * @param $params->categoryIds_last {stringCommaSepareted} — id конечных категорий(parent). Если пусто то выводятся только непосредственный родитель товара. Defalut: —. * @param $params->offerFields_price {stringTvName|''} — Поле, содержащее актуальную цену товара. @required * @param $params->offerFields_priceOld {stringTvName} — Поле, содержащее старую цену товара (должна быть выше актуальной цены). Default: —. * @param $params->offerFields_picture {stringTvName} — Поле, содержащее изображение товара. Defalut: —. @@ -186,6 +186,9 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { function __construct($params = []){ $params = (object) $params; + + //# Prepare params + //Convert params to objects $this->shopData = (object) $this->shopData; $this->offerFields = (object) $this->offerFields; @@ -206,41 +209,49 @@ function __construct($params = []){ $this->templates->{$templateName} = trim($templateText); } - //Call base constructor + + //# Call base constructor parent::__construct($params); - //Save shop data and offer fields + + //# Save shop data and offer fields foreach ( $params as $paramName => $paramValue ){ //Shop data - if (substr( - $paramName, - 0, - 9 - ) == 'shopData_'){ + if ( + substr( + $paramName, + 0, + 9 + ) == 'shopData_' + ){ $this->shopData->{substr( $paramName, 9 )} = $paramValue; //Offer field names - }elseif (substr( - $paramName, - 0, - 12 - ) == 'offerFields_'){ + }elseif ( + substr( + $paramName, + 0, + 12 + ) == 'offerFields_' + ){ $this->offerFields->{substr( $paramName, 12 )}->docFieldName = $paramValue; //Templates - }elseif (substr( - $paramName, - 0, - 10 - ) == 'templates_'){ + }elseif ( + substr( + $paramName, + 0, + 10 + ) == 'templates_' + ){ $this->templates->{substr( $paramName, 10 @@ -248,6 +259,7 @@ function __construct($params = []){ } } + //Offer $templateData = [ 'shopData' => (array) $this->shopData @@ -288,6 +300,7 @@ function __construct($params = []){ 'mergeAll' => false ]); + //Wrapper $templateData = [ 'shopData' => (array) $this->shopData, @@ -314,6 +327,7 @@ function __construct($params = []){ 'mergeAll' => false ]); + //save last parent id for category $this->categoryIds_last = isset($params->categoryIds_last) ? @@ -321,6 +335,7 @@ function __construct($params = []){ '' ; + //We use the “String” Outputter as base $outputter_StringClass = \ddGetDocuments\Outputter\Outputter::includeOutputterByName('String'); $outputter_StringParams = (object) [ From 58b982abdae0f74b7c195bf501e9f5fd32ce1b03 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 05:11:51 +0300 Subject: [PATCH 03/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::__construct`: Refactoring, templates preparation was moved out to a standalone method. --- src/Outputter/Yandexmarket/Outputter.php | 56 ++++++++++++++---------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index a878b2f..356843e 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -150,7 +150,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { /** * __construct - * @version 1.3.1 (2021-02-08) + * @version 1.3.2 (2021-02-08) * * @note @link https://yandex.ru/support/partnermarket/export/yml.html * @@ -260,6 +260,38 @@ function __construct($params = []){ } + //# Prepare templates + $this->construct_prepareTemplates(); + + + //save last parent id for category + $this->categoryIds_last = + isset($params->categoryIds_last) ? + trim($params->categoryIds_last) : + '' + ; + + + //We use the “String” Outputter as base + $outputter_StringClass = \ddGetDocuments\Outputter\Outputter::includeOutputterByName('String'); + $outputter_StringParams = (object) [ + 'itemTpl' => $this->templates->offers_item, + 'wrapperTpl' => $this->templates->wrapper + ]; + //Transfer provider link + if (isset($params->dataProvider)){ + $outputter_StringParams->dataProvider = $params->dataProvider; + } + $this->outputter_StringInstance = new $outputter_StringClass($outputter_StringParams); + } + + /** + * construct_prepareTemplates + * @version 1.0 (2021-02-08) + * + * @return {void} + */ + private function construct_prepareTemplates(){ //Offer $templateData = [ 'shopData' => (array) $this->shopData @@ -320,33 +352,13 @@ function __construct($params = []){ 'mergeAll' => false ]); } + //Prepare wrapper template $this->templates->wrapper = \ddTools::parseText([ 'text' => $this->templates->wrapper, 'data' => $templateData, 'mergeAll' => false ]); - - - //save last parent id for category - $this->categoryIds_last = - isset($params->categoryIds_last) ? - trim($params->categoryIds_last) : - '' - ; - - - //We use the “String” Outputter as base - $outputter_StringClass = \ddGetDocuments\Outputter\Outputter::includeOutputterByName('String'); - $outputter_StringParams = (object) [ - 'itemTpl' => $this->templates->offers_item, - 'wrapperTpl' => $this->templates->wrapper - ]; - //Transfer provider link - if (isset($params->dataProvider)){ - $outputter_StringParams->dataProvider = $params->dataProvider; - } - $this->outputter_StringInstance = new $outputter_StringClass($outputter_StringParams); } /** From b4c01d1dc0693a6b5fe9c7700d611aa5c85a5425 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 05:12:53 +0300 Subject: [PATCH 04/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::__construct`: Refactoring. --- src/Outputter/Yandexmarket/Outputter.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 356843e..7a42852 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -150,7 +150,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { /** * __construct - * @version 1.3.2 (2021-02-08) + * @version 1.3.3 (2021-02-08) * * @note @link https://yandex.ru/support/partnermarket/export/yml.html * @@ -259,11 +259,6 @@ function __construct($params = []){ } } - - //# Prepare templates - $this->construct_prepareTemplates(); - - //save last parent id for category $this->categoryIds_last = isset($params->categoryIds_last) ? @@ -272,6 +267,10 @@ function __construct($params = []){ ; + //# Prepare templates + $this->construct_prepareTemplates(); + + //We use the “String” Outputter as base $outputter_StringClass = \ddGetDocuments\Outputter\Outputter::includeOutputterByName('String'); $outputter_StringParams = (object) [ From 9e6848ca14c42601a13939f0cce313552312f241 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 05:19:09 +0300 Subject: [PATCH 05/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::__construct`: Refactoring, object fields preparation was moved out to a standalone method. --- src/Outputter/Yandexmarket/Outputter.php | 54 ++++++++++++++---------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 7a42852..8dadc40 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -150,7 +150,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { /** * __construct - * @version 1.3.3 (2021-02-08) + * @version 1.3.4 (2021-02-08) * * @note @link https://yandex.ru/support/partnermarket/export/yml.html * @@ -187,27 +187,8 @@ function __construct($params = []){ $params = (object) $params; - //# Prepare params - - //Convert params to objects - $this->shopData = (object) $this->shopData; - $this->offerFields = (object) $this->offerFields; - foreach ( - $this->offerFields as - $offerFieldName => - $offerFieldValue - ){ - $this->offerFields->{$offerFieldName} = (object) $this->offerFields->{$offerFieldName}; - } - $this->templates = (object) $this->templates; - //Trim all templates - foreach ( - $this->templates as - $templateName => - $templateText - ){ - $this->templates->{$templateName} = trim($templateText); - } + //# Prepare object fields + $this->construct_prepareFields(); //# Call base constructor @@ -284,6 +265,35 @@ function __construct($params = []){ $this->outputter_StringInstance = new $outputter_StringClass($outputter_StringParams); } + /** + * construct_prepareFields + * @version 1.0 (2021-02-08) + * + * @return {void} + */ + private function construct_prepareFields(){ + //Convert fields to objects + $this->shopData = (object) $this->shopData; + $this->offerFields = (object) $this->offerFields; + foreach ( + $this->offerFields as + $offerFieldName => + $offerFieldValue + ){ + $this->offerFields->{$offerFieldName} = (object) $this->offerFields->{$offerFieldName}; + } + + $this->templates = (object) $this->templates; + //Trim all templates + foreach ( + $this->templates as + $templateName => + $templateText + ){ + $this->templates->{$templateName} = trim($templateText); + } + } + /** * construct_prepareTemplates * @version 1.0 (2021-02-08) From 0f84354d979cda7605e89a72eda89c8e8be94019 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 06:26:50 +0300 Subject: [PATCH 06/42] * Minor changes. --- src/Outputter/Yandexmarket/Outputter.php | 80 +++++++++++++++++++----- 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 8dadc40..d9f13fe 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -400,33 +400,42 @@ private function escapeSpecialChars($inputString){ /** * parse - * @version 1.4.1 (2019-10-30) + * @version 1.4.2 (2021-02-08) * * @param $data {Output} * * @return {string} */ public function parse(Output $data){ - //Foreach all docs-items + //# Items foreach ( + //Foreach all docs-items $data->provider->items as $docIndex => $docData ){ //Correct price if ( + //Main price is not set empty($docData[$this->offerFields->price->docFieldName]) && + //But old price is set !empty($docData[$this->offerFields->priceOld->docFieldName]) ){ + //Use old price as main price $docData[$this->offerFields->price->docFieldName] = $docData[$this->offerFields->priceOld->docFieldName]; + //And old price is no needed unset($docData[$this->offerFields->priceOld->docFieldName]); } //Check required elements - if (!empty($docData[$this->offerFields->price->docFieldName])){ - //Save category id + if ( + //Price + !empty($docData[$this->offerFields->price->docFieldName]) + ){ + //If category is set if (is_numeric($docData[$this->offerFields->categoryId->docFieldName])){ + //Save category ID $this->categoryIds[] = $docData[$this->offerFields->categoryId->docFieldName]; } @@ -438,27 +447,37 @@ public function parse(Output $data){ ){ //Smart offer name if ($offerFieldName == 'name'){ + //TODO: Should it be moved to constructor? + //If name is not set if (empty($offerFieldData->docFieldName)){ + //Pagetitle will be used $offerFieldData->docFieldName = 'pagetitle'; } + //If name is empty if (empty($docData[$offerFieldData->docFieldName])){ + //Pagetitle will be used $docData[$offerFieldData->docFieldName] = $docData['pagetitle']; } } + //Numeric fields if ( + //If weight is set $offerFieldName == 'weight' && + //But invalid $data->provider->items[$docIndex][$offerFieldData->docFieldName] == '0' ){ + //Clear it $data->provider->items[$docIndex][$offerFieldData->docFieldName] = ''; } + if ( - //If is set + //If object field is set !empty($offerFieldData->docFieldName) && - //And data is set + //And doc data is set !empty($docData[$offerFieldData->docFieldName]) ){ //Boolean fields @@ -483,6 +502,7 @@ public function parse(Output $data){ ; } + //Fields that may be set as document IDs if ( in_array( @@ -506,17 +526,25 @@ public function parse(Output $data){ } } + //Value prefix if (isset($offerFieldData->valuePrefix)){ - $data->provider->items[$docIndex][$offerFieldData->docFieldName] = $offerFieldData->valuePrefix . $data->provider->items[$docIndex][$offerFieldData->docFieldName]; + $data->provider->items[$docIndex][$offerFieldData->docFieldName] = + $offerFieldData->valuePrefix . + $data->provider->items[$docIndex][$offerFieldData->docFieldName] + ; } //Value suffix if (isset($offerFieldData->valueSuffix)){ $data->provider->items[$docIndex][$offerFieldData->docFieldName] .= $offerFieldData->valueSuffix; } + //Try to search template by name - $templateName = 'offers_item_elem' . ucfirst($offerFieldName); + $templateName = + 'offers_item_elem' . + ucfirst($offerFieldName) + ; if (!isset($this->templates->{$templateName})){ //Default element template if (!isset($offerFieldData->templateName)){ @@ -526,6 +554,7 @@ public function parse(Output $data){ } } + if ( //If need to use template !empty($templateName) && @@ -550,12 +579,14 @@ public function parse(Output $data){ } } - //Prepare categories + + //# Categories $categoriesString = ''; $this->categoryIds = array_unique($this->categoryIds); $categoryIds_all = []; $categoryIds_last = $this->categoryIds; + //TODO: Move explosion to constructor if(!empty($this->categoryIds_last)){ $categoryIds_last = explode( ',', @@ -563,16 +594,22 @@ public function parse(Output $data){ ); } + //TODO: Avoid to use global variables + //Return result instead of global variable using $getCategories = function ($id) use ( $categoryIds_last, &$categoryIds_all, &$categoriesString, &$getCategories ){ - if(!in_array( - $id, - $categoryIds_all - )){ + if( + !in_array( + $id, + $categoryIds_all + ) + ){ + //TODO: Move getting to the condition below + //Get category doc data $category = \ddTools::getDocument( //id $id, @@ -582,12 +619,16 @@ public function parse(Output $data){ //deleted 0 ); + $categoryIds_all[] = $id; - if(!in_array( - $category['id'], - $categoryIds_last - )){ + if( + !in_array( + $category['id'], + $categoryIds_last + ) + ){ + //Result $categoriesString .= \ddTools::parseText([ 'text' => $this->templates->categories_item, 'data' => [ @@ -598,11 +639,14 @@ public function parse(Output $data){ ], 'mergeAll' => false ]); + + //Get parent category $getCategories($category['parent']); } } }; + //TODO: It is needed only if $this->categoryIds_last is not empty foreach( $this->categoryIds as $id @@ -623,6 +667,7 @@ public function parse(Output $data){ //deleted 0 ); + $categoriesString .= \ddTools::parseText([ 'text' => $this->templates->categories_item, 'data' => [ @@ -636,6 +681,7 @@ public function parse(Output $data){ $this->outputter_StringInstance->placeholders['ddGetDocuments_categories'] = $categoriesString; + //Just use the “String” class return $this->outputter_StringInstance->parse($data); } From aa0ad480039b3b10214fb8243c06addfee8d1c00 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 07:19:07 +0300 Subject: [PATCH 07/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::parse`: Refactoring, categories preparation was moved out to a standalone method. --- src/Outputter/Yandexmarket/Outputter.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index d9f13fe..4d989f6 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -400,7 +400,7 @@ private function escapeSpecialChars($inputString){ /** * parse - * @version 1.4.2 (2021-02-08) + * @version 1.4.3 (2021-02-08) * * @param $data {Output} * @@ -581,6 +581,20 @@ public function parse(Output $data){ //# Categories + $this->outputter_StringInstance->placeholders['ddGetDocuments_categories'] = $this->parse_categories(); + + + //Just use the “String” class + return $this->outputter_StringInstance->parse($data); + } + + /** + * parse_categories + * @version 1.0 (2021-02-08) + * + * @return {string} + */ + private function parse_categories(){ $categoriesString = ''; $this->categoryIds = array_unique($this->categoryIds); $categoryIds_all = []; @@ -679,10 +693,6 @@ public function parse(Output $data){ ]); } - $this->outputter_StringInstance->placeholders['ddGetDocuments_categories'] = $categoriesString; - - - //Just use the “String” class - return $this->outputter_StringInstance->parse($data); + return $categoriesString; } } \ No newline at end of file From 8ad89369e588faee23df200792f12042dcb1f485 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 07:29:05 +0300 Subject: [PATCH 08/42] + `\ddGetDocuments\Outputter\Yandexmarket\Outputter::__construct`: Added preparation of last parent category IDs. --- src/Outputter/Yandexmarket/Outputter.php | 26 +++++++++++------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 4d989f6..4863041 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -140,7 +140,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { 'offers_item_elem' => '<[+tagName+][+attrs+]>[+value+]', // 'offers_item_elemAdditionalParams' => '[+value+]', ], - $categoryIds_last + $categoryIds_last = [] ; private @@ -150,7 +150,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { /** * __construct - * @version 1.3.4 (2021-02-08) + * @version 1.4 (2021-02-08) * * @note @link https://yandex.ru/support/partnermarket/export/yml.html * @@ -240,12 +240,14 @@ function __construct($params = []){ } } - //save last parent id for category - $this->categoryIds_last = - isset($params->categoryIds_last) ? - trim($params->categoryIds_last) : - '' - ; + + //# Prepare last parent category IDs + if (!is_array($this->categoryIds_last)){ + $this->categoryIds_last = explode( + ',', + trim($this->categoryIds_last) + ); + } //# Prepare templates @@ -590,7 +592,7 @@ public function parse(Output $data){ /** * parse_categories - * @version 1.0 (2021-02-08) + * @version 1.0.1 (2021-02-08) * * @return {string} */ @@ -600,12 +602,8 @@ private function parse_categories(){ $categoryIds_all = []; $categoryIds_last = $this->categoryIds; - //TODO: Move explosion to constructor if(!empty($this->categoryIds_last)){ - $categoryIds_last = explode( - ',', - $this->categoryIds_last - ); + $categoryIds_last = $this->categoryIds_last; } //TODO: Avoid to use global variables From 4c4957b31747d680487ad32da48ace0f991792c6 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 15:31:18 +0300 Subject: [PATCH 09/42] + `\ddGetDocuments\Outputter\Yandexmarket\Outputter::__construct`: Added preparation of required `$this->offerFields->name->docFieldName`. --- src/Outputter/Yandexmarket/Outputter.php | 30 ++++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 4863041..2ca1b39 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -150,7 +150,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { /** * __construct - * @version 1.4 (2021-02-08) + * @version 1.5 (2021-02-08) * * @note @link https://yandex.ru/support/partnermarket/export/yml.html * @@ -240,6 +240,12 @@ function __construct($params = []){ } } + //If name doc field is not set + if (empty($this->offerFields->name->docFieldName)){ + //Pagetitle will be used + $this->offerFields->name->docFieldName = 'pagetitle'; + } + //# Prepare last parent category IDs if (!is_array($this->categoryIds_last)){ @@ -402,7 +408,7 @@ private function escapeSpecialChars($inputString){ /** * parse - * @version 1.4.3 (2021-02-08) + * @version 1.4.4 (2021-02-08) * * @param $data {Output} * @@ -448,19 +454,13 @@ public function parse(Output $data){ $offerFieldData ){ //Smart offer name - if ($offerFieldName == 'name'){ - //TODO: Should it be moved to constructor? - //If name is not set - if (empty($offerFieldData->docFieldName)){ - //Pagetitle will be used - $offerFieldData->docFieldName = 'pagetitle'; - } - - //If name is empty - if (empty($docData[$offerFieldData->docFieldName])){ - //Pagetitle will be used - $docData[$offerFieldData->docFieldName] = $docData['pagetitle']; - } + if ( + $offerFieldName == 'name' && + //If doc name is empty + empty($docData[$offerFieldData->docFieldName]) + ){ + //Pagetitle will be used + $docData[$offerFieldData->docFieldName] = $docData['pagetitle']; } From 3cc13a52d6aa19a4e4f4eda5ff23f0baf5b2d97a Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 15:56:39 +0300 Subject: [PATCH 10/42] + `\ddGetDocuments\Outputter\Yandexmarket\Outputter::parse_categories`: Optimization. --- src/Outputter/Yandexmarket/Outputter.php | 27 ++++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 2ca1b39..1a1d753 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -592,7 +592,7 @@ public function parse(Output $data){ /** * parse_categories - * @version 1.0.1 (2021-02-08) + * @version 1.1 (2021-02-08) * * @return {string} */ @@ -620,26 +620,25 @@ private function parse_categories(){ $categoryIds_all ) ){ - //TODO: Move getting to the condition below - //Get category doc data - $category = \ddTools::getDocument( - //id - $id, - 'pagetitle,id,parent', - //published - 'all', - //deleted - 0 - ); - $categoryIds_all[] = $id; if( !in_array( - $category['id'], + $id, $categoryIds_last ) ){ + //Get category doc data + $category = \ddTools::getDocument( + //id + $id, + 'pagetitle,id,parent', + //published + 'all', + //deleted + 0 + ); + //Result $categoriesString .= \ddTools::parseText([ 'text' => $this->templates->categories_item, From a26ddaf207701851758a4d7c467c3187d522f8b9 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 15:58:20 +0300 Subject: [PATCH 11/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::parse_categories`: Refactoring. --- src/Outputter/Yandexmarket/Outputter.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 1a1d753..035c829 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -592,19 +592,21 @@ public function parse(Output $data){ /** * parse_categories - * @version 1.1 (2021-02-08) + * @version 1.1.1 (2021-02-08) * * @return {string} */ private function parse_categories(){ $categoriesString = ''; + $this->categoryIds = array_unique($this->categoryIds); - $categoryIds_all = []; - $categoryIds_last = $this->categoryIds; - if(!empty($this->categoryIds_last)){ - $categoryIds_last = $this->categoryIds_last; - } + $categoryIds_all = []; + $categoryIds_last = + !empty($this->categoryIds_last) ? + $this->categoryIds_last : + $this->categoryIds + ; //TODO: Avoid to use global variables //Return result instead of global variable using From c24f7196b15f618b573c13303d481a40c576cef5 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 15:58:56 +0300 Subject: [PATCH 12/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::parse_categories`: Refactoring. --- src/Outputter/Yandexmarket/Outputter.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 035c829..22d47b7 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -592,12 +592,12 @@ public function parse(Output $data){ /** * parse_categories - * @version 1.1.1 (2021-02-08) + * @version 1.1.2 (2021-02-08) * * @return {string} */ private function parse_categories(){ - $categoriesString = ''; + $result = ''; $this->categoryIds = array_unique($this->categoryIds); @@ -613,7 +613,7 @@ private function parse_categories(){ $getCategories = function ($id) use ( $categoryIds_last, &$categoryIds_all, - &$categoriesString, + &$result, &$getCategories ){ if( @@ -642,7 +642,7 @@ private function parse_categories(){ ); //Result - $categoriesString .= \ddTools::parseText([ + $result .= \ddTools::parseText([ 'text' => $this->templates->categories_item, 'data' => [ 'id' => $category['id'], @@ -681,7 +681,7 @@ private function parse_categories(){ 0 ); - $categoriesString .= \ddTools::parseText([ + $result .= \ddTools::parseText([ 'text' => $this->templates->categories_item, 'data' => [ 'id' => $category['id'], @@ -692,6 +692,6 @@ private function parse_categories(){ ]); } - return $categoriesString; + return $result; } } \ No newline at end of file From 024e3a067d37ad2d167a4da6b6157e87aa6538a6 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 16:18:21 +0300 Subject: [PATCH 13/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::parse_categories`: Refactoring. --- src/Outputter/Yandexmarket/Outputter.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 22d47b7..f16427e 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -592,7 +592,7 @@ public function parse(Output $data){ /** * parse_categories - * @version 1.1.2 (2021-02-08) + * @version 1.1.3 (2021-02-08) * * @return {string} */ @@ -609,13 +609,13 @@ private function parse_categories(){ ; //TODO: Avoid to use global variables - //Return result instead of global variable using $getCategories = function ($id) use ( $categoryIds_last, &$categoryIds_all, - &$result, &$getCategories ){ + $result = ''; + if( !in_array( $id, @@ -654,9 +654,11 @@ private function parse_categories(){ ]); //Get parent category - $getCategories($category['parent']); + $result .= $getCategories($category['parent']); } } + + return $result; }; //TODO: It is needed only if $this->categoryIds_last is not empty @@ -664,7 +666,7 @@ private function parse_categories(){ $this->categoryIds as $id ){ - $getCategories($id); + $result .= $getCategories($id); } foreach( From b6ce3bdae95e0dedd4804ffd0ce092c8a8eb210a Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 16:31:49 +0300 Subject: [PATCH 14/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::parse_categories`: Refactoring. --- src/Outputter/Yandexmarket/Outputter.php | 72 +++++++++++------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index f16427e..f55aa4e 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -408,7 +408,7 @@ private function escapeSpecialChars($inputString){ /** * parse - * @version 1.4.4 (2021-02-08) + * @version 1.5 (2021-02-08) * * @param $data {Output} * @@ -581,6 +581,8 @@ public function parse(Output $data){ } } + $this->categoryIds = array_unique($this->categoryIds); + //# Categories $this->outputter_StringInstance->placeholders['ddGetDocuments_categories'] = $this->parse_categories(); @@ -592,25 +594,17 @@ public function parse(Output $data){ /** * parse_categories - * @version 1.1.3 (2021-02-08) + * @version 1.1.4 (2021-02-08) * * @return {string} */ private function parse_categories(){ $result = ''; - $this->categoryIds = array_unique($this->categoryIds); - $categoryIds_all = []; - $categoryIds_last = - !empty($this->categoryIds_last) ? - $this->categoryIds_last : - $this->categoryIds - ; //TODO: Avoid to use global variables $getCategories = function ($id) use ( - $categoryIds_last, &$categoryIds_all, &$getCategories ){ @@ -625,9 +619,12 @@ private function parse_categories(){ $categoryIds_all[] = $id; if( + //If root categories are set + !empty($this->categoryIds_last) && + //And it is not one of the “root” category !in_array( $id, - $categoryIds_last + $this->categoryIds_last ) ){ //Get category doc data @@ -641,7 +638,6 @@ private function parse_categories(){ 0 ); - //Result $result .= \ddTools::parseText([ 'text' => $this->templates->categories_item, 'data' => [ @@ -655,45 +651,43 @@ private function parse_categories(){ //Get parent category $result .= $getCategories($category['parent']); + }else{ + //Get category doc data + $category = \ddTools::getDocument( + //id + $id, + 'pagetitle,id,parent', + //published + 'all', + //deleted + 0 + ); + + $result .= \ddTools::parseText([ + 'text' => $this->templates->categories_item, + 'data' => [ + 'id' => $category['id'], + 'value' => $this->escapeSpecialChars($category['pagetitle']), + 'parent' => $category['parent'] + ], + 'mergeAll' => false + ]); } } return $result; }; - //TODO: It is needed only if $this->categoryIds_last is not empty foreach( - $this->categoryIds as + array_unique(array_merge( + $this->categoryIds, + $this->categoryIds_last + )) as $id ){ $result .= $getCategories($id); } - foreach( - $categoryIds_last as - $id - ){ - $category = \ddTools::getDocument( - //id - $id, - 'pagetitle,id,parent', - //published - 'all', - //deleted - 0 - ); - - $result .= \ddTools::parseText([ - 'text' => $this->templates->categories_item, - 'data' => [ - 'id' => $category['id'], - 'value' => $this->escapeSpecialChars($category['pagetitle']), - 'parent' => $category['parent'] - ], - 'mergeAll' => false - ]); - } - return $result; } } \ No newline at end of file From 413dde399e13b915592ed87a43db404d286c2f4c Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 16:33:25 +0300 Subject: [PATCH 15/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::parse_categories`: Refactoring. --- src/Outputter/Yandexmarket/Outputter.php | 35 ++++++++---------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index f55aa4e..4a98653 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -594,7 +594,7 @@ public function parse(Output $data){ /** * parse_categories - * @version 1.1.4 (2021-02-08) + * @version 1.1.5 (2021-02-08) * * @return {string} */ @@ -618,6 +618,17 @@ private function parse_categories(){ ){ $categoryIds_all[] = $id; + //Get category doc data + $category = \ddTools::getDocument( + //id + $id, + 'pagetitle,id,parent', + //published + 'all', + //deleted + 0 + ); + if( //If root categories are set !empty($this->categoryIds_last) && @@ -627,17 +638,6 @@ private function parse_categories(){ $this->categoryIds_last ) ){ - //Get category doc data - $category = \ddTools::getDocument( - //id - $id, - 'pagetitle,id,parent', - //published - 'all', - //deleted - 0 - ); - $result .= \ddTools::parseText([ 'text' => $this->templates->categories_item, 'data' => [ @@ -652,17 +652,6 @@ private function parse_categories(){ //Get parent category $result .= $getCategories($category['parent']); }else{ - //Get category doc data - $category = \ddTools::getDocument( - //id - $id, - 'pagetitle,id,parent', - //published - 'all', - //deleted - 0 - ); - $result .= \ddTools::parseText([ 'text' => $this->templates->categories_item, 'data' => [ From 13e95a47ebdedd59f8c399c34b14f94dbb360140 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 16:34:38 +0300 Subject: [PATCH 16/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::parse_categories`: Refactoring. --- src/Outputter/Yandexmarket/Outputter.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 4a98653..7546ab7 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -594,7 +594,7 @@ public function parse(Output $data){ /** * parse_categories - * @version 1.1.5 (2021-02-08) + * @version 1.1.6 (2021-02-08) * * @return {string} */ @@ -619,7 +619,7 @@ private function parse_categories(){ $categoryIds_all[] = $id; //Get category doc data - $category = \ddTools::getDocument( + $categoryDocData = \ddTools::getDocument( //id $id, 'pagetitle,id,parent', @@ -641,23 +641,23 @@ private function parse_categories(){ $result .= \ddTools::parseText([ 'text' => $this->templates->categories_item, 'data' => [ - 'id' => $category['id'], - 'value' => $this->escapeSpecialChars($category['pagetitle']), - 'parent' => $category['parent'], - 'attrs' => ' parentId="' . $category['parent'] . '"' + 'id' => $categoryDocData['id'], + 'value' => $this->escapeSpecialChars($categoryDocData['pagetitle']), + 'parent' => $categoryDocData['parent'], + 'attrs' => ' parentId="' . $categoryDocData['parent'] . '"' ], 'mergeAll' => false ]); //Get parent category - $result .= $getCategories($category['parent']); + $result .= $getCategories($categoryDocData['parent']); }else{ $result .= \ddTools::parseText([ 'text' => $this->templates->categories_item, 'data' => [ - 'id' => $category['id'], - 'value' => $this->escapeSpecialChars($category['pagetitle']), - 'parent' => $category['parent'] + 'id' => $categoryDocData['id'], + 'value' => $this->escapeSpecialChars($categoryDocData['pagetitle']), + 'parent' => $categoryDocData['parent'] ], 'mergeAll' => false ]); From 1055f0c359b40ea7e45260ba94209fa1f95f7a88 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 8 Feb 2021 16:43:02 +0300 Subject: [PATCH 17/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::parse_categories`: Refactoring. --- src/Outputter/Yandexmarket/Outputter.php | 43 +++++++++++------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 7546ab7..d43ed01 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -594,7 +594,7 @@ public function parse(Output $data){ /** * parse_categories - * @version 1.1.6 (2021-02-08) + * @version 1.1.7 (2021-02-08) * * @return {string} */ @@ -629,7 +629,7 @@ private function parse_categories(){ 0 ); - if( + $hasParentCategory = //If root categories are set !empty($this->categoryIds_last) && //And it is not one of the “root” category @@ -637,30 +637,25 @@ private function parse_categories(){ $id, $this->categoryIds_last ) - ){ - $result .= \ddTools::parseText([ - 'text' => $this->templates->categories_item, - 'data' => [ - 'id' => $categoryDocData['id'], - 'value' => $this->escapeSpecialChars($categoryDocData['pagetitle']), - 'parent' => $categoryDocData['parent'], - 'attrs' => ' parentId="' . $categoryDocData['parent'] . '"' - ], - 'mergeAll' => false - ]); - + ; + + $result .= \ddTools::parseText([ + 'text' => $this->templates->categories_item, + 'data' => [ + 'id' => $categoryDocData['id'], + 'value' => $this->escapeSpecialChars($categoryDocData['pagetitle']), + 'parent' => $categoryDocData['parent'], + 'attrs' => + $hasParentCategory ? + ' parentId="' . $categoryDocData['parent'] . '"' : + '' + ], + 'mergeAll' => false + ]); + + if($hasParentCategory){ //Get parent category $result .= $getCategories($categoryDocData['parent']); - }else{ - $result .= \ddTools::parseText([ - 'text' => $this->templates->categories_item, - 'data' => [ - 'id' => $categoryDocData['id'], - 'value' => $this->escapeSpecialChars($categoryDocData['pagetitle']), - 'parent' => $categoryDocData['parent'] - ], - 'mergeAll' => false - ]); } } From 57fde5059e0d9ce661411e2976051baf29d783f4 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Tue, 9 Feb 2021 20:27:07 +0300 Subject: [PATCH 18/42] * `\ddGetDocuments\Outputter\Yandexmarket\Outputter::parse`: Fixed smart name and price preparation. --- README.md | 6 ++-- src/Outputter/Yandexmarket/Outputter.php | 38 ++++++++++++++++-------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5f38395..60dea4e 100644 --- a/README.md +++ b/README.md @@ -340,7 +340,8 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). * **Required** * `outputterParams->offerFields_price` - * Desctription: A document field name, that contains offer price. + * Desctription: A document field name, that contains offer price. + If a document field value is empty, but `outputterParams->offerFields_priceOld` is set, the last will be used instead. * Valid values: `stringTvName` * **Required** @@ -355,7 +356,8 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). * Default value: — * `outputterParams->offerFields_name` - * Desctription: A document field name, that contains offer name. + * Desctription: A document field name, that contains offer name. + If a document field value is empty, the `pagetitle` field will be used instead. * Valid values: * `stringDocFieldName` * `stringTvName` diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index d43ed01..ea380af 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -6,6 +6,14 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { protected + /** + * @property $docFields {array} — Document fields including TVs used in the output. + */ + $docFields = [ + 'id', + //May need for smart name + 'pagetitle' + ], /** * @property $templates {stdClass} * @property $templates->wrapper {string} @@ -408,7 +416,7 @@ private function escapeSpecialChars($inputString){ /** * parse - * @version 1.5 (2021-02-08) + * @version 1.5.1 (2021-02-09) * * @param $data {Output} * @@ -420,7 +428,8 @@ public function parse(Output $data){ //Foreach all docs-items $data->provider->items as $docIndex => - $docData + //Value must be assigned by reference for modifying inside the cycle + &$docData ){ //Correct price if ( @@ -469,10 +478,10 @@ public function parse(Output $data){ //If weight is set $offerFieldName == 'weight' && //But invalid - $data->provider->items[$docIndex][$offerFieldData->docFieldName] == '0' + $docData[$offerFieldData->docFieldName] == '0' ){ //Clear it - $data->provider->items[$docIndex][$offerFieldData->docFieldName] = ''; + $docData[$offerFieldData->docFieldName] = ''; } @@ -497,7 +506,7 @@ public function parse(Output $data){ $docData[$offerFieldData->docFieldName] !== 'false' ) ){ - $data->provider->items[$docIndex][$offerFieldData->docFieldName] = + $docData[$offerFieldData->docFieldName] = (bool) $docData[$offerFieldData->docFieldName] ? 'true' : 'false' @@ -515,30 +524,30 @@ public function parse(Output $data){ ] ) && //Set as document id - is_numeric($data->provider->items[$docIndex][$offerFieldData->docFieldName]) + is_numeric($docData[$offerFieldData->docFieldName]) ){ //Try to get pagetitle $docData_fieldToGet_data = \ddTools::getDocument( - $data->provider->items[$docIndex][$offerFieldData->docFieldName], + $docData[$offerFieldData->docFieldName], 'pagetitle' ); //If success if (is_array($docData_fieldToGet_data)){ - $data->provider->items[$docIndex][$offerFieldData->docFieldName] = $docData_fieldToGet_data['pagetitle']; + $docData[$offerFieldData->docFieldName] = $docData_fieldToGet_data['pagetitle']; } } //Value prefix if (isset($offerFieldData->valuePrefix)){ - $data->provider->items[$docIndex][$offerFieldData->docFieldName] = + $docData[$offerFieldData->docFieldName] = $offerFieldData->valuePrefix . - $data->provider->items[$docIndex][$offerFieldData->docFieldName] + $docData[$offerFieldData->docFieldName] ; } //Value suffix if (isset($offerFieldData->valueSuffix)){ - $data->provider->items[$docIndex][$offerFieldData->docFieldName] .= $offerFieldData->valueSuffix; + $docData[$offerFieldData->docFieldName] .= $offerFieldData->valueSuffix; } @@ -564,11 +573,11 @@ public function parse(Output $data){ !empty($offerFieldData->tagName) ){ //Final element parsing - $data->provider->items[$docIndex][$offerFieldData->docFieldName] = \ddTools::parseText([ + $docData[$offerFieldData->docFieldName] = \ddTools::parseText([ 'text' => $this->templates->{$templateName}, 'data' => [ 'tagName' => $offerFieldData->tagName, - 'value' => $this->escapeSpecialChars($data->provider->items[$docIndex][$offerFieldData->docFieldName]) + 'value' => $this->escapeSpecialChars($docData[$offerFieldData->docFieldName]) ], 'mergeAll' => false ]); @@ -581,6 +590,9 @@ public function parse(Output $data){ } } + //Destroy unused referenced variable + unset($docData); + $this->categoryIds = array_unique($this->categoryIds); From d353f5eeb6a58fddaa1c0412c04f1ec4993b92d5 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Wed, 10 Feb 2021 11:42:05 +0300 Subject: [PATCH 19/42] =?UTF-8?q?+=20README=20=E2=86=92=C2=A0Parameters=20?= =?UTF-8?q?description=20=E2=86=92=C2=A0Extenders=20parameters=20=E2=86=92?= =?UTF-8?q?=20`extendersParams->{$extenderName}`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 60dea4e..6d2e561 100644 --- a/README.md +++ b/README.md @@ -566,6 +566,11 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). * `stirngJsonObject` — as [JSON](https://en.wikipedia.org/wiki/JSON) * `stringQueryFormated` — as [Query string](https://en.wikipedia.org/wiki/Query_string) * Default value: — + +* `extendersParams->{$extenderName}` + * Desctription: Parameters of an extender, when the key is the extender name and the value is the extender parameters. + * Valid values: `object + * Default value: — ##### Extenders → Pagination (``&extenders=`pagination` ``) From c1920f980b481ffed3203b4120b97b1d521eb581 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Wed, 10 Feb 2021 12:00:14 +0300 Subject: [PATCH 20/42] * Snippet: `\DDTools\ObjectTools::convertType` is used istead of `\ddTools::encodedStringToArray`. --- ddGetDocuments_snippet.php | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index 69cab74..fefa0b3 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -116,17 +116,26 @@ if(class_exists($dataProviderClass)){ //Prepare provider params - $providerParams = \ddTools::encodedStringToArray($providerParams); + $providerParams = \DDTools\ObjectTools::convertType([ + 'object' => $providerParams, + 'type' => 'objectStdClass' + ]); //Backward compatibility with <= 1.1 if (isset($orderBy)){ - $providerParams['orderBy'] = $orderBy; + $providerParams->orderBy = $orderBy; } //Prepare extender params - $extendersParams = (object) \ddTools::encodedStringToArray($extendersParams); + $extendersParams = \DDTools\ObjectTools::convertType([ + 'object' => $extendersParams, + 'type' => 'objectStdClass' + ]); //Prepare outputter params - $outputterParams = \ddTools::encodedStringToArray($outputterParams); + $outputterParams = \DDTools\ObjectTools::convertType([ + 'object' => $outputterParams, + 'type' => 'objectStdClass' + ]); if(!empty($extenders)){ //If we have a single extender then make sure that extender params set as an array @@ -151,11 +160,11 @@ } //Make sure orderBy and filter looks like SQL - if (!empty($providerParams['orderBy'])){ - $providerParams['orderBy'] = str_replace( + if (!empty($providerParams->orderBy)){ + $providerParams->orderBy = str_replace( $fieldDelimiter, '`', - $providerParams['orderBy'] + $providerParams->orderBy ); } $filter = str_replace( From cbdef28d04b45d1e0a48d660dba99e6e31e386ad Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Wed, 10 Feb 2021 15:54:07 +0300 Subject: [PATCH 21/42] * Snippet: Refactoring, the `$params` variable is used instead of a standalone variable for each parameter. --- ddGetDocuments_snippet.php | 174 ++++++++++++++++--------------------- 1 file changed, 76 insertions(+), 98 deletions(-) diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index fefa0b3..3f5114b 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -41,147 +41,125 @@ } //Backward compatibility -extract(\ddTools::verifyRenamedParams([ +$params = \ddTools::verifyRenamedParams([ 'params' => $params, 'compliance' => [ 'outputter' => 'outputFormat', 'outputterParams' => 'outputFormatParams' + ], + 'returnCorrectedOnly' => false +]); + +//Defaults +$params = \DDTools\ObjectTools::extend([ + 'objects' => [ + (object) [ + //General + 'total' => NULL, + 'offset' => 0, + 'filter' => NULL, + 'fieldDelimiter' => '', + + //Data provider + 'provider' => 'parent', + 'providerParams' => '', + + //Outputter + 'outputter' => 'string', + 'outputterParams' => '', + + //Extenders + 'extenders' => [], + 'extendersParams' => '' + ], + $params ] -])); +]); -//General -$total = - isset($total) ? - $total : - null -; -$offset = - isset($offset) ? - $offset : - 0 -; -$filter = - isset($filter) ? - $filter : - null -; -$fieldDelimiter = - isset($fieldDelimiter) ? - $fieldDelimiter : - '`' -; +$dataProviderClass = \ddGetDocuments\DataProvider\DataProvider::includeProviderByName($params->provider); -//Data provider -$provider = - isset($provider) ? - $provider : - 'parent' -; -$dataProviderClass = \ddGetDocuments\DataProvider\DataProvider::includeProviderByName($provider); -$providerParams = - isset($providerParams) ? - $providerParams : - '' -; +//TODO: Is it needed? +$params->outputter = strtolower($params->outputter); -//Output format -$outputter = - isset($outputter) ? - strtolower($outputter) : - 'string' -; -$outputterParams = - isset($outputterParams) ? - $outputterParams : - '' -; - -//Extenders -$extenders = - ( - isset($extenders) && - !empty($extenders) - ) ? - explode( - ',', - trim($extenders) - ) : - [] -; -$extendersParams = - isset($extendersParams) ? - $extendersParams : - '' -; +if (is_string($params->extenders)){ + if (!empty($params->extenders)){ + $params->extenders = explode( + ',', + trim($params->extenders) + ); + }else{ + $params->extenders = []; + } +} if(class_exists($dataProviderClass)){ //Prepare provider params - $providerParams = \DDTools\ObjectTools::convertType([ - 'object' => $providerParams, + $params->providerParams = \DDTools\ObjectTools::convertType([ + 'object' => $params->providerParams, 'type' => 'objectStdClass' ]); //Backward compatibility with <= 1.1 - if (isset($orderBy)){ - $providerParams->orderBy = $orderBy; + if (isset($params->orderBy)){ + $params->providerParams->orderBy = $params->orderBy; } //Prepare extender params - $extendersParams = \DDTools\ObjectTools::convertType([ - 'object' => $extendersParams, + $params->extendersParams = \DDTools\ObjectTools::convertType([ + 'object' => $params->extendersParams, 'type' => 'objectStdClass' ]); //Prepare outputter params - $outputterParams = \DDTools\ObjectTools::convertType([ - 'object' => $outputterParams, + $params->outputterParams = \DDTools\ObjectTools::convertType([ + 'object' => $params->outputterParams, 'type' => 'objectStdClass' ]); - if(!empty($extenders)){ + if(!empty($params->extenders)){ //If we have a single extender then make sure that extender params set as an array //like [extenderName => [extenderParameter_1, extenderParameter_2, ...]] - if(count($extenders) === 1){ - if(!isset($extendersParams->{$extenders[0]})){ - $extendersParams = (object) [ - $extenders[0] => $extendersParams + if(count($params->extenders) === 1){ + if(!isset($params->extendersParams->{$params->extenders[0]})){ + $params->extendersParams = (object) [ + $params->extenders[0] => $params->extendersParams ]; } }else{ - //Make sure that for each extender there is an item in $extendersParams + //Make sure that for each extender there is an item in $params->extendersParams foreach( - $extenders as + $params->extenders as $extenderName ){ - if(!isset($extendersParams->{$extenderName})){ - $extendersParams->{$extenderName} = []; + if(!isset($params->extendersParams->{$extenderName})){ + $params->extendersParams->{$extenderName} = []; } } } } //Make sure orderBy and filter looks like SQL - if (!empty($providerParams->orderBy)){ - $providerParams->orderBy = str_replace( - $fieldDelimiter, + if (!empty($params->providerParams->orderBy)){ + $params->providerParams->orderBy = str_replace( + $params->fieldDelimiter, '`', - $providerParams->orderBy + $params->providerParams->orderBy ); } - $filter = str_replace( - $fieldDelimiter, + $params->filter = str_replace( + $params->fieldDelimiter, '`', - $filter + $params->filter ); $input = new \ddGetDocuments\Input([ 'snippetParams' => [ - 'offset' => $offset, - 'total' => $total, - 'filter' => $filter + 'offset' => $params->offset, + 'total' => $params->total, + 'filter' => $params->filter ], - 'providerParams' => $providerParams, - 'extendersParams' => $extendersParams, - 'outputterParams' => $outputterParams + 'providerParams' => $params->providerParams, + 'extendersParams' => $params->extendersParams, + 'outputterParams' => $params->outputterParams ]); //Extenders storage @@ -189,7 +167,7 @@ //Iterate through all extenders to create their instances foreach( - $extenders as + $params->extenders as $extenderName ){ $extenderObject = \ddGetDocuments\Extender\Extender::createChildInstance([ @@ -213,11 +191,11 @@ $dataProviderObject = new $dataProviderClass($input); - if ($outputter != 'raw'){ + if ($params->outputter != 'raw'){ $input->outputterParams->dataProvider = $dataProviderObject; $outputterObject = \ddGetDocuments\Outputter\Outputter::createChildInstance([ - 'name' => $outputter, + 'name' => $params->outputter, 'parentDir' => $snippetPath_src . 'Outputter' @@ -240,7 +218,7 @@ $outputData->extenders[$extenderName] = $extenderObject->applyToOutput($dataProviderResult); } - if ($outputter == 'raw'){ + if ($params->outputter == 'raw'){ $snippetResult = $outputData; }else{ $snippetResult = $outputterObject->parse($outputData); From 9f88700985e834b37255aa29da87ec05faad57fd Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Fri, 12 Feb 2021 11:58:46 +0300 Subject: [PATCH 22/42] + `\ddGetDocuments\Input`: Parameters preparation code moved here from the snippet. --- ddGetDocuments_snippet.php | 98 ++------------------ src/Input.php | 184 ++++++++++++++++++++++++++++++++----- 2 files changed, 171 insertions(+), 111 deletions(-) diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index 3f5114b..3b88155 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -76,99 +76,19 @@ ] ]); -$dataProviderClass = \ddGetDocuments\DataProvider\DataProvider::includeProviderByName($params->provider); +$input = new \ddGetDocuments\Input($params); -//TODO: Is it needed? -$params->outputter = strtolower($params->outputter); - -if (is_string($params->extenders)){ - if (!empty($params->extenders)){ - $params->extenders = explode( - ',', - trim($params->extenders) - ); - }else{ - $params->extenders = []; - } -} +$dataProviderClass = \ddGetDocuments\DataProvider\DataProvider::includeProviderByName($input->provider); if(class_exists($dataProviderClass)){ - //Prepare provider params - $params->providerParams = \DDTools\ObjectTools::convertType([ - 'object' => $params->providerParams, - 'type' => 'objectStdClass' - ]); - - //Backward compatibility with <= 1.1 - if (isset($params->orderBy)){ - $params->providerParams->orderBy = $params->orderBy; - } - - //Prepare extender params - $params->extendersParams = \DDTools\ObjectTools::convertType([ - 'object' => $params->extendersParams, - 'type' => 'objectStdClass' - ]); - //Prepare outputter params - $params->outputterParams = \DDTools\ObjectTools::convertType([ - 'object' => $params->outputterParams, - 'type' => 'objectStdClass' - ]); - - if(!empty($params->extenders)){ - //If we have a single extender then make sure that extender params set as an array - //like [extenderName => [extenderParameter_1, extenderParameter_2, ...]] - if(count($params->extenders) === 1){ - if(!isset($params->extendersParams->{$params->extenders[0]})){ - $params->extendersParams = (object) [ - $params->extenders[0] => $params->extendersParams - ]; - } - }else{ - //Make sure that for each extender there is an item in $params->extendersParams - foreach( - $params->extenders as - $extenderName - ){ - if(!isset($params->extendersParams->{$extenderName})){ - $params->extendersParams->{$extenderName} = []; - } - } - } - } - - //Make sure orderBy and filter looks like SQL - if (!empty($params->providerParams->orderBy)){ - $params->providerParams->orderBy = str_replace( - $params->fieldDelimiter, - '`', - $params->providerParams->orderBy - ); - } - $params->filter = str_replace( - $params->fieldDelimiter, - '`', - $params->filter - ); - - $input = new \ddGetDocuments\Input([ - 'snippetParams' => [ - 'offset' => $params->offset, - 'total' => $params->total, - 'filter' => $params->filter - ], - 'providerParams' => $params->providerParams, - 'extendersParams' => $params->extendersParams, - 'outputterParams' => $params->outputterParams - ]); - //Extenders storage $extendersStorage = []; //Iterate through all extenders to create their instances foreach( - $params->extenders as - $extenderName + $input->extendersParams as + $extenderName => + $extenderParams ){ $extenderObject = \ddGetDocuments\Extender\Extender::createChildInstance([ 'name' => $extenderName, @@ -177,7 +97,7 @@ 'Extender' , //Passing parameters into constructor - 'params' => $input->extendersParams->{$extenderName} + 'params' => $extenderParams ]); //Passing a link to the storage $extendersStorage[$extenderName] = $extenderObject; @@ -191,11 +111,11 @@ $dataProviderObject = new $dataProviderClass($input); - if ($params->outputter != 'raw'){ + if ($input->outputter != 'raw'){ $input->outputterParams->dataProvider = $dataProviderObject; $outputterObject = \ddGetDocuments\Outputter\Outputter::createChildInstance([ - 'name' => $params->outputter, + 'name' => $input->outputter, 'parentDir' => $snippetPath_src . 'Outputter' @@ -218,7 +138,7 @@ $outputData->extenders[$extenderName] = $extenderObject->applyToOutput($dataProviderResult); } - if ($params->outputter == 'raw'){ + if ($input->outputter == 'raw'){ $snippetResult = $outputData; }else{ $snippetResult = $outputterObject->parse($outputData); diff --git a/src/Input.php b/src/Input.php index 96abe2d..afd5c0b 100644 --- a/src/Input.php +++ b/src/Input.php @@ -3,40 +3,180 @@ class Input extends \DDTools\BaseClass { - /** - * @property $snippetParams {stdClass} - * @property $extendersParams {stdClass} - * @property $providerParams {stdClass} - * @property $outputterParams {stdClass} - */ public + /** + * @property $snippetParams {stdClass} + * @property $snippetParams->offset {integer} + * @property $snippetParams->total {integer} + * @property $snippetParams->filter {string} + * @property $snippetParams->fieldDelimiter {string} + */ $snippetParams, - $extendersParams, + + /** + * @property $provider {string} + * @property $providerParams {stdClass} + * @property $providerParams->{$paramName} {mixed} + */ + $provider = 'parent', $providerParams, - $outputterParams + + /** + * @property $outputter {string} + * @property $outputterParams {stdClass} + * @property $outputterParams->{$paramName} {mixed} + */ + $outputter = 'string', + $outputterParams, + + /** + * @property $extendersParams {stdClass} + * @property $extendersParams->{$extenderName} {stdClass} + * @property $extendersParams->{$extenderName}->{$paramName} {mixed} + */ + $extendersParams ; /** * __construct - * @version 3.0 (2020-03-11) + * @version 4.0 (2021-02-12) * - * @param $params {stdClass|arrayAssociative} — The object of params. @required - * @param $params->snippetParams {stdClass|arrayAssociative} — @required - * @param $params->providerParams {stdClass|arrayAssociative} — @required - * @param $params->extendersParams {stdClass|arrayAssociative} — @required - * @param $params->outputterParams {stdClass|arrayAssociative} — @required + * @param $snippetParams {stdClass} — The object of parameters. @required + * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} — @required + * @param $snippetParams->extendersParams {stdClass|arrayAssociative|stringJsonObject} — @required + * @param $snippetParams->outputterParams {stdClass|arrayAssociative|stringJsonObject} — @required */ - public function __construct($params){ - //Set object properties from parameters - $this->setExistingProps($params); + public function __construct($snippetParams){ + //Save snippet parameters and prapare them later + $this->snippetParams = $snippetParams; - //All property types must be stdClass + + //Prepare provider, outputter and extender params foreach ( - $this as - $propertyName => - $propertyValue + [ + 'providerParams', + 'outputterParams', + 'extendersParams' + ] as + $paramName + ){ + $this->{$paramName} = \DDTools\ObjectTools::convertType([ + 'object' => $this->snippetParams->{$paramName}, + 'type' => 'objectStdClass' + ]); + + //No needed in snippet params + unset($this->snippetParams->{$paramName}); + } + + + //Backward compatibility + $this->paramsBackwardCompatibility(); + + + //Set object properties from snippet parameters + $this->setExistingProps($this->snippetParams); + + + $this->prepareExtendersParams(); + + + //TODO: Is it needed? + $this->outputter = strtolower($this->outputter); + + + //Make sure orderBy and filter looks like SQL + if (!empty($this->providerParams->orderBy)){ + $this->providerParams->orderBy = str_replace( + $this->snippetParams->fieldDelimiter, + '`', + $this->providerParams->orderBy + ); + } + $this->snippetParams->filter = str_replace( + $this->snippetParams->fieldDelimiter, + '`', + $this->snippetParams->filter + ); + } + + /** + * prepareExtendersParams + * @version 1.0 (2021-02-12) + * + * @desc Prepare extenders params. + * + * @return {void} + */ + private function prepareExtendersParams(){ + //Prepare extenders + if (is_string($this->snippetParams->extenders)){ + if (!empty($this->snippetParams->extenders)){ + $this->snippetParams->extenders = explode( + ',', + trim($this->snippetParams->extenders) + ); + }else{ + $this->snippetParams->extenders = []; + } + } + + //Prepare extenders params + if(!empty($this->snippetParams->extenders)){ + //If we have a single extender then make sure that extender params set as an array + //like [extenderName => [extenderParameter_1, extenderParameter_2, ...]] + if(count($this->snippetParams->extenders) === 1){ + if( + !\DDTools\ObjectTools::isPropExists([ + 'object' => $this->extendersParams, + 'propName' => $this->snippetParams->extenders[0] + ]) + ){ + $this->extendersParams = (object) [ + $this->snippetParams->extenders[0] => $this->extendersParams + ]; + } + }else{ + //Make sure that for each extender there is an item in $this->extendersParams + foreach( + $this->snippetParams->extenders as + $extenderName + ){ + if( + !\DDTools\ObjectTools::isPropExists([ + 'object' => $this->extendersParams, + 'propName' => $extenderName + ]) + ){ + $this->extendersParams->{$extenderName} = (object) []; + } + } + } + } + + //No needed anymore, all data was saved to $this->extendersParams + unset($this->snippetParams->extenders); + } + + /** + * paramsBackwardCompatibility + * @version 1.0 (2021-02-12) + * + * @desc Prepare snippet params preserve backward compatibility. + * + * @return {void} + */ + private function paramsBackwardCompatibility(){ + //Backward compatibility with <= 1.1 + if ( + \DDTools\ObjectTools::isPropExists([ + 'object' => $this->snippetParams, + 'propName' => 'orderBy' + ]) ){ - $this->{$propertyName} = (object) $propertyValue; + $this->providerParams->orderBy = $this->snippetParams->orderBy; + + unset($this->snippetParams->orderBy); } } } \ No newline at end of file From 64718bec12f725ecfae5860642be6a7f201cfb04 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 15 Feb 2021 11:01:03 +0300 Subject: [PATCH 23/42] =?UTF-8?q?*=20Parameters:=20The=20following=20param?= =?UTF-8?q?eters=20were=20moved=20from=20Snippet=20to=20Provider=20(with?= =?UTF-8?q?=20backward=20compatibility):=20=09*=20`filter`=20=E2=86=92=20`?= =?UTF-8?q?providerParams->filter`.=20=09*=20`offset`=20=E2=86=92=20`provi?= =?UTF-8?q?derParams->offset`.=20=09*=20`total`=20=E2=86=92=20`providerPar?= =?UTF-8?q?ams->total`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 108 ++++++++++++++------------- ddGetDocuments_snippet.php | 6 -- src/DataProvider/DataProvider.php | 20 +---- src/Extender/Extender.php | 12 --- src/Extender/Pagination/Extender.php | 28 +++---- src/Extender/Search/Extender.php | 20 ++--- src/Extender/Tagging/Extender.php | 20 ++--- src/Input.php | 63 ++++++++++------ 8 files changed, 135 insertions(+), 142 deletions(-) diff --git a/README.md b/README.md index 6d2e561..a5f5751 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,14 @@ A snippet for fetching and parsing resources from the document tree or custom DB ### Parameters description +#### Core parameters + +* `fieldDelimiter` + * Desctription: The field delimiter to be used in order to distinct data base column names in those parameters which can contain SQL queries directly, e. g. `providerParams->orderBy` and `providerParams->filter`. + * Valid values: `string` + * Default value: ``'`'`` + + #### Data provider parameters * `provider` @@ -52,6 +60,22 @@ A snippet for fetching and parsing resources from the document tree or custom DB * `stringQueryFormated` — as [Query string](https://en.wikipedia.org/wiki/Query_string) * Default value: — +* `providerParams->filter` + * Desctription: The filter condition in SQL-style to be applied while resource fetching. + Notice that all fields/tvs names specified in the filter parameter must be wrapped in `fieldDelimiter`. + * Valid values: `string` + * Default value: ``'`published` = 1 AND `deleted` = 0'`` + +* `providerParams->total` + * Desctription: The maximum number of the resources that will be returned. + * Valid values: `integer` + * Default value: — (all) + +* `providerParams->offset` + * Desctription: Resources offset. + * Valid values: `integer` + * Default value: `0` + * `providerParams->orderBy` * Desctription: A string representing the sorting rule. TV names also can be used. @@ -116,30 +140,6 @@ Get resources from custom DB table. * **Required** -#### Core parameters - -* `fieldDelimiter` - * Desctription: The field delimiter to be used in order to distinct data base column names in those parameters which can contain SQL queries directly, e. g. `providerParams->orderBy` and `filter`. - * Valid values: `string` - * Default value: ``'`'`` - -* `filter` - * Desctription: The filter condition in SQL-style to be applied while resource fetching. - Notice that all fields/tvs names specified in the filter parameter must be wrapped in `fieldDelimiter`. - * Valid values: `string` - * Default value: ``'`published` = 1 AND `deleted` = 0'`` - -* `total` - * Desctription: The maximum number of the resources that will be returned. - * Valid values: `integer` - * Default value: — (all) - -* `offset` - * Desctription: Resources offset. - * Valid values: `integer` - * Default value: `0` - - #### Output format parameters * `outputter` @@ -738,7 +738,7 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). ``` -#### Simple fetching child documents from a parent with ID = `1` with the `filter` +#### Simple fetching child documents from a parent with ID = `1` with the `providerParams->filter` Add a filter that would not output everything. Let's say we need only published documents. @@ -746,12 +746,12 @@ _Don't forget about `fieldDelimiter`._ ``` [[ddGetDocuments? + &fieldDelimiter=`#` &providerParams=`{ "parentIds": "1", - "depth": 1 + "depth": 1, + "filter": "#published# = 1" }` - &fieldDelimiter=`#` - &filter=`#published# = 1` &outputterParams=`{ "itemTpl": "documents_item" }` @@ -761,27 +761,37 @@ _Don't forget about `fieldDelimiter`._ So we can filter as much as we like (we can use `AND` and `OR`, doucument fields and TVs): ``` -&filter=` - #published# = 1 AND - #hidemenu# = 0 OR - #SomeTVName# = 1 -` +[[ddGetDocuments? + &fieldDelimiter=`#` + &providerParams=`{ + "parentIds": "1", + "depth": 1, + "filter": "#published# = 1 AND #hidemenu# = 0 OR #SomeTVName# = 1" + }` + &outputterParams=`{ + "itemTpl": "documents_item" + }` +]] ``` -#### Sorting by TV with the `date` type (`orderBy`) +#### Sorting by TV with the `date` type (`providerParams->orderBy`) Dates in DB stored in specific format (`01-02-2017 08:59:45`) and sorting works unexpectedly at first sight. So, we can't just type: ``` -&orderBy=`#TVName# DESC` +&providerParams=`{ + "orderBy": "#TVName# DESC" +}` ``` For correct working we need to convert date from DB to Unixtime for sorting: ``` -&orderBy=`STR_TO_DATE(#TVName#, '%d-%m-%Y %H:%i:%s') DESC` +&providerParams=`{ + "orderBy": "STR_TO_DATE(#TVName#, '%d-%m-%Y %H:%i:%s') DESC" +}` ``` When `TVName` — TV name for sorting by. @@ -849,13 +859,13 @@ Returns: ``` [[ddGetDocuments? + &fieldDelimiter=`#` &providerParams=`{ - "parentIds": "[*id*]" + "parentIds": "[*id*]", + "filter": "#published# = 1", + "total": 3, + "orderBy": "#pub_date# DESC`" }` - &fieldDelimiter=`#` - &filter=`#published# = 1` - &total=`3` - &orderBy=`#pub_date# DESC` &outputterParams=`{ "itemTpl": "documents_item", "wrapperTpl": "@CODE:[+ddGetDocuments_items+][+extenders.pagination+]", @@ -877,11 +887,11 @@ Returns: ``` * ``&providerParams=`{"parentIds": "[*id*]"}` `` — fetch current doc children. -* ``&filter=`#published# = 1` `` — only published. +* ``&providerParams=`{"filter": "#published# = 1"}` `` — only published. +* ``&providerParams=`{"total": 3}` `` — items per page. +* ``&providerParams=`{"orderBy": "#pub_date# DESC"} `` — sort by publish date, new first. * ``&outputterParams=`{"itemTpl": "documents_item"}` `` — item template (chunk name). * ``&outputterParams=`{"noResult": "@CODE:"}` `` — return nothing if nothing found. -* ``&total=`3` `` — items per page. -* ``&orderBy=`#pub_date# DESC` `` — sort by publish date, new first. * ``&extendersParams=`{"pagination": {}}` `` — pagination templates (see the parameters description). * ``&wrapperTpl=`@CODE:[+ddGetDocuments_items+][+extenders.pagination+]` `` — we need set where pagination will being outputted. @@ -894,16 +904,12 @@ Set up filter to get only necessary documets. ``` [[ddGetDocuments? + &fieldDelimiter=`#` &providerParams=`{ "parentIds": 1, - "depth": 3 + "depth": 3, + "filter": "#published# = 1 AND #deleted# = 0 AND #template# = 11" }` - &fieldDelimiter=`#` - &filter=` - #published# = 1 AND - #deleted# = 0 AND - #template# = 11 - ` &extenders=`search` &extendersParams=`{ "docFieldsToSearch": "pagetitle,content,someTv" diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index 3b88155..128f812 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -55,9 +55,6 @@ 'objects' => [ (object) [ //General - 'total' => NULL, - 'offset' => 0, - 'filter' => NULL, 'fieldDelimiter' => '', //Data provider @@ -102,9 +99,6 @@ //Passing a link to the storage $extendersStorage[$extenderName] = $extenderObject; - //Overwrite the snippet parameters with the result of applying them to the current extender - $input->snippetParams = $extenderObject->applyToSnippetParams($input->snippetParams); - //Overwrite the data provider parameters with the result of applying them to the current extender $input->providerParams = $extenderObject->applyToDataProviderParams($input->providerParams); } diff --git a/src/DataProvider/DataProvider.php b/src/DataProvider/DataProvider.php index 32a0f43..70d5fa8 100644 --- a/src/DataProvider/DataProvider.php +++ b/src/DataProvider/DataProvider.php @@ -20,8 +20,8 @@ abstract class DataProvider extends \DDTools\BaseClass { 'tvs' => [] ], $total, - $filter, - $offset, + $filter = '', + $offset = 0, $orderBy = '' ; @@ -76,25 +76,11 @@ public final static function includeProviderByName($providerName){ /** * __construct - * @version 1.3.2 (2020-10-01) + * @version 1.3.3 (2021-02-12) * * @param $input {\ddGetDocuments\Input} */ function __construct(\ddGetDocuments\Input $input){ - //Params from the snippet first - foreach ( - [ - 'total', - 'filter', - 'offset' - ] - as $paramName - ){ - if(isset($input->snippetParams->{$paramName})){ - $this->{$paramName} = $input->snippetParams->{$paramName}; - } - } - //Все параметры задают свойства объекта $this->setExistingProps($input->providerParams); diff --git a/src/Extender/Extender.php b/src/Extender/Extender.php index bf20b35..318b3cf 100644 --- a/src/Extender/Extender.php +++ b/src/Extender/Extender.php @@ -17,18 +17,6 @@ function __construct($params = []){ $this->setExistingProps($params); } - /** - * applyToSnippetParams - * @version 2.1 (2020-10-02) - * - * @param $snippetParams {stdClass} - * - * @return {stdClass} - */ - public function applyToSnippetParams($snippetParams){ - return $snippetParams; - } - /** * applyToDataProviderParams * @version 1.0 (2020-10-01) diff --git a/src/Extender/Pagination/Extender.php b/src/Extender/Pagination/Extender.php index 396e97c..9cb28a5 100644 --- a/src/Extender/Pagination/Extender.php +++ b/src/Extender/Pagination/Extender.php @@ -5,9 +5,9 @@ class Extender extends \ddGetDocuments\Extender\Extender { private /** - * @property $snippetParams {stdClass} + * @property $dataProviderParams {stdClass} */ - $snippetParams, + $dataProviderParams, //Current page index $pageIndex, //The parameter in $_REQUEST to get the current page index from @@ -74,33 +74,33 @@ public function __construct($params = []){ } /** - * applyToSnippetParams - * @version 2.0.1 (2020-10-02) + * applyToDataProviderParams + * @version 1.0 (2020-10-02) * - * @param $snippetParams {stdClass} + * @param $dataProviderParams {stdClass} * * @return {stdClass} */ - public function applyToSnippetParams($snippetParams){ + public function applyToDataProviderParams($dataProviderParams){ //If “total” is set then we need to override “offset” according to the current page index - if(isset($snippetParams->total)){ - $snippetParams->offset = + if(isset($dataProviderParams->total)){ + $dataProviderParams->offset = ( $this->pageIndex - 1 ) * - $snippetParams->total + $dataProviderParams->total ; } - $this->snippetParams = $snippetParams; + $this->dataProviderParams = $dataProviderParams; - return $snippetParams; + return $dataProviderParams; } /** * applyToOutput - * @version 1.2.2 (2020-10-02) + * @version 1.2.3 (2021-02-12) * * @param $dataProviderOutput {\ddGetDocuments\DataProvider\DataProviderOutput} * @@ -110,10 +110,10 @@ public function applyToOutput(\ddGetDocuments\DataProvider\DataProviderOutput $d $result = ''; //Check to prevent division by zero - if($this->snippetParams->total != 0){ + if($this->dataProviderParams->total != 0){ $pagesTotal = ceil( $dataProviderOutput->totalFound / - $this->snippetParams->total + $this->dataProviderParams->total ); if($pagesTotal > 1){ diff --git a/src/Extender/Search/Extender.php b/src/Extender/Search/Extender.php index 01ce5d8..2014506 100644 --- a/src/Extender/Search/Extender.php +++ b/src/Extender/Search/Extender.php @@ -38,14 +38,14 @@ public function __construct($params = []){ } /** - * applyToSnippetParams - * @version 2.0.1 (2021-01-04) + * applyToDataProviderParams + * @version 1.0 (2021-02-12) * - * @param $snippetParams {stdClass} + * @param $dataProviderParams {stdClass} * * @return {stdClass} */ - public function applyToSnippetParams($snippetParams){ + public function applyToDataProviderParams($dataProviderParams){ //If URL contains tags if (!empty($this->currentQuery)){ $searchQueries = []; @@ -64,15 +64,15 @@ public function applyToSnippetParams($snippetParams){ } if( - isset($snippetParams->filter) && - trim($snippetParams->filter) != '' + isset($dataProviderParams->filter) && + trim($dataProviderParams->filter) != '' ){ - $snippetParams->filter .= ' AND'; + $dataProviderParams->filter .= ' AND'; }else{ - $snippetParams->filter = ''; + $dataProviderParams->filter = ''; } - $snippetParams->filter .= + $dataProviderParams->filter .= ' (' . implode( ' OR ', @@ -82,6 +82,6 @@ public function applyToSnippetParams($snippetParams){ ; } - return $snippetParams; + return $dataProviderParams; } } \ No newline at end of file diff --git a/src/Extender/Tagging/Extender.php b/src/Extender/Tagging/Extender.php index adb0a32..ca2357a 100644 --- a/src/Extender/Tagging/Extender.php +++ b/src/Extender/Tagging/Extender.php @@ -54,23 +54,23 @@ public function __construct($params = []){ } /** - * applyToSnippetParams - * @version 2.0 (2020-03-11) + * applyToDataProviderParams + * @version 1.0 (2021-02-12) * - * @param $snippetParams {stdClass} + * @param $dataProviderParams {stdClass} * * @return {stdClass} */ - public function applyToSnippetParams($snippetParams){ + public function applyToDataProviderParams($dataProviderParams){ //If URL contains tags if (!empty($this->currentTags)){ if( - isset($snippetParams->filter) && - trim($snippetParams->filter) != '' + isset($dataProviderParams->filter) && + trim($dataProviderParams->filter) != '' ){ - $snippetParams->filter .= ' AND'; + $dataProviderParams->filter .= ' AND'; }else{ - $snippetParams->filter = ''; + $dataProviderParams->filter = ''; } $tagQueries = []; @@ -92,7 +92,7 @@ public function applyToSnippetParams($snippetParams){ ; } - $snippetParams->filter .= + $dataProviderParams->filter .= ' (' . implode( ' OR ', @@ -102,7 +102,7 @@ public function applyToSnippetParams($snippetParams){ ; } - return $snippetParams; + return $dataProviderParams; } /** diff --git a/src/Input.php b/src/Input.php index afd5c0b..0e49b13 100644 --- a/src/Input.php +++ b/src/Input.php @@ -6,9 +6,6 @@ class Input extends \DDTools\BaseClass { public /** * @property $snippetParams {stdClass} - * @property $snippetParams->offset {integer} - * @property $snippetParams->total {integer} - * @property $snippetParams->filter {string} * @property $snippetParams->fieldDelimiter {string} */ $snippetParams, @@ -19,7 +16,12 @@ class Input extends \DDTools\BaseClass { * @property $providerParams->{$paramName} {mixed} */ $provider = 'parent', - $providerParams, + $providerParams = [ + 'filter' => '', + 'offset' => 0, + 'total' => NULL, + 'orderBy' => '' + ], /** * @property $outputter {string} @@ -27,19 +29,19 @@ class Input extends \DDTools\BaseClass { * @property $outputterParams->{$paramName} {mixed} */ $outputter = 'string', - $outputterParams, + $outputterParams = [], /** * @property $extendersParams {stdClass} * @property $extendersParams->{$extenderName} {stdClass} * @property $extendersParams->{$extenderName}->{$paramName} {mixed} */ - $extendersParams + $extendersParams = [] ; /** * __construct - * @version 4.0 (2021-02-12) + * @version 4.1 (2021-02-14) * * @param $snippetParams {stdClass} — The object of parameters. @required * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} — @required @@ -60,9 +62,16 @@ public function __construct($snippetParams){ ] as $paramName ){ - $this->{$paramName} = \DDTools\ObjectTools::convertType([ - 'object' => $this->snippetParams->{$paramName}, - 'type' => 'objectStdClass' + $this->{$paramName} = \DDTools\ObjectTools::extend([ + 'objects' => [ + //Defaults + (object) $this->{$paramName}, + //Given parameters + \DDTools\ObjectTools::convertType([ + 'object' => $this->snippetParams->{$paramName}, + 'type' => 'objectStdClass' + ]) + ] ]); //No needed in snippet params @@ -93,10 +102,10 @@ public function __construct($snippetParams){ $this->providerParams->orderBy ); } - $this->snippetParams->filter = str_replace( + $this->providerParams->filter = str_replace( $this->snippetParams->fieldDelimiter, '`', - $this->snippetParams->filter + $this->providerParams->filter ); } @@ -160,23 +169,33 @@ private function prepareExtendersParams(){ /** * paramsBackwardCompatibility - * @version 1.0 (2021-02-12) + * @version 1.1 (2021-02-12) * * @desc Prepare snippet params preserve backward compatibility. * * @return {void} */ private function paramsBackwardCompatibility(){ - //Backward compatibility with <= 1.1 - if ( - \DDTools\ObjectTools::isPropExists([ - 'object' => $this->snippetParams, - 'propName' => 'orderBy' - ]) + //Move parameters from snippetParams to providerParams + foreach ( + [ + 'filter', + 'offset', + 'total', + 'orderBy' + ] as + $paramName ){ - $this->providerParams->orderBy = $this->snippetParams->orderBy; - - unset($this->snippetParams->orderBy); + if ( + \DDTools\ObjectTools::isPropExists([ + 'object' => $this->snippetParams, + 'propName' => $paramName + ]) + ){ + $this->providerParams->{$paramName} = $this->snippetParams->{$paramName}; + + unset($this->snippetParams->{$paramName}); + } } } } \ No newline at end of file From 1a3369f3ce75ce647586a41e7faf839b50873eb3 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 15 Feb 2021 12:14:37 +0300 Subject: [PATCH 24/42] * `\ddGetDocuments\DataProvider\DataProvider::__construct`: Parameters type changed to stdClass|array from `\ddGetDocuments\Input`. --- ddGetDocuments_snippet.php | 2 +- src/DataProvider/DataProvider.php | 8 ++++---- src/DataProvider/Parent/DataProvider.php | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index 128f812..ad5f08c 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -103,7 +103,7 @@ $input->providerParams = $extenderObject->applyToDataProviderParams($input->providerParams); } - $dataProviderObject = new $dataProviderClass($input); + $dataProviderObject = new $dataProviderClass($input->providerParams); if ($input->outputter != 'raw'){ $input->outputterParams->dataProvider = $dataProviderObject; diff --git a/src/DataProvider/DataProvider.php b/src/DataProvider/DataProvider.php index 70d5fa8..0ce70f9 100644 --- a/src/DataProvider/DataProvider.php +++ b/src/DataProvider/DataProvider.php @@ -76,13 +76,13 @@ public final static function includeProviderByName($providerName){ /** * __construct - * @version 1.3.3 (2021-02-12) + * @version 2.0 (2021-02-15) * - * @param $input {\ddGetDocuments\Input} + * @param $params {stdClass|arrayAssociative} */ - function __construct(\ddGetDocuments\Input $input){ + function __construct($params){ //Все параметры задают свойства объекта - $this->setExistingProps($input->providerParams); + $this->setExistingProps($params); //Init source DB table name $this->resourcesTableName = diff --git a/src/DataProvider/Parent/DataProvider.php b/src/DataProvider/Parent/DataProvider.php index d085bfc..318809e 100644 --- a/src/DataProvider/Parent/DataProvider.php +++ b/src/DataProvider/Parent/DataProvider.php @@ -13,13 +13,13 @@ class DataProvider extends \ddGetDocuments\DataProvider\DataProvider { /** * __construct - * @version 1.1.3 (2020-03-11) + * @version 2.0 (2021-02-15) * - * @param $input {\ddGetDocuments\Input} + * @param $params {stdClass|arrayAssociative} */ - public function __construct(\ddGetDocuments\Input $input){ + public function __construct($params){ //Call base constructor - parent::__construct($input); + parent::__construct($params); //Comma separated strings support if (!is_array($this->parentIds)){ From 83fb10475c84adc0c652c39983559241106ab435 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 15 Feb 2021 12:41:29 +0300 Subject: [PATCH 25/42] * `\ddGetDocuments\Input`: - `snippetParams`. + `fieldDelimiter`. --- ddGetDocuments_snippet.php | 11 ++---- src/Input.php | 73 ++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 43 deletions(-) diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index ad5f08c..d9d81a0 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -54,20 +54,15 @@ $params = \DDTools\ObjectTools::extend([ 'objects' => [ (object) [ - //General - 'fieldDelimiter' => '', - //Data provider - 'provider' => 'parent', - 'providerParams' => '', + 'providerParams' => [], //Outputter - 'outputter' => 'string', - 'outputterParams' => '', + 'outputterParams' => [], //Extenders 'extenders' => [], - 'extendersParams' => '' + 'extendersParams' => [] ], $params ] diff --git a/src/Input.php b/src/Input.php index 0e49b13..311ebe8 100644 --- a/src/Input.php +++ b/src/Input.php @@ -5,10 +5,9 @@ class Input extends \DDTools\BaseClass { public /** - * @property $snippetParams {stdClass} - * @property $snippetParams->fieldDelimiter {string} + * @property $fieldDelimiter {string} */ - $snippetParams, + $fieldDelimiter = '', /** * @property $provider {string} @@ -41,7 +40,7 @@ class Input extends \DDTools\BaseClass { /** * __construct - * @version 4.1 (2021-02-14) + * @version 4.1.1 (2021-02-14) * * @param $snippetParams {stdClass} — The object of parameters. @required * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} — @required @@ -49,10 +48,6 @@ class Input extends \DDTools\BaseClass { * @param $snippetParams->outputterParams {stdClass|arrayAssociative|stringJsonObject} — @required */ public function __construct($snippetParams){ - //Save snippet parameters and prapare them later - $this->snippetParams = $snippetParams; - - //Prepare provider, outputter and extender params foreach ( [ @@ -68,26 +63,26 @@ public function __construct($snippetParams){ (object) $this->{$paramName}, //Given parameters \DDTools\ObjectTools::convertType([ - 'object' => $this->snippetParams->{$paramName}, + 'object' => $snippetParams->{$paramName}, 'type' => 'objectStdClass' ]) ] ]); //No needed in snippet params - unset($this->snippetParams->{$paramName}); + unset($snippetParams->{$paramName}); } //Backward compatibility - $this->paramsBackwardCompatibility(); + $snippetParams = $this->paramsBackwardCompatibility($snippetParams); //Set object properties from snippet parameters - $this->setExistingProps($this->snippetParams); + $this->setExistingProps($snippetParams); - $this->prepareExtendersParams(); + $snippetParams = $this->prepareExtendersParams($snippetParams); //TODO: Is it needed? @@ -97,13 +92,13 @@ public function __construct($snippetParams){ //Make sure orderBy and filter looks like SQL if (!empty($this->providerParams->orderBy)){ $this->providerParams->orderBy = str_replace( - $this->snippetParams->fieldDelimiter, + $this->fieldDelimiter, '`', $this->providerParams->orderBy ); } $this->providerParams->filter = str_replace( - $this->snippetParams->fieldDelimiter, + $this->fieldDelimiter, '`', $this->providerParams->filter ); @@ -111,44 +106,46 @@ public function __construct($snippetParams){ /** * prepareExtendersParams - * @version 1.0 (2021-02-12) + * @version 2.0 (2021-02-15) * * @desc Prepare extenders params. * - * @return {void} + * @param $snippetParams {stdClass} + * + * @return {stdClass} */ - private function prepareExtendersParams(){ + private function prepareExtendersParams($snippetParams){ //Prepare extenders - if (is_string($this->snippetParams->extenders)){ - if (!empty($this->snippetParams->extenders)){ - $this->snippetParams->extenders = explode( + if (is_string($snippetParams->extenders)){ + if (!empty($snippetParams->extenders)){ + $snippetParams->extenders = explode( ',', - trim($this->snippetParams->extenders) + trim($snippetParams->extenders) ); }else{ - $this->snippetParams->extenders = []; + $snippetParams->extenders = []; } } //Prepare extenders params - if(!empty($this->snippetParams->extenders)){ + if(!empty($snippetParams->extenders)){ //If we have a single extender then make sure that extender params set as an array //like [extenderName => [extenderParameter_1, extenderParameter_2, ...]] - if(count($this->snippetParams->extenders) === 1){ + if(count($snippetParams->extenders) === 1){ if( !\DDTools\ObjectTools::isPropExists([ 'object' => $this->extendersParams, - 'propName' => $this->snippetParams->extenders[0] + 'propName' => $snippetParams->extenders[0] ]) ){ $this->extendersParams = (object) [ - $this->snippetParams->extenders[0] => $this->extendersParams + $snippetParams->extenders[0] => $this->extendersParams ]; } }else{ //Make sure that for each extender there is an item in $this->extendersParams foreach( - $this->snippetParams->extenders as + $snippetParams->extenders as $extenderName ){ if( @@ -164,18 +161,22 @@ private function prepareExtendersParams(){ } //No needed anymore, all data was saved to $this->extendersParams - unset($this->snippetParams->extenders); + unset($snippetParams->extenders); + + return $snippetParams; } /** * paramsBackwardCompatibility - * @version 1.1 (2021-02-12) + * @version 2.0 (2021-02-15) * * @desc Prepare snippet params preserve backward compatibility. * - * @return {void} + * @param $snippetParams {stdClass} + * + * @return {stdClass} */ - private function paramsBackwardCompatibility(){ + private function paramsBackwardCompatibility($snippetParams){ //Move parameters from snippetParams to providerParams foreach ( [ @@ -188,14 +189,16 @@ private function paramsBackwardCompatibility(){ ){ if ( \DDTools\ObjectTools::isPropExists([ - 'object' => $this->snippetParams, + 'object' => $snippetParams, 'propName' => $paramName ]) ){ - $this->providerParams->{$paramName} = $this->snippetParams->{$paramName}; + $this->providerParams->{$paramName} = $snippetParams->{$paramName}; - unset($this->snippetParams->{$paramName}); + unset($snippetParams->{$paramName}); } } + + return $snippetParams; } } \ No newline at end of file From 425c3ced20cc87e7b199f51d2ff31231c7a9c0dd Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 15 Feb 2021 12:43:52 +0300 Subject: [PATCH 26/42] * `\ddGetDocuments\Input::__construct`: Refactoring. --- src/Input.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Input.php b/src/Input.php index 311ebe8..a740c0c 100644 --- a/src/Input.php +++ b/src/Input.php @@ -40,7 +40,7 @@ class Input extends \DDTools\BaseClass { /** * __construct - * @version 4.1.1 (2021-02-14) + * @version 4.1.2 (2021-02-14) * * @param $snippetParams {stdClass} — The object of parameters. @required * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} — @required @@ -78,11 +78,11 @@ public function __construct($snippetParams){ $snippetParams = $this->paramsBackwardCompatibility($snippetParams); - //Set object properties from snippet parameters - $this->setExistingProps($snippetParams); + $snippetParams = $this->prepareExtendersParams($snippetParams); - $snippetParams = $this->prepareExtendersParams($snippetParams); + //Set object properties from snippet parameters + $this->setExistingProps($snippetParams); //TODO: Is it needed? From 2521d374de06b76a769a84126b05ef2afbb2e16e Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 15 Feb 2021 12:45:55 +0300 Subject: [PATCH 27/42] * `\ddGetDocuments\Input::backwardCompatibility_dataProviderParams`: Renamed from `paramsBackwardCompatibility`. --- src/Input.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Input.php b/src/Input.php index a740c0c..28da3fa 100644 --- a/src/Input.php +++ b/src/Input.php @@ -40,7 +40,7 @@ class Input extends \DDTools\BaseClass { /** * __construct - * @version 4.1.2 (2021-02-14) + * @version 4.1.3 (2021-02-15) * * @param $snippetParams {stdClass} — The object of parameters. @required * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} — @required @@ -75,7 +75,7 @@ public function __construct($snippetParams){ //Backward compatibility - $snippetParams = $this->paramsBackwardCompatibility($snippetParams); + $snippetParams = $this->backwardCompatibility_dataProviderParams($snippetParams); $snippetParams = $this->prepareExtendersParams($snippetParams); @@ -167,8 +167,8 @@ private function prepareExtendersParams($snippetParams){ } /** - * paramsBackwardCompatibility - * @version 2.0 (2021-02-15) + * backwardCompatibility_dataProviderParams + * @version 1.0 (2021-02-15) * * @desc Prepare snippet params preserve backward compatibility. * @@ -176,7 +176,7 @@ private function prepareExtendersParams($snippetParams){ * * @return {stdClass} */ - private function paramsBackwardCompatibility($snippetParams){ + private function backwardCompatibility_dataProviderParams($snippetParams){ //Move parameters from snippetParams to providerParams foreach ( [ From f10d4a9eebc5681a5d1fcfe63005fa86f5364a72 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 15 Feb 2021 12:58:07 +0300 Subject: [PATCH 28/42] * `\ddGetDocuments\Input::__construct`: `$snippetParams->providerParams`, `$snippetParams->outputterParams` and `$snippetParams->extendersParams` are not required anymore. * `\ddGetDocuments\Input::prepareExtendersParams`: `$snippetParams->extenders` is not required anymore. --- ddGetDocuments_snippet.php | 18 ------ src/Input.php | 123 +++++++++++++++++++++---------------- 2 files changed, 70 insertions(+), 71 deletions(-) diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index d9d81a0..e85e98f 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -50,24 +50,6 @@ 'returnCorrectedOnly' => false ]); -//Defaults -$params = \DDTools\ObjectTools::extend([ - 'objects' => [ - (object) [ - //Data provider - 'providerParams' => [], - - //Outputter - 'outputterParams' => [], - - //Extenders - 'extenders' => [], - 'extendersParams' => [] - ], - $params - ] -]); - $input = new \ddGetDocuments\Input($params); $dataProviderClass = \ddGetDocuments\DataProvider\DataProvider::includeProviderByName($input->provider); diff --git a/src/Input.php b/src/Input.php index 28da3fa..1e1be5c 100644 --- a/src/Input.php +++ b/src/Input.php @@ -40,12 +40,12 @@ class Input extends \DDTools\BaseClass { /** * __construct - * @version 4.1.3 (2021-02-15) + * @version 4.2 (2021-02-15) * * @param $snippetParams {stdClass} — The object of parameters. @required - * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} — @required - * @param $snippetParams->extendersParams {stdClass|arrayAssociative|stringJsonObject} — @required - * @param $snippetParams->outputterParams {stdClass|arrayAssociative|stringJsonObject} — @required + * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} + * @param $snippetParams->extendersParams {stdClass|arrayAssociative|stringJsonObject} + * @param $snippetParams->outputterParams {stdClass|arrayAssociative|stringJsonObject} */ public function __construct($snippetParams){ //Prepare provider, outputter and extender params @@ -57,17 +57,27 @@ public function __construct($snippetParams){ ] as $paramName ){ - $this->{$paramName} = \DDTools\ObjectTools::extend([ - 'objects' => [ - //Defaults - (object) $this->{$paramName}, - //Given parameters - \DDTools\ObjectTools::convertType([ - 'object' => $snippetParams->{$paramName}, - 'type' => 'objectStdClass' - ]) - ] - ]); + //Convert to object + $this->{$paramName} = (object) $this->{$paramName}; + + if ( + \DDTools\ObjectTools::isPropExists([ + 'object' => $snippetParams, + 'propName' => $paramName + ]) + ){ + $this->{$paramName} = \DDTools\ObjectTools::extend([ + 'objects' => [ + //Defaults + $this->{$paramName}, + //Given parameters + \DDTools\ObjectTools::convertType([ + 'object' => $snippetParams->{$paramName}, + 'type' => 'objectStdClass' + ]) + ] + ]); + } //No needed in snippet params unset($snippetParams->{$paramName}); @@ -106,7 +116,7 @@ public function __construct($snippetParams){ /** * prepareExtendersParams - * @version 2.0 (2021-02-15) + * @version 2.1 (2021-02-15) * * @desc Prepare extenders params. * @@ -115,54 +125,61 @@ public function __construct($snippetParams){ * @return {stdClass} */ private function prepareExtendersParams($snippetParams){ - //Prepare extenders - if (is_string($snippetParams->extenders)){ - if (!empty($snippetParams->extenders)){ - $snippetParams->extenders = explode( - ',', - trim($snippetParams->extenders) - ); - }else{ - $snippetParams->extenders = []; - } - } - - //Prepare extenders params - if(!empty($snippetParams->extenders)){ - //If we have a single extender then make sure that extender params set as an array - //like [extenderName => [extenderParameter_1, extenderParameter_2, ...]] - if(count($snippetParams->extenders) === 1){ - if( - !\DDTools\ObjectTools::isPropExists([ - 'object' => $this->extendersParams, - 'propName' => $snippetParams->extenders[0] - ]) - ){ - $this->extendersParams = (object) [ - $snippetParams->extenders[0] => $this->extendersParams - ]; + if ( + \DDTools\ObjectTools::isPropExists([ + 'object' => $snippetParams, + 'propName' => 'extenders' + ]) + ){ + //Prepare extenders + if (is_string($snippetParams->extenders)){ + if (!empty($snippetParams->extenders)){ + $snippetParams->extenders = explode( + ',', + trim($snippetParams->extenders) + ); + }else{ + $snippetParams->extenders = []; } - }else{ - //Make sure that for each extender there is an item in $this->extendersParams - foreach( - $snippetParams->extenders as - $extenderName - ){ + } + + //Prepare extenders params + if(!empty($snippetParams->extenders)){ + //If we have a single extender then make sure that extender params set as an array + //like [extenderName => [extenderParameter_1, extenderParameter_2, ...]] + if(count($snippetParams->extenders) === 1){ if( !\DDTools\ObjectTools::isPropExists([ 'object' => $this->extendersParams, - 'propName' => $extenderName + 'propName' => $snippetParams->extenders[0] ]) ){ - $this->extendersParams->{$extenderName} = (object) []; + $this->extendersParams = (object) [ + $snippetParams->extenders[0] => $this->extendersParams + ]; + } + }else{ + //Make sure that for each extender there is an item in $this->extendersParams + foreach( + $snippetParams->extenders as + $extenderName + ){ + if( + !\DDTools\ObjectTools::isPropExists([ + 'object' => $this->extendersParams, + 'propName' => $extenderName + ]) + ){ + $this->extendersParams->{$extenderName} = (object) []; + } } } } + + //No needed anymore, all data was saved to $this->extendersParams + unset($snippetParams->extenders); } - //No needed anymore, all data was saved to $this->extendersParams - unset($snippetParams->extenders); - return $snippetParams; } From 09d79727e1f6df23a825d6a169d52355e698280e Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 15 Feb 2021 13:03:27 +0300 Subject: [PATCH 29/42] + `\ddGetDocuments\Input::backwardCompatibility_verifyRenamedParams`: Moved here from Snippet. --- ddGetDocuments_snippet.php | 10 ---------- src/Input.php | 29 +++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index e85e98f..e67b909 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -40,16 +40,6 @@ ); } -//Backward compatibility -$params = \ddTools::verifyRenamedParams([ - 'params' => $params, - 'compliance' => [ - 'outputter' => 'outputFormat', - 'outputterParams' => 'outputFormatParams' - ], - 'returnCorrectedOnly' => false -]); - $input = new \ddGetDocuments\Input($params); $dataProviderClass = \ddGetDocuments\DataProvider\DataProvider::includeProviderByName($input->provider); diff --git a/src/Input.php b/src/Input.php index 1e1be5c..b8c08db 100644 --- a/src/Input.php +++ b/src/Input.php @@ -40,7 +40,7 @@ class Input extends \DDTools\BaseClass { /** * __construct - * @version 4.2 (2021-02-15) + * @version 4.3 (2021-02-15) * * @param $snippetParams {stdClass} — The object of parameters. @required * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} @@ -48,6 +48,8 @@ class Input extends \DDTools\BaseClass { * @param $snippetParams->outputterParams {stdClass|arrayAssociative|stringJsonObject} */ public function __construct($snippetParams){ + $snippetParams = $this->backwardCompatibility_verifyRenamedParams($snippetParams); + //Prepare provider, outputter and extender params foreach ( [ @@ -183,11 +185,34 @@ private function prepareExtendersParams($snippetParams){ return $snippetParams; } + /** + * backwardCompatibility_verifyRenamedParams + * @version 1.0 (2021-02-15) + * + * @desc Verify renamed snippet parameters. + * + * @param $snippetParams {stdClass} + * + * @return {stdClass} + */ + private function backwardCompatibility_verifyRenamedParams($snippetParams){ + $snippetParams = \ddTools::verifyRenamedParams([ + 'params' => $snippetParams, + 'compliance' => [ + 'outputter' => 'outputFormat', + 'outputterParams' => 'outputFormatParams' + ], + 'returnCorrectedOnly' => false + ]); + + return $snippetParams; + } + /** * backwardCompatibility_dataProviderParams * @version 1.0 (2021-02-15) * - * @desc Prepare snippet params preserve backward compatibility. + * @desc Prepare data provider params preserve backward compatibility. * * @param $snippetParams {stdClass} * From b455ca626f3bcc5783d2fc73c41898abafba17d7 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 15 Feb 2021 20:18:19 +0300 Subject: [PATCH 30/42] - `\ddGetDocuments\DataProvider\DataProvider::includeProviderByName`: The method was removed. `\DDTools\BaseClass::createChildInstance` is used instead. --- ddGetDocuments_snippet.php | 16 +++++++----- src/DataProvider/DataProvider.php | 41 ------------------------------- 2 files changed, 10 insertions(+), 47 deletions(-) diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index e67b909..e6b5184 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -33,7 +33,7 @@ ); } -if(!class_exists('\ddGetDocuments\DataProvider\DataProvider')){ +if(!class_exists('\ddGetDocuments\Input')){ require_once( $snippetPath . 'require.php' @@ -42,9 +42,6 @@ $input = new \ddGetDocuments\Input($params); -$dataProviderClass = \ddGetDocuments\DataProvider\DataProvider::includeProviderByName($input->provider); - -if(class_exists($dataProviderClass)){ //Extenders storage $extendersStorage = []; @@ -70,7 +67,15 @@ $input->providerParams = $extenderObject->applyToDataProviderParams($input->providerParams); } - $dataProviderObject = new $dataProviderClass($input->providerParams); + $dataProviderObject = \ddGetDocuments\DataProvider\DataProvider::createChildInstance([ + 'name' => $input->provider, + 'parentDir' => + $snippetPath_src . + 'DataProvider' + , + //Passing parameters into constructor + 'params' => $input->providerParams + ]); if ($input->outputter != 'raw'){ $input->outputterParams->dataProvider = $dataProviderObject; @@ -104,7 +109,6 @@ }else{ $snippetResult = $outputterObject->parse($outputData); } -} return $snippetResult; ?> \ No newline at end of file diff --git a/src/DataProvider/DataProvider.php b/src/DataProvider/DataProvider.php index 0ce70f9..5bc7f44 100644 --- a/src/DataProvider/DataProvider.php +++ b/src/DataProvider/DataProvider.php @@ -33,47 +33,6 @@ abstract class DataProvider extends \DDTools\BaseClass { coalesce(`tvValue`.`value`, `tvName`.`default_text`) )'; - /** - * includeProviderByName - * @version 1.0.5 (2020-03-10) - * - * @TODO: Remove it, use `\DDTools\BaseClass::createChildInstance` instead - * - * @param $providerName - * @return string - * @throws \Exception - */ - public final static function includeProviderByName($providerName){ - $providerName = ucfirst(strtolower($providerName)); - $providerPath = - $providerName . - DIRECTORY_SEPARATOR . - 'DataProvider' . - '.php' - ; - - if(is_file(__DIR__.DIRECTORY_SEPARATOR.$providerPath)){ - require_once($providerPath); - - return - __NAMESPACE__ . - '\\' . - $providerName . - '\\' . - 'DataProvider' - ; - }else{ - throw new \Exception( - ( - 'Data provider ' . - $providerName . - ' not found.' - ), - 500 - ); - } - } - /** * __construct * @version 2.0 (2021-02-15) From ad240715ebf55f1d7be4ff8bc66f8d534b95a148 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 15 Feb 2021 20:21:07 +0300 Subject: [PATCH 31/42] * Including of (MODX)Evolution.libraries.ddTools was moved to `require.php`. --- ddGetDocuments_snippet.php | 8 -------- require.php | 10 ++++++++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index e6b5184..1b04baa 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -25,14 +25,6 @@ DIRECTORY_SEPARATOR ; -//Include (MODX)EvolutionCMS.libraries.ddTools -if(!class_exists('\ddTools')){ - require_once( - $modx->getConfig('base_path') . - 'assets/libs/ddTools/modx.ddtools.class.php' - ); -} - if(!class_exists('\ddGetDocuments\Input')){ require_once( $snippetPath . diff --git a/require.php b/require.php index 401b2c6..314e530 100644 --- a/require.php +++ b/require.php @@ -1,4 +1,14 @@ Date: Mon, 15 Feb 2021 20:21:25 +0300 Subject: [PATCH 32/42] * Minor changes. --- ddGetDocuments_snippet.php | 120 ++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index 1b04baa..a7d2ff1 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -34,73 +34,73 @@ $input = new \ddGetDocuments\Input($params); - //Extenders storage - $extendersStorage = []; - - //Iterate through all extenders to create their instances - foreach( - $input->extendersParams as - $extenderName => - $extenderParams - ){ - $extenderObject = \ddGetDocuments\Extender\Extender::createChildInstance([ - 'name' => $extenderName, - 'parentDir' => - $snippetPath_src . - 'Extender' - , - //Passing parameters into constructor - 'params' => $extenderParams - ]); - //Passing a link to the storage - $extendersStorage[$extenderName] = $extenderObject; - - //Overwrite the data provider parameters with the result of applying them to the current extender - $input->providerParams = $extenderObject->applyToDataProviderParams($input->providerParams); - } - - $dataProviderObject = \ddGetDocuments\DataProvider\DataProvider::createChildInstance([ - 'name' => $input->provider, +//Extenders storage +$extendersStorage = []; + +//Iterate through all extenders to create their instances +foreach( + $input->extendersParams as + $extenderName => + $extenderParams +){ + $extenderObject = \ddGetDocuments\Extender\Extender::createChildInstance([ + 'name' => $extenderName, 'parentDir' => $snippetPath_src . - 'DataProvider' + 'Extender' , //Passing parameters into constructor - 'params' => $input->providerParams + 'params' => $extenderParams ]); + //Passing a link to the storage + $extendersStorage[$extenderName] = $extenderObject; - if ($input->outputter != 'raw'){ - $input->outputterParams->dataProvider = $dataProviderObject; - - $outputterObject = \ddGetDocuments\Outputter\Outputter::createChildInstance([ - 'name' => $input->outputter, - 'parentDir' => - $snippetPath_src . - 'Outputter' - , - //Passing parameters into constructor - 'params' => $input->outputterParams - ]); - } - - $dataProviderResult = $dataProviderObject->get(); - - $outputData = new \ddGetDocuments\Output($dataProviderResult); - - //Iterate through all extenders again to apply them to the output - foreach( - $extendersStorage as - $extenderName => - $extenderObject - ){ - $outputData->extenders[$extenderName] = $extenderObject->applyToOutput($dataProviderResult); - } + //Overwrite the data provider parameters with the result of applying them to the current extender + $input->providerParams = $extenderObject->applyToDataProviderParams($input->providerParams); +} + +$dataProviderObject = \ddGetDocuments\DataProvider\DataProvider::createChildInstance([ + 'name' => $input->provider, + 'parentDir' => + $snippetPath_src . + 'DataProvider' + , + //Passing parameters into constructor + 'params' => $input->providerParams +]); + +if ($input->outputter != 'raw'){ + $input->outputterParams->dataProvider = $dataProviderObject; - if ($input->outputter == 'raw'){ - $snippetResult = $outputData; - }else{ - $snippetResult = $outputterObject->parse($outputData); - } + $outputterObject = \ddGetDocuments\Outputter\Outputter::createChildInstance([ + 'name' => $input->outputter, + 'parentDir' => + $snippetPath_src . + 'Outputter' + , + //Passing parameters into constructor + 'params' => $input->outputterParams + ]); +} + +$dataProviderResult = $dataProviderObject->get(); + +$outputData = new \ddGetDocuments\Output($dataProviderResult); + +//Iterate through all extenders again to apply them to the output +foreach( + $extendersStorage as + $extenderName => + $extenderObject +){ + $outputData->extenders[$extenderName] = $extenderObject->applyToOutput($dataProviderResult); +} + +if ($input->outputter == 'raw'){ + $snippetResult = $outputData; +}else{ + $snippetResult = $outputterObject->parse($outputData); +} return $snippetResult; ?> \ No newline at end of file From ae9769f9fcb44ac6131aeb626a85a011198b8236 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Mon, 22 Feb 2021 18:21:45 +0300 Subject: [PATCH 33/42] + `\ddGetDocuments\Snippet`: The new class. All snippet code moved here. * Snippet: Just calls `\DDTools\Snippet::runSnippet` to run the snippet without DB and eval. * Attention! (MODX)Evolution.libraries.ddTools >= 0.42 is required. --- README.md | 2 +- composer.json | 2 +- ddGetDocuments_snippet.php | 97 +++----------------------------------- require.php | 2 + src/Snippet.php | 88 ++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 92 deletions(-) create mode 100644 src/Snippet.php diff --git a/README.md b/README.md index a5f5751..dd0a6b9 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A snippet for fetching and parsing resources from the document tree or custom DB * PHP >= 5.4 * MySQL >= 8 or MariaDB >= 10.3.10 (not tested in older versions). * [(MODX)EvolutionCMS](https://github.com/evolution-cms/evolution) >= 1.1 -* [(MODX)EvolutionCMS.libraries.ddTools](https://code.divandesign.biz/modx/ddtools) >= 0.40.1 +* [(MODX)EvolutionCMS.libraries.ddTools](https://code.divandesign.biz/modx/ddtools) >= 0.42 ## Documentation diff --git a/composer.json b/composer.json index f2732f6..2761436 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "license": "MIT", "require": { "php": ">=5.4.0", - "dd/evolutioncms-libraries-ddtools": ">=0.40.1", + "dd/evolutioncms-libraries-ddtools": ">=0.42.0", "composer/installers": "~1.0.0" }, "autoload": { diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index a7d2ff1..2f84c08 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -10,97 +10,14 @@ * @copyright 2015–2020 DD Group {@link https://DivanDesign.biz } */ -//The snippet must return an empty string even if result is absent -$snippetResult = ''; - -global $modx; - -$snippetPath = +//Include (MODX)EvolutionCMS.libraries.ddTools +require_once( $modx->getConfig('base_path') . - 'assets/snippets/ddGetDocuments/' -; -$snippetPath_src = - $snippetPath . - 'src' . - DIRECTORY_SEPARATOR -; - -if(!class_exists('\ddGetDocuments\Input')){ - require_once( - $snippetPath . - 'require.php' - ); -} - -$input = new \ddGetDocuments\Input($params); - -//Extenders storage -$extendersStorage = []; - -//Iterate through all extenders to create their instances -foreach( - $input->extendersParams as - $extenderName => - $extenderParams -){ - $extenderObject = \ddGetDocuments\Extender\Extender::createChildInstance([ - 'name' => $extenderName, - 'parentDir' => - $snippetPath_src . - 'Extender' - , - //Passing parameters into constructor - 'params' => $extenderParams - ]); - //Passing a link to the storage - $extendersStorage[$extenderName] = $extenderObject; - - //Overwrite the data provider parameters with the result of applying them to the current extender - $input->providerParams = $extenderObject->applyToDataProviderParams($input->providerParams); -} + 'assets/libs/ddTools/modx.ddtools.class.php' +); -$dataProviderObject = \ddGetDocuments\DataProvider\DataProvider::createChildInstance([ - 'name' => $input->provider, - 'parentDir' => - $snippetPath_src . - 'DataProvider' - , - //Passing parameters into constructor - 'params' => $input->providerParams +return \DDTools\Snippet::runSnippet([ + 'name' => 'ddGetDocuments', + 'params' => $params ]); - -if ($input->outputter != 'raw'){ - $input->outputterParams->dataProvider = $dataProviderObject; - - $outputterObject = \ddGetDocuments\Outputter\Outputter::createChildInstance([ - 'name' => $input->outputter, - 'parentDir' => - $snippetPath_src . - 'Outputter' - , - //Passing parameters into constructor - 'params' => $input->outputterParams - ]); -} - -$dataProviderResult = $dataProviderObject->get(); - -$outputData = new \ddGetDocuments\Output($dataProviderResult); - -//Iterate through all extenders again to apply them to the output -foreach( - $extendersStorage as - $extenderName => - $extenderObject -){ - $outputData->extenders[$extenderName] = $extenderObject->applyToOutput($dataProviderResult); -} - -if ($input->outputter == 'raw'){ - $snippetResult = $outputData; -}else{ - $snippetResult = $outputterObject->parse($outputData); -} - -return $snippetResult; ?> \ No newline at end of file diff --git a/require.php b/require.php index 314e530..d67aacf 100644 --- a/require.php +++ b/require.php @@ -17,4 +17,6 @@ require_once('src/Outputter/String/Outputter.php'); require_once('src/DataProvider/DataProvider.php'); require_once('src/DataProvider/Parent/DataProvider.php'); + +require_once('src/Snippet.php'); ?> \ No newline at end of file diff --git a/src/Snippet.php b/src/Snippet.php new file mode 100644 index 0000000..8f7809d --- /dev/null +++ b/src/Snippet.php @@ -0,0 +1,88 @@ +params); + + //Extenders storage + $extendersStorage = []; + + //Iterate through all extenders to create their instances + foreach( + $input->extendersParams as + $extenderName => + $extenderParams + ){ + $extenderObject = \ddGetDocuments\Extender\Extender::createChildInstance([ + 'name' => $extenderName, + 'parentDir' => + $this->paths->src . + 'Extender' + , + //Passing parameters into constructor + 'params' => $extenderParams + ]); + //Passing a link to the storage + $extendersStorage[$extenderName] = $extenderObject; + + //Overwrite the data provider parameters with the result of applying them to the current extender + $input->providerParams = $extenderObject->applyToDataProviderParams($input->providerParams); + } + + $dataProviderObject = \ddGetDocuments\DataProvider\DataProvider::createChildInstance([ + 'name' => $input->provider, + 'parentDir' => + $this->paths->src . + 'DataProvider' + , + //Passing parameters into constructor + 'params' => $input->providerParams + ]); + + if ($input->outputter != 'raw'){ + $input->outputterParams->dataProvider = $dataProviderObject; + + $outputterObject = \ddGetDocuments\Outputter\Outputter::createChildInstance([ + 'name' => $input->outputter, + 'parentDir' => + $this->paths->src . + 'Outputter' + , + //Passing parameters into constructor + 'params' => $input->outputterParams + ]); + } + + $dataProviderResult = $dataProviderObject->get(); + + $outputData = new \ddGetDocuments\Output($dataProviderResult); + + //Iterate through all extenders again to apply them to the output + foreach( + $extendersStorage as + $extenderName => + $extenderObject + ){ + $outputData->extenders[$extenderName] = $extenderObject->applyToOutput($dataProviderResult); + } + + if ($input->outputter == 'raw'){ + $result = $outputData; + }else{ + $result = $outputterObject->parse($outputData); + } + + return $result; + } +} \ No newline at end of file From 986e1131012d68a0871d0ddef124fd924a287c68 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Wed, 24 Feb 2021 19:50:25 +0300 Subject: [PATCH 34/42] - `\ddGetDocuments\Input::backwardCompatibility_verifyRenamedParams`: The method was removed. `\ddGetDocuments\Snippet::$renamedParamsCompliance` is used instead. --- src/Input.php | 27 +-------------------------- src/Snippet.php | 7 ++++++- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/src/Input.php b/src/Input.php index b8c08db..22392cc 100644 --- a/src/Input.php +++ b/src/Input.php @@ -40,7 +40,7 @@ class Input extends \DDTools\BaseClass { /** * __construct - * @version 4.3 (2021-02-15) + * @version 4.3.1 (2021-02-24) * * @param $snippetParams {stdClass} — The object of parameters. @required * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} @@ -48,8 +48,6 @@ class Input extends \DDTools\BaseClass { * @param $snippetParams->outputterParams {stdClass|arrayAssociative|stringJsonObject} */ public function __construct($snippetParams){ - $snippetParams = $this->backwardCompatibility_verifyRenamedParams($snippetParams); - //Prepare provider, outputter and extender params foreach ( [ @@ -185,29 +183,6 @@ private function prepareExtendersParams($snippetParams){ return $snippetParams; } - /** - * backwardCompatibility_verifyRenamedParams - * @version 1.0 (2021-02-15) - * - * @desc Verify renamed snippet parameters. - * - * @param $snippetParams {stdClass} - * - * @return {stdClass} - */ - private function backwardCompatibility_verifyRenamedParams($snippetParams){ - $snippetParams = \ddTools::verifyRenamedParams([ - 'params' => $snippetParams, - 'compliance' => [ - 'outputter' => 'outputFormat', - 'outputterParams' => 'outputFormatParams' - ], - 'returnCorrectedOnly' => false - ]); - - return $snippetParams; - } - /** * backwardCompatibility_dataProviderParams * @version 1.0 (2021-02-15) diff --git a/src/Snippet.php b/src/Snippet.php index 8f7809d..ec82259 100644 --- a/src/Snippet.php +++ b/src/Snippet.php @@ -3,7 +3,12 @@ class Snippet extends \DDTools\Snippet { protected - $version = '1.2' + $version = '1.2', + + $renamedParamsCompliance = [ + 'outputter' => 'outputFormat', + 'outputterParams' => 'outputFormatParams' + ] ; /** From 90829cb831ce5f780c8fc49fd0ed53d64aaa74d6 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Thu, 25 Feb 2021 12:21:07 +0300 Subject: [PATCH 35/42] * Minor changes. --- src/Outputter/Yandexmarket/Outputter.php | 27 +++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index ea380af..7dc78a7 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -14,12 +14,10 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { //May need for smart name 'pagetitle' ], + /** - * @property $templates {stdClass} - * @property $templates->wrapper {string} - * @property $templates->offers_item {string} - * @property $templates->offers_item_elem {string} - * @property $templates->offers_item_elemParam {string} + * @property $shopData {stdClass} + * @property $shopData->{$name} {string} */ $shopData = [ 'shopName' => '', @@ -28,6 +26,16 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { 'platform' => '(MODX) Evolution CMS', 'version' => '[(settings_version)]' ], + + /** + * @property $offerFields {stdClass} + * @property $offerFields->{$fieldName} {stdClass} + * @property $offerFields->{$fieldName}->docFieldName {string} — Название поля документа, содержащего значение. + * @property $offerFields->{$fieldName}->tagName {string} — Имя тега для вывода. + * @property $offerFields->{$fieldName}->valuePrefix {string} — Префикс, который будет добавлен к значению при выводе. + * @property $offerFields->{$fieldName}->valueSuffix {string} — Суффикс, который будет добавлен к значению при выводе. + * @property $offerFields->{$fieldName}->templateName {string} — Название шаблона, по которому парсить вывод. + */ $offerFields = [ 'name' => [ 'docFieldName' => 'pagetitle', @@ -103,6 +111,14 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { 'templateName' => '' ] ], + + /** + * @property $templates {stdClass} + * @property $templates->wrapper {string} + * @property $templates->offers_item {string} + * @property $templates->offers_item_elem {string} + * @property $templates->offers_item_elemParam {string} + */ $templates = [ 'wrapper' => ' @@ -148,6 +164,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { 'offers_item_elem' => '<[+tagName+][+attrs+]>[+value+]', // 'offers_item_elemAdditionalParams' => '[+value+]', ], + $categoryIds_last = [] ; From 82b7e0891d3603b571d48fbd757bb75a25512014 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Thu, 25 Feb 2021 12:30:54 +0300 Subject: [PATCH 36/42] * `\ddGetDocuments\Input::__construct`: Refactoring. --- src/Input.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Input.php b/src/Input.php index 22392cc..a780bcd 100644 --- a/src/Input.php +++ b/src/Input.php @@ -40,7 +40,7 @@ class Input extends \DDTools\BaseClass { /** * __construct - * @version 4.3.1 (2021-02-24) + * @version 4.3.2 (2021-02-25) * * @param $snippetParams {stdClass} — The object of parameters. @required * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} @@ -100,18 +100,19 @@ public function __construct($snippetParams){ //Make sure orderBy and filter looks like SQL - if (!empty($this->providerParams->orderBy)){ - $this->providerParams->orderBy = str_replace( + foreach ( + [ + 'filter', + 'orderBy' + ] as + $paramName + ){ + $this->providerParams->{$paramName} = str_replace( $this->fieldDelimiter, '`', - $this->providerParams->orderBy + $this->providerParams->{$paramName} ); } - $this->providerParams->filter = str_replace( - $this->fieldDelimiter, - '`', - $this->providerParams->filter - ); } /** From 3e261139a4bdaea561c6995be9cf7c5f919d6bb3 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Fri, 26 Feb 2021 09:45:30 +0300 Subject: [PATCH 37/42] =?UTF-8?q?*=20Outputters=20=E2=86=92=20Yandexmarket?= =?UTF-8?q?=20=E2=86=92=20Parameters:=20Structure=20was=20changed,=20added?= =?UTF-8?q?=20an=20additional=20nesting=20level=20for=20`shopData`,=20`off?= =?UTF-8?q?erFields`=20and=20`templates`=20(with=20backward=20compatibilit?= =?UTF-8?q?y).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 73 ++++++----- src/Input.php | 84 ++++++++++++- src/Outputter/Yandexmarket/Outputter.php | 151 ++++++++++++----------- 3 files changed, 207 insertions(+), 101 deletions(-) diff --git a/README.md b/README.md index dd0a6b9..b914363 100644 --- a/README.md +++ b/README.md @@ -299,32 +299,37 @@ Output in [Sitemap XML format](https://en.wikipedia.org/wiki/Sitemaps). Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). -* `outputterParams->shopData_shopName` +* `outputterParams->shopData` + * Desctription: Shop data. + * Valid values: `object` + * **Required** + +* `outputterParams->shopData->shopName` * Desctription: Short shop name, length <= 20. * Valid values: `string` * **Required** -* `outputterParams->shopData_companyName` +* `outputterParams->shopData->companyName` * Desctription: Company legal name. Internal data that not be displayed but required by Yandex. * Valid values: `string` * **Required** -* `outputterParams->shopData_agency` +* `outputterParams->shopData->agency` * Desctription: Web developers agency name. * Valid values: `string` * Default value: — -* `outputterParams->shopData_currencyId` +* `outputterParams->shopData->currencyId` * Desctription: Currency code (see [Yandex docs](https://yandex.ru/support/partnermarket/currencies.html)). * Valid values: `string` * Default value: `'RUR'` -* `outputterParams->shopData_platform` +* `outputterParams->shopData->platform` * Desctription: `` tag content. * Valid values: `string` * Default value: `'(MODX) Evolution CMS'` -* `outputterParams->shopData_version` +* `outputterParams->shopData->version` * Desctription: `` tag content. * Valid values: `string` * Default value: `'[(settings_version)]'` @@ -339,23 +344,28 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). * Valid values: `integer` * **Required** -* `outputterParams->offerFields_price` +* `outputterParams->offerFields` + * Desctription: Offer fields parameters. + * Valid values: `object` + * **Required** + +* `outputterParams->offerFields->price` * Desctription: A document field name, that contains offer price. - If a document field value is empty, but `outputterParams->offerFields_priceOld` is set, the last will be used instead. + If a document field value is empty, but `outputterParams->offerFields->priceOld` is set, the last will be used instead. * Valid values: `stringTvName` * **Required** -* `outputterParams->offerFields_priceOld` - * Desctription: A document field name, that contains old offer price (must be less then `outputterParams->offerFields_price` or will not be used). +* `outputterParams->offerFields->priceOld` + * Desctription: A document field name, that contains old offer price (must be less then `outputterParams->offerFields->price` or will not be used). * Valid values: `stringTvName` * Default value: — -* `outputterParams->offerFields_picture` +* `outputterParams->offerFields->picture` * Desctription: A document field name, that contains offer picture. * Valid values: `stringTvName` * Default value: — -* `outputterParams->offerFields_name` +* `outputterParams->offerFields->name` * Desctription: A document field name, that contains offer name. If a document field value is empty, the `pagetitle` field will be used instead. * Valid values: @@ -363,59 +373,59 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). * `stringTvName` * Default value: `'pagetitle'` -* `outputterParams->offerFields_model` +* `outputterParams->offerFields->model` * Desctription: A document field name, that contains offer model. * Valid values: * `stringDocFieldName` * `stringTvName` * Default value: — -* `outputterParams->offerFields_model` +* `outputterParams->offerFields->model` * Desctription: A document field name, that contains offer vendor. * Valid values: * `stringDocFieldName` * `stringTvName` * Default value: — -* `outputterParams->offerFields_available` +* `outputterParams->offerFields->available` * Desctription: A document field name, that contains offer availability status (`true`|`false`). * Valid values: * `stringTvName` * `''` — always display `'true'`. * Default value: `''` -* `outputterParams->offerFields_description` +* `outputterParams->offerFields->description` * Desctription: A document field name, that contains offer description (less than 3 000 chars). * Valid values: * `stringDocFieldName` * `stringTvName` * Default value: — -* `outputterParams->offerFields_salesNotes` +* `outputterParams->offerFields->salesNotes` * Desctription: A document field name, that contains offer <[sales_notes](https://yandex.ru/support/partnermarket/elements/sales_notes.html)>. * Valid values: * `stringDocFieldName` * `stringTvName` * Default value: — -* `outputterParams->offerFields_manufacturerWarranty` +* `outputterParams->offerFields->manufacturerWarranty` * Desctription: A document field name, that contains offer manufacturer warraynty status (`true`|`false`). * Valid values: `stringTvName` * Default value: — -* `outputterParams->offerFields_countryOfOrigin` +* `outputterParams->offerFields->countryOfOrigin` * Desctription: A document field name, that contains offer country of origin. * Valid values: * `stringDocFieldName` * `stringTvName` * Default value: — -* `outputterParams->offerFields_homeCourierDelivery` +* `outputterParams->offerFields->homeCourierDelivery` * Desctription: A document field name, that contains offer courier delivery status (`true`|`false`). * Valid values: `stringTvName` * Default value: — -* `outputterParams->offerFields_dimensions` +* `outputterParams->offerFields->dimensions` * Desctription: A document field name, that contains offer dimensions (length, width, height) including packaging. Specify dimensions in centimeters. Format: three positive numbers with accuracy of 0.001, using a dot as the decimal separator. The numbers must be separated by the slash character `/` without spaces. @@ -424,7 +434,7 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). * `stringTvName` * Default value: — -* `outputterParams->offerFields_weight` +* `outputterParams->offerFields->weight` * Desctription: Item weight in kilograms including packaging. Some categories have limits on the minimum or maximum weight. [Download a list of minimum and maximum weight values](https://download.cdn.yandex.net/support/ru/partnermarket/yandex.market-weight.xlsx). @@ -435,21 +445,26 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). * `stringTvName` * Default value: — -* `outputterParams->offerFields_additionalParams` +* `outputterParams->offerFields->additionalParams` * Desctription: A document field name, that contains offer <[param](https://yandex.ru/support/partnermarket/param.html)> elements. * Valid values: * `stringDocFieldName` * `stringTvName` * Default value: — -* `outputterParams->offerFields_customData` +* `outputterParams->offerFields->customData` * Desctription: A document field name, that contains custom text that must be inserted before ``. * Valid values: * `stringDocFieldName` * `stringTvName` * Default value: — -* `outputterParams->templates_wrapper` +* `outputterParams->templates` + * Desctription: Templates. + * Valid values: `object` + * Default value: — + +* `outputterParams->templates->wrapper` * Desctription: Wrapper template. Available placeholders: * `[+`any document field or tv name`+]` — Any document field name or TV. @@ -478,7 +493,7 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). ``` -* `outputterParams->templates_categories_item` +* `outputterParams->templates->categories_item` * Desctription: Category item template. Available placeholders: * `[+id+]` — Category doc ID. @@ -495,7 +510,7 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). ``` -* `outputterParams->templates_offers_item` +* `outputterParams->templates->offers_item` * Desctription: Offer item template. Available placeholders: * `[+`any document field or tv name`+]` — Any document field name or TV. @@ -529,8 +544,8 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). ``` -* `outputterParams->{'templates_offers_item_elem' . $FieldName}` - * Desctription: You can set custom template for any offer element. Specify an element name in accordance with offerFields_ parameters, e. g. `outputterParams->templates_offers_item_elemCountryOfOrigin`. +* `outputterParams->templates->{'offers_item_elem' . $FieldName}` + * Desctription: You can set custom template for any offer element. Specify an element name in accordance with `offerFields->` parameters, e. g. `outputterParams->templates->offers_item_elemCountryOfOrigin`. Available placeholders: * `[+tagName+]` — Element tag name. * `[+value+]` — Element value. diff --git a/src/Input.php b/src/Input.php index a780bcd..1274c7b 100644 --- a/src/Input.php +++ b/src/Input.php @@ -40,7 +40,7 @@ class Input extends \DDTools\BaseClass { /** * __construct - * @version 4.3.2 (2021-02-25) + * @version 4.4 (2021-02-26) * * @param $snippetParams {stdClass} — The object of parameters. @required * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} @@ -87,6 +87,8 @@ public function __construct($snippetParams){ //Backward compatibility $snippetParams = $this->backwardCompatibility_dataProviderParams($snippetParams); + $this->backwardCompatibility_outputterParams(); + $snippetParams = $this->prepareExtendersParams($snippetParams); @@ -219,4 +221,84 @@ private function backwardCompatibility_dataProviderParams($snippetParams){ return $snippetParams; } + + /** + * backwardCompatibility_outputterParams + * @version 1.0 (2021-02-25) + * + * @desc Prepare data provider params preserve backward compatibility. + * + * @return {void} + */ + private function backwardCompatibility_outputterParams(){ + if ( + $this->outputter == 'yandexmarket' && + //If required shopData is not set, then we need to provide backward compatibility + !\DDTools\ObjectTools::isPropExists([ + 'object' => $this->outputterParams, + 'propName' => 'shopData' + ]) + ){ + //If shopData is not set, then offerFields and templates are not set too + $this->outputterParams->shopData = (object) []; + $this->outputterParams->offerFields = (object) []; + $this->outputterParams->templates = (object) []; + + foreach ( + $this->outputterParams as + $paramName => + $paramValue + ){ + $targetGroupName = NULL; + $targetParamName = NULL; + + //$this->outputterParams->shopData + if ( + substr( + $paramName, + 0, + 9 + ) == 'shopData_' + ){ + $targetGroupName = 'shopData'; + $targetParamName = substr( + $paramName, + 9 + ); + //$this->outputterParams->offerFields + }elseif ( + substr( + $paramName, + 0, + 12 + ) == 'offerFields_' + ){ + $targetGroupName = 'offerFields'; + $targetParamName = substr( + $paramName, + 12 + ); + //$this->outputterParams->templates + }elseif ( + substr( + $paramName, + 0, + 10 + ) == 'templates_' + ){ + $targetGroupName = 'offerFields'; + $targetParamName = substr( + $paramName, + 10 + ); + } + + if (!is_null($targetGroupName)){ + $this->outputterParams->{$targetGroupName}->{$targetParamName} = $paramValue; + + unset($this->outputterParams->{$paramName}); + } + } + } + } } \ No newline at end of file diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index 7dc78a7..fa2977a 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -115,9 +115,10 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { /** * @property $templates {stdClass} * @property $templates->wrapper {string} + * @property $templates->categories_item {string} * @property $templates->offers_item {string} * @property $templates->offers_item_elem {string} - * @property $templates->offers_item_elemParam {string} + * @property $templates->{'offers_item_elem' . $FieldName} {string} */ $templates = [ 'wrapper' => ' @@ -175,38 +176,41 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { /** * __construct - * @version 1.5 (2021-02-08) + * @version 2.0 (2021-02-25) * * @note @link https://yandex.ru/support/partnermarket/export/yml.html * * @param $params {stdClass|arrayAssociative} - * @param $params->shopData_shopName {string} — Короткое название магазина, не более 20 символов. @required - * @param $params->shopData_companyName {string} — Полное наименование компании, владеющей магазином. Не публикуется, используется для внутренней идентификации. @required - * @param $params->shopData_agency {string} — Наименование агентства, которое оказывает техническую поддержку магазину и отвечает за работоспособность сайта. Default: —. - * @param $params->shopData_currencyId {string} — Currency code (https://yandex.ru/support/partnermarket/currencies.html). Default: 'RUR'. - * @param $params->shopData_platform {string} — Содержимое тега ``. Default: '(MODX) Evolution CMS'. - * @param $params->shopData_version {string} — Содержимое тега ``. Default: '[(settings_version)]'. + * @param $params->shopData {stdClass} — Shop data. @required + * @param $params->shopData->shopName {string} — Короткое название магазина, не более 20 символов. @required + * @param $params->shopData->companyName {string} — Полное наименование компании, владеющей магазином. Не публикуется, используется для внутренней идентификации. @required + * @param $params->shopData->agency {string} — Наименование агентства, которое оказывает техническую поддержку магазину и отвечает за работоспособность сайта. Default: —. + * @param $params->shopData->currencyId {string} — Currency code (https://yandex.ru/support/partnermarket/currencies.html). Default: 'RUR'. + * @param $params->shopData->platform {string} — Содержимое тега ``. Default: '(MODX) Evolution CMS'. + * @param $params->shopData->version {string} — Содержимое тега ``. Default: '[(settings_version)]'. + * @param $params->offerFields {stdClass} — Offer fields parameters. + * @param $params->offerFields->price {stringTvName|''} — Поле, содержащее актуальную цену товара. @required + * @param $params->offerFields->priceOld {stringTvName} — Поле, содержащее старую цену товара (должна быть выше актуальной цены). Default: —. + * @param $params->offerFields->picture {stringTvName} — Поле, содержащее изображение товара. Defalut: —. + * @param $params->offerFields->name {stringDocFieldName|stringTvName} — Поле, содержащее модель товара. Default: 'pagetitle'. + * @param $params->offerFields->model {stringDocFieldName|stringTvName} — Поле, содержащее модель товара. Defalut: —. + * @param $params->offerFields->vendor {stringDocFieldName|stringTvName} — Поле, содержащее производителя товара. Defalut: —. + * @param $params->offerFields->available {stringDocFieldName|''} — Поле, содержащее статус товара ('true'|'false'). Default: '' (всегда выводить 'true'). + * @param $params->offerFields->description {stringDocFieldName|stringTvName} — Поле, содержащее описание предложения (длина текста — не более 3000 символов). Default: —. + * @param $params->offerFields->salesNotes {stringDocFieldName|stringTvName} — Поле, содержащее «sales_notes» (https://yandex.ru/support/partnermarket/elements/sales_notes.html). Default: —. + * @param $params->offerFields->manufacturerWarranty {stringTvName} — Поле, содержащее наличие официальной гарантии производителя ('true'|'false'). Default: —. + * @param $params->offerFields->countryOfOrigin {stringDocFieldName|stringTvName} — Поле, содержащее страну производства товара. Default: —. + * @param $params->offerFields->homeCourierDelivery {stringTvName} — Поле, содержащее возможность курьерской доставки по региону магазина ('true'|'false'). Default: —. + * @param $params->offerFields->dimensions {stringDocFieldName|stringTvName} — Поле, содержащее габариты товара (длина, ширина, высота) в упаковке (размеры укажите в сантиметрах, формат: три положительных числа с точностью 0.001, разделитель целой и дробной части — точка, числа должны быть разделены символом «/» без пробелов). Default: —. + * @param $params->offerFields->weight {stringDocFieldName|stringTvName} — Поле, содержащее вес товара в килограммах с учетом упаковки (формат: положительное число с точностью 0.001, разделитель целой и дробной части — точка). Default: —. + * @param $params->offerFields->additionalParams {stringDocFieldName|stringTvName} — Поле, содержащее элементы «param» (https://yandex.ru/support/partnermarket/param.html). Default: —. + * @param $params->offerFields->customData {stringDocFieldName|stringTvName} — Поле, содержащее произвольный текст, который будет вставлен перед закрывающим тегом «». Default: —. + * @param $params->templates {stdClass} — Templates. Default: — + * @param $params->templates->wrapper {stringChunkName|string} — Available placeholders: [+ddGetDocuments_items+], [+any of extender placeholders+]. Default: ''. + * @param $params->templates->categories_item {stringChunkName|string} — Available placeholders: [+id+], [+value+], [+parent+]. Default: '[+value+]'. + * @param $params->templates->offers_item {stringChunkName|string} — Available placeholders: [+any field or tv name+], [+any of extender placeholders+]. Default: ''. + * @param $params->templates->{'offers_item_elem' . $FieldName} {stringChunkName|string} — Можно задать шаблон любого элемента offer, называем в соответствии с параметрами 'offerFields_', например: $params->templates_offers_item_elemCountryOfOrigin. Default: —. * @param $params->categoryIds_last {stringCommaSepareted} — id конечных категорий(parent). Если пусто то выводятся только непосредственный родитель товара. Defalut: —. - * @param $params->offerFields_price {stringTvName|''} — Поле, содержащее актуальную цену товара. @required - * @param $params->offerFields_priceOld {stringTvName} — Поле, содержащее старую цену товара (должна быть выше актуальной цены). Default: —. - * @param $params->offerFields_picture {stringTvName} — Поле, содержащее изображение товара. Defalut: —. - * @param $params->offerFields_name {stringDocFieldName|stringTvName} — Поле, содержащее модель товара. Default: 'pagetitle'. - * @param $params->offerFields_model {stringDocFieldName|stringTvName} — Поле, содержащее модель товара. Defalut: —. - * @param $params->offerFields_vendor {stringDocFieldName|stringTvName} — Поле, содержащее производителя товара. Defalut: —. - * @param $params->offerFields_available {stringDocFieldName|''} — Поле, содержащее статус товара ('true'|'false'). Default: '' (всегда выводить 'true'). - * @param $params->offerFields_description {stringDocFieldName|stringTvName} — Поле, содержащее описание предложения (длина текста — не более 3000 символов). Default: —. - * @param $params->offerFields_salesNotes {stringDocFieldName|stringTvName} — Поле, содержащее «sales_notes» (https://yandex.ru/support/partnermarket/elements/sales_notes.html). Default: —. - * @param $params->offerFields_manufacturerWarranty {stringTvName} — Поле, содержащее наличие официальной гарантии производителя ('true'|'false'). Default: —. - * @param $params->offerFields_countryOfOrigin {stringDocFieldName|stringTvName} — Поле, содержащее страну производства товара. Default: —. - * @param $params->offerFields_homeCourierDelivery {stringTvName} — Поле, содержащее возможность курьерской доставки по региону магазина ('true'|'false'). Default: —. - * @param $params->offerFields_dimensions {stringDocFieldName|stringTvName} — Поле, содержащее габариты товара (длина, ширина, высота) в упаковке (размеры укажите в сантиметрах, формат: три положительных числа с точностью 0.001, разделитель целой и дробной части — точка, числа должны быть разделены символом «/» без пробелов). Default: —. - * @param $params->offerFields_weight {stringDocFieldName|stringTvName} — Поле, содержащее вес товара в килограммах с учетом упаковки (формат: положительное число с точностью 0.001, разделитель целой и дробной части — точка). Default: —. - * @param $params->offerFields_additionalParams {stringDocFieldName|stringTvName} — Поле, содержащее элементы «param» (https://yandex.ru/support/partnermarket/param.html). Default: —. - * @param $params->offerFields_customData {stringDocFieldName|stringTvName} — Поле, содержащее произвольный текст, который будет вставлен перед закрывающим тегом «». Default: —. - * @param $params->templates_wrapper {stringChunkName|string} — Available placeholders: [+ddGetDocuments_items+], [+any of extender placeholders+]. Default: ''. - * @param $params->templates_categories_item {stringChunkName|string} — Available placeholders: [+id+], [+value+], [+parent+]. Default: '[+value+]'. - * @param $params->templates_offers_item {stringChunkName|string} — Available placeholders: [+any field or tv name+], [+any of extender placeholders+]. Default: ''. - * @param $params->{'templates_offers_item_elem' . $FieldName} {stringChunkName|string} — Можно задать шаблон любого элемента offer, называем в соответствии с параметрами 'offerFields_', например: $params->templates_offers_item_elemCountryOfOrigin. Default: —. */ function __construct($params = []){ $params = (object) $params; @@ -216,55 +220,46 @@ function __construct($params = []){ $this->construct_prepareFields(); - //# Call base constructor - parent::__construct($params); - - - //# Save shop data and offer fields - foreach ( - $params as - $paramName => - $paramValue + //# Save shopData and templates + foreach( + [ + 'shopData', + 'templates' + ] as + $fieldName ){ - //Shop data + //If parameter is passed if ( - substr( - $paramName, - 0, - 9 - ) == 'shopData_' - ){ - $this->shopData->{substr( - $paramName, - 9 - )} = $paramValue; - //Offer field names - }elseif ( - substr( - $paramName, - 0, - 12 - ) == 'offerFields_' + \DDTools\ObjectTools::isPropExists([ + 'object' => $params, + 'propName' => $fieldName + ]) ){ - $this->offerFields->{substr( - $paramName, - 12 - )}->docFieldName = $paramValue; - //Templates - }elseif ( - substr( - $paramName, - 0, - 10 - ) == 'templates_' - ){ - $this->templates->{substr( - $paramName, - 10 - )} = \ddTools::$modx->getTpl($paramValue); + $this->{$fieldName} = \DDTools\ObjectTools::extend([ + 'objects' => [ + $this->{$fieldName}, + $params->{$fieldName} + ] + ]); + + //Remove from params to prevent overwriting through `$this->setExistingProps` + unset($params->{$fieldName}); } } + + //# Save offerFields + foreach ( + $params->offerFields as + $offerFieldName => + $offerFieldParamValue + ){ + $this->offerFields->{$offerFieldName}->docFieldName = $offerFieldParamValue; + } + + //Remove from params to prevent overwriting through `$this->setExistingProps` + unset($params->offerFields); + //If name doc field is not set if (empty($this->offerFields->name->docFieldName)){ //Pagetitle will be used @@ -272,6 +267,20 @@ function __construct($params = []){ } + //# Prepare templates + foreach ( + $this->templates as + $templateName => + $templateValue + ){ + $this->templates->{$templateName} = \ddTools::$modx->getTpl($templateValue); + } + + + //# Call base constructor + parent::__construct($params); + + //# Prepare last parent category IDs if (!is_array($this->categoryIds_last)){ $this->categoryIds_last = explode( From 46c79e9ff68f990a9f69cf5a55619ca58807c6b8 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Fri, 26 Feb 2021 09:59:45 +0300 Subject: [PATCH 38/42] * `\ddGetDocuments\Input`: Refactoring. --- src/Input.php | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/Input.php b/src/Input.php index 1274c7b..3de665b 100644 --- a/src/Input.php +++ b/src/Input.php @@ -40,7 +40,7 @@ class Input extends \DDTools\BaseClass { /** * __construct - * @version 4.4 (2021-02-26) + * @version 4.4.1 (2021-02-26) * * @param $snippetParams {stdClass} — The object of parameters. @required * @param $snippetParams->providerParams {stdClass|arrayAssociative|stringJsonObject} @@ -79,18 +79,17 @@ public function __construct($snippetParams){ ]); } - //No needed in snippet params + //Remove it to prevent overwriting through `$this->setExistingProps` unset($snippetParams->{$paramName}); } //Backward compatibility - $snippetParams = $this->backwardCompatibility_dataProviderParams($snippetParams); - + $this->backwardCompatibility_dataProviderParams($snippetParams); $this->backwardCompatibility_outputterParams(); - $snippetParams = $this->prepareExtendersParams($snippetParams); + $this->prepareExtendersParams($snippetParams); //Set object properties from snippet parameters @@ -119,13 +118,13 @@ public function __construct($snippetParams){ /** * prepareExtendersParams - * @version 2.1 (2021-02-15) + * @version 3.0 (2021-02-26) * * @desc Prepare extenders params. * * @param $snippetParams {stdClass} * - * @return {stdClass} + * @return {void} */ private function prepareExtendersParams($snippetParams){ if ( @@ -178,23 +177,18 @@ private function prepareExtendersParams($snippetParams){ } } } - - //No needed anymore, all data was saved to $this->extendersParams - unset($snippetParams->extenders); } - - return $snippetParams; } /** * backwardCompatibility_dataProviderParams - * @version 1.0 (2021-02-15) + * @version 2.0 (2021-02-26) * * @desc Prepare data provider params preserve backward compatibility. * * @param $snippetParams {stdClass} * - * @return {stdClass} + * @return {void} */ private function backwardCompatibility_dataProviderParams($snippetParams){ //Move parameters from snippetParams to providerParams @@ -214,12 +208,8 @@ private function backwardCompatibility_dataProviderParams($snippetParams){ ]) ){ $this->providerParams->{$paramName} = $snippetParams->{$paramName}; - - unset($snippetParams->{$paramName}); } } - - return $snippetParams; } /** From bca4455ea71dff5d330d94f44212cae83b6881a6 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sat, 27 Feb 2021 15:31:50 +0300 Subject: [PATCH 39/42] =?UTF-8?q?+=20Outputters=20=E2=86=92=20Yandexmarket?= =?UTF-8?q?:=20Added=20the=20ability=20to=20disable=20escaping=20special?= =?UTF-8?q?=20characters=20(`'`,=20`"`,=20`&`,=20`<`,=20`>`)=20in=20the=20?= =?UTF-8?q?field=20value=20(see=20`outputterParams->offerFields->{$fieldNa?= =?UTF-8?q?me}->disableEscaping`).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 +++++++++++++ src/Outputter/Yandexmarket/Outputter.php | 32 +++++++++++++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b914363..e38e835 100644 --- a/README.md +++ b/README.md @@ -349,6 +349,23 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). * Valid values: `object` * **Required** +* `outputterParams->offerFields->{$fieldName}` + * Desctription: Offer field parameter. + * Valid values: + * `string` — the parameter can be set as a document field name + * `object` — or as an object with additional params (see below) + * Default value: — + +* `outputterParams->offerFields->{$fieldName}->docFieldName` + * Desctription: A document field name that contains offer field value. + * Valid values: `stringTvName` + * **Required** + +* `outputterParams->offerFields->{$fieldName}->disableEscaping` + * Desctription: You can disable escaping special characters (`'`, `"`, `&`, `<`, `>`) in the field value. + * Valid values: `boolean` + * Default value: `false` + * `outputterParams->offerFields->price` * Desctription: A document field name, that contains offer price. If a document field value is empty, but `outputterParams->offerFields->priceOld` is set, the last will be used instead. diff --git a/src/Outputter/Yandexmarket/Outputter.php b/src/Outputter/Yandexmarket/Outputter.php index fa2977a..48215ab 100644 --- a/src/Outputter/Yandexmarket/Outputter.php +++ b/src/Outputter/Yandexmarket/Outputter.php @@ -35,6 +35,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { * @property $offerFields->{$fieldName}->valuePrefix {string} — Префикс, который будет добавлен к значению при выводе. * @property $offerFields->{$fieldName}->valueSuffix {string} — Суффикс, который будет добавлен к значению при выводе. * @property $offerFields->{$fieldName}->templateName {string} — Название шаблона, по которому парсить вывод. + * @property $offerFields->{$fieldName}->disableEscaping {boolean} — Отключить экранирование специальных символов? */ $offerFields = [ 'name' => [ @@ -176,7 +177,7 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter { /** * __construct - * @version 2.0 (2021-02-25) + * @version 2.1 (2021-02-27) * * @note @link https://yandex.ru/support/partnermarket/export/yml.html * @@ -254,7 +255,18 @@ function __construct($params = []){ $offerFieldName => $offerFieldParamValue ){ - $this->offerFields->{$offerFieldName}->docFieldName = $offerFieldParamValue; + //If parameter set as full data + if (is_object($offerFieldParamValue)){ + $this->offerFields->{$offerFieldName} = \DDTools\ObjectTools::extend([ + 'objects' => [ + $this->offerFields->{$offerFieldName}, + $offerFieldParamValue + ] + ]); + //If parameter set as doc field name only + }else{ + $this->offerFields->{$offerFieldName}->docFieldName = $offerFieldParamValue; + } } //Remove from params to prevent overwriting through `$this->setExistingProps` @@ -442,7 +454,7 @@ private function escapeSpecialChars($inputString){ /** * parse - * @version 1.5.1 (2021-02-09) + * @version 1.6 (2021-02-27) * * @param $data {Output} * @@ -603,7 +615,19 @@ public function parse(Output $data){ 'text' => $this->templates->{$templateName}, 'data' => [ 'tagName' => $offerFieldData->tagName, - 'value' => $this->escapeSpecialChars($docData[$offerFieldData->docFieldName]) + 'value' => + //If escaping is disabled + ( + \DDTools\ObjectTools::isPropExists([ + 'object' => $offerFieldData, + 'propName' => 'disableEscaping' + ]) && + $offerFieldData->disableEscaping + ) ? + //Unescaped value + $docData[$offerFieldData->docFieldName] : + //Escaped value + $this->escapeSpecialChars($docData[$offerFieldData->docFieldName]) ], 'mergeAll' => false ]); From 60eecc8ace9197d69faa35279607b0bc4d5272d2 Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sat, 27 Feb 2021 15:49:05 +0300 Subject: [PATCH 40/42] =?UTF-8?q?+=20Outputters=20=E2=86=92=20Yandexmarket?= =?UTF-8?q?:=20Also=20you=20can=20set=20a=20custom=20string=20that=20will?= =?UTF-8?q?=20be=20added=20before=20or=20after=20the=20field=20value.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index e38e835..7e760bb 100644 --- a/README.md +++ b/README.md @@ -366,6 +366,16 @@ Output in [YML format](https://yandex.ru/support/partnermarket/export/yml.html). * Valid values: `boolean` * Default value: `false` +* `outputterParams->offerFields->{$fieldName}->valuePrefix` + * Desctription: You can set custom string that will be added before the field value. + * Valid values: `string` + * Default value: — + +* `outputterParams->offerFields->{$fieldName}->valueSuffix` + * Desctription: You can set custom string that will be added after the field value. + * Valid values: `string` + * Default value: — + * `outputterParams->offerFields->price` * Desctription: A document field name, that contains offer price. If a document field value is empty, but `outputterParams->offerFields->priceOld` is set, the last will be used instead. From 6fb23224019dab415cd2cd08a8d426a30f68d56e Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sat, 27 Feb 2021 16:03:21 +0300 Subject: [PATCH 41/42] =?UTF-8?q?+=20REAMDE=20=E2=86=92=C2=A0Links=20?= =?UTF-8?q?=E2=86=92=20Packagist.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7e760bb..fe7c10a 100644 --- a/README.md +++ b/README.md @@ -971,6 +971,7 @@ We recommend to use cashed snippet calls and turn on document caching type with * [Home page](https://code.divandesign.biz/modx/ddgetdocuments) * [Telegram chat](https://t.me/dd_code) +* [Packagist](https://packagist.org/packages/dd/evolutioncms-snippets-ddgetdocuments) \ No newline at end of file From 110452d29e3eba6a28488ceb23b48d527d26e51d Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Sat, 27 Feb 2021 16:23:24 +0300 Subject: [PATCH 42/42] Prerelease --- CHANGELOG.md | 23 +++++++++++++++++++++++ README.md | 2 +- composer.json | 2 +- ddGetDocuments_snippet.php | 4 ++-- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bc5fd7..54c7f2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,29 @@ # (MODX)EvolutionCMS.snippets.ddGetDocuments changelog +## Version 1.3 (2021-02-27) +* \* Attention! (MODX)Evolution.libraries.ddTools >= 0.42 is required. +* \* Parameters: The following parameters were moved from Snippet to Provider (with backward compatibility): + * \* `filter` → `providerParams->filter`. + * \* `offset` → `providerParams->offset`. + * \* `total` → `providerParams->total`. +* \* Outputters → Yandexmarket: + * \* Fixed smart name and price preparation. + * \* Parameters: Structure was changed, added an additional nesting level for `shopData`, `offerFields` and `templates` (with backward compatibility). + * \+ Added the ability to disable escaping special characters (`'`, `"`, `&`, `<`, `>`) in the field value (see `outputterParams->offerFields->{$fieldName}->disableEscaping`). + * \+ Also you can set a custom string that will be added before or after the field value (see `outputterParams->offerFields->{$fieldName}->valuePrefix` and `outputterParams->offerFields->{$fieldName}->valueSuffix`). +* \+ Added the ability to run snippet without DB requests and PHP `eval`: + ```php + \DDTools\Snippet::runSnippet([ + 'name' => 'ddGetDocuments', + 'params' => $params + ]); + ``` +* \+ README: + * \+ Parameters description → Extenders parameters → `extendersParams->{$extenderName}`. + * \+ Links → Packagist. + + ## Version 1.2 (2020-10-09) * \+ Extenders → SortFromURL (see README). * \* Parameters: The following were changed (with backward compatibility): diff --git a/README.md b/README.md index fe7c10a..afaed8b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ A snippet for fetching and parsing resources from the document tree or custom DB #### 1. Elements → Snippets: Create a new snippet with the following data 1. Snippet name: `ddGetDocuments`. -2. Description: `1.2 A snippet for fetching and parsing resources from the document tree or custom DB table by a custom rule.`. +2. Description: `1.3 A snippet for fetching and parsing resources from the document tree or custom DB table by a custom rule.`. 3. Category: `Core`. 4. Parse DocBlock: `no`. 5. Snippet code (php): Insert content of the `ddGetDocuments_snippet.php` file from the archive. diff --git a/composer.json b/composer.json index 2761436..e2707ed 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "dd/evolutioncms-snippets-ddgetdocuments", "type": "modxevo-snippet", - "version": "1.2.0", + "version": "1.3.0", "description": "A snippet for fetching and parsing resources from the document tree or custom DB table by a custom rule.", "keywords": [ "modx", diff --git a/ddGetDocuments_snippet.php b/ddGetDocuments_snippet.php index 2f84c08..cdff53f 100644 --- a/ddGetDocuments_snippet.php +++ b/ddGetDocuments_snippet.php @@ -1,13 +1,13 @@