Skip to content

Commit

Permalink
+ Outputters → Json → Parameters → outputterParams->docFields[i]: A…
Browse files Browse the repository at this point in the history
…dded ability to use custom aliases instead of field names for output using the `'='` separator, for example: `'pagetitle=title'`, `'content=text'`, etc. (see README → Examples).
  • Loading branch information
Ronef committed Oct 6, 2024
1 parent 4fbd273 commit 07aa33e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 8 deletions.
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,10 @@ Get resources from custom DB table.

* `outputterParams->docFields[i]`
* Desctription: Document field or TV.
* You can use custom aliases instead of field names for output using the `'='` separator (for example: `'pagetitle=title'`, `'content=text'`, etc).
* Valid values:
* `stringDocFieldName`
* `stringTvName`
* `string` — document field
* `stringSeparated` — document field and it's alias separated by `'='`
* **Required**

* `outputterParams->templates->{$docFieldName}`
Expand Down Expand Up @@ -983,6 +984,48 @@ Returns:
```


### Outputters → JSON (``&outputter=`json` ``): Use custom aliases instead of field names

```
[[ddGetDocuments?
&providerParams=`{
parentIds: 1
}`
&outputter=`json`
&outputterParams=`{
docFields: pagetitle=name,menuindex=position
}`
]]
```

Returns:

```json
[
{
"name": "Denial",
"position": "0",
},
{
"name": "Anger",
"position": "1",
},
{
"name": "Bargaining",
"position": "2",
},
{
"name": "Depression",
"position": "3",
},
{
"name": "Acceptance",
"position": "4",
}
]
```


### Group items that have the same field values into summary item (`providerParams->orderBy`)

For example we have the following documents with TV `gender`:
Expand Down
19 changes: 17 additions & 2 deletions src/Outputter/Json/Outputter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Outputter extends \ddGetDocuments\Outputter\Outputter {
/**
* parse
* @version 2.4 (2024-10-05)
* @version 2.5 (2024-10-05)
*
* @param $data {Output}
*
Expand All @@ -16,6 +16,8 @@ class Outputter extends \ddGetDocuments\Outputter\Outputter {
public function parse(Output $data){
$result = [];

$isFieldAliasesUsed = !\ddTools::isEmpty($this->fieldAliases);

// Пройдемся по полученным данным
foreach(
$data->provider->items
Expand All @@ -29,7 +31,20 @@ public function parse(Output $data){
$this->docFields
as $docField
){
$result_item[$docField] = $itemData[$docField];
// If aliases are used
if (
$isFieldAliasesUsed
&& \DDTools\Tools\Objects::isPropExists([
'object' => $this->fieldAliases,
'propName' => $docField,
])
){
$result_item[$this->fieldAliases->{$docField}] = $itemData[$docField];

$docField = $this->fieldAliases->{$docField};
}else{
$result_item[$docField] = $itemData[$docField];
}

// If template for this field is set
if (
Expand Down
46 changes: 42 additions & 4 deletions src/Outputter/Outputter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ abstract class Outputter extends \DDTools\Base\Base {
*/
$docFields = ['id'],

/**
* @property $fieldAliases {stdClass} — Aliases of fields if used.
* @property $fieldAliases->{$fieldName} {string} — A key is an original field name, a value is an alias.
*/
$fieldAliases = [],

/**
* @property $templates {stdClass}
* @property $templates->{$templateName} {string}
Expand All @@ -22,7 +28,7 @@ abstract class Outputter extends \DDTools\Base\Base {

/**
* __construct
* @version 1.5.3 (2024-08-06)
* @version 1.6 (2024-10-05)
*
* @param $params {stdClass|arrayAssociative}
* @param $params->dataProvider {\ddGetDocuments\DataProvider\DataProvider}
Expand All @@ -38,6 +44,8 @@ public function __construct($params = []){
// Все параметры задают свойства объекта
$this->setExistingProps($params);

$this->fieldAliases = (object) $this->fieldAliases;

// Comma separated strings
if (!is_array($this->docFields)){
$this->docFields = explode(
Expand All @@ -49,9 +57,39 @@ public function __construct($params = []){
if (empty($this->docFields)){
// We need something
$this->docFields = ['id'];
}elseif (isset($params->dataProvider)){
// Ask dataProvider to get them
$params->dataProvider->addResourcesFieldsToGet($this->docFields);
}else{
// Prepare field aliases
foreach (
$this->docFields
as $fieldNameIndex
=> $fieldName
){
// If alias is used
if (
strpos(
$fieldName,
'='
)
!== false
){
// E. g. 'pagetitle=title'
$fieldName = explode(
'=',
$fieldName
);

// Remove alias from field name
$this->docFields[$fieldNameIndex] = $fieldName[0];

// Save alias
$this->fieldAliases->{$this->docFields[$fieldNameIndex]} = $fieldName[1];
}
}

if (isset($params->dataProvider)){
// Ask dataProvider to get them
$params->dataProvider->addResourcesFieldsToGet($this->docFields);
}
}
}

Expand Down

0 comments on commit 07aa33e

Please sign in to comment.