Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Allow fields and dataProcessing merging in JSON #592

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions Classes/ContentObject/JsonContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function render($conf = []): string
$data = $this->cObjGet($conf['fields.']);
}
if (isset($conf['dataProcessing.'])) {
$data = $this->processFieldWithDataProcessing($conf);
$data = $this->processFieldWithDataProcessing($conf, $data);
}

$json = '';
Expand Down Expand Up @@ -100,7 +100,6 @@ public function cObjGet(array $setup, string $addKey = ''): array
$theValue = $setup[$theKey];
if ((string)$theKey && strpos($theKey, '.') === false) {
$conf = $setup[$theKey . '.'] ?? [];
$contentDataProcessing['dataProcessing.'] = $conf['dataProcessing.'] ?? [];
$content[$theKey] = $this->cObj->cObjGetSingle($theValue, $conf, $addKey . $theKey);
if ((isset($conf['intval']) && $conf['intval']) || $theValue === 'INT') {
$content[$theKey] = (int)$content[$theKey];
Expand All @@ -117,19 +116,21 @@ public function cObjGet(array $setup, string $addKey = ''): array
if ((int)($conf['ifEmptyReturnNull'] ?? 0) === 1 && $content[$theKey] === '') {
$content[$theKey] = null;
}
if (!empty($contentDataProcessing['dataProcessing.'])) {
$content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing($contentDataProcessing);
if (!empty($conf['dataProcessing.'] ?? [])) {
$content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing($conf);
}
}
if ((string)$theKey && strpos($theKey, '.') > 0 && !isset($setup[rtrim($theKey, '.')])) {
$contentFieldName = $theValue['source'] ?? rtrim($theKey, '.');
$contentFieldTypeProcessing['dataProcessing.'] = $theValue['dataProcessing.'] ?? [];

if (array_key_exists('fields.', $theValue)) {
$content[$contentFieldName] = $this->cObjGet($theValue['fields.']);
}
if (!empty($contentFieldTypeProcessing['dataProcessing.'])) {
$content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing($contentFieldTypeProcessing);
if (!empty($theValue['dataProcessing.'] ?? [])) {
$content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing(
$theValue,
$content[rtrim($theKey, '.')] ?? null
);
}
}
}
Expand All @@ -155,11 +156,10 @@ protected function filterByStringKeys(array $setupArr, bool $acceptAnyKeys = fal
return array_unique($filteredKeys);
}

/**
* @param array $dataProcessing
*/
protected function processFieldWithDataProcessing(array $dataProcessing): mixed
protected function processFieldWithDataProcessing(array $conf, mixed $fieldsData = null): mixed
{
$dataProcessing['dataProcessing.'] = $conf['dataProcessing.'] ?? [];

$data = $this->contentDataProcessor->process(
$this->cObj,
$dataProcessing,
Expand All @@ -177,7 +177,8 @@ protected function processFieldWithDataProcessing(array $dataProcessing): mixed
$dataProcessingData = $data[$value];
}
}
return $dataProcessingData;
$merge = ((int)($conf['merge'] ?? 0) === 1) && is_array($fieldsData) && is_array($dataProcessingData);
return $merge ? array_merge($fieldsData, $dataProcessingData) : $dataProcessingData;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions Tests/Unit/ContentObject/JsonContentObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,17 @@ public function dataProvider(): array
[['fields.' => ['test' => 'TEXT', 'test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites'], '20' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '20.' => ['as' => 'sites']]]]], json_encode(['test' => ['SomeCustomProcessing']])],
[['fields.' => ['test' => 'TEXT', 'test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites'], 'dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']]]]]], json_encode(['test' => ['SomeCustomProcessing']])],
[['fields.' => ['test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites'], 'dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']]]]]], json_encode(['test' => ['SomeCustomProcessing']])],
[['returnNullIfDataProcessingEmpty'=>1, 'fields.' => ['test' => 'TEXT', 'test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\EmptyDataProcessingExample', '10.' => ['as' => 'sites']]]]], json_encode(['test' => [null]])],
[['returnNullIfDataProcessingEmpty' => 1, 'fields.' => ['test' => 'TEXT', 'test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\EmptyDataProcessingExample', '10.' => ['as' => 'sites']]]]], json_encode(['test' => [null]])],
[['fields.' => ['test' => 'INT', 'test.' => ['value' => 1]]], json_encode(['test' => 1])],
[['fields.' => ['test' => 'BOOL', 'test.' => ['value' => 0]]], json_encode(['test' => false])],
[['fields.' => ['test' => 'BOOL', 'test.' => ['value' => 1]]], json_encode(['test' => true])],
[['fields.' => ['test' => 'BOOL', 'test.' => ['value' => 1], 'nested.' => ['fields.' => ['nestedTest' => 'INT', 'nestedTest.' => ['value' => 10]]] ]], json_encode(['test' => true, 'nested' => ['nestedTest' => 10]])],
[['fields.' => ['test' => 'BOOL', 'test.' => ['value' => 1], 'nested.' => ['fields.' => ['nestedTest' => 'INT', 'nestedTest.' => ['value' => 10]]]]], json_encode(['test' => true, 'nested' => ['nestedTest' => 10]])],
[['fields.' => ['test' => 'BOOL', 'test.' => ['value' => 1], 'nested.' => ['fields.' => ['nestedTest' => 'INT', 'nestedTest.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']]]]]]], json_encode(['test' => true, 'nested' => ['nestedTest' => ['SomeCustomProcessing']]])],
[['fields.' => ['test' => 'FLOAT', 'test.' => ['value' => 12.34]]], json_encode(['test' => 12.34])],
[['fields.' => ['test' => 'USER_INT', 'test.' => ['userFunc' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\ExampleUserFunc->someUserFunc']]], json_encode(['test' => 'HEADLESS_INT_START<<<!--INT_SCRIPT.202cb962ac59075b964b07152d234b70-->>>HEADLESS_INT_END'])],
[['fields.' => ['test' => 'USER', 'test.' => ['userFunc' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\ExampleUserFunc->someUserFunc']]], json_encode(['test' => ['test2' => 'someExtraCustomData']])],
[['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']], 'merge' => '1', 'fields.' => ['test' => 'TEXT', 'test.' => ['value' => '1']]], json_encode(['test' => '1', 'SomeCustomProcessing'])],
[['fields.' => ['test.' => ['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']], 'merge' => '1', 'fields.' => ['nested' => 'TEXT', 'nested.' => ['value' => '1']]]]], json_encode(['test' => ['nested' => '1', 'SomeCustomProcessing']])],
];
}
}