From 4b90df414a406d7b4b8bd52cb1818ebfc56324e9 Mon Sep 17 00:00:00 2001 From: David Mellen Date: Tue, 9 May 2023 08:16:06 +0200 Subject: [PATCH 1/4] [FEATURE] Allow fields and dataProcessing merging in JSON --- Classes/ContentObject/JsonContentObject.php | 29 ++++++++++++--------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Classes/ContentObject/JsonContentObject.php b/Classes/ContentObject/JsonContentObject.php index 3ea68daa..18e4f4b8 100755 --- a/Classes/ContentObject/JsonContentObject.php +++ b/Classes/ContentObject/JsonContentObject.php @@ -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 = ''; @@ -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]; @@ -117,19 +116,24 @@ 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, + $content[rtrim($theKey, '.')] + ); } } 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, '.')] + ); } } } @@ -155,11 +159,11 @@ 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 = []): mixed { + $dataProcessing['dataProcessing.'] = $conf['dataProcessing.'] ?? []; + $merge = (int)($conf['merge'] ?? 0) === 1; + $data = $this->contentDataProcessor->process( $this->cObj, $dataProcessing, @@ -177,7 +181,8 @@ protected function processFieldWithDataProcessing(array $dataProcessing): mixed $dataProcessingData = $data[$value]; } } - return $dataProcessingData; + $fieldsData = is_array($fieldsData) ? $fieldsData : []; + return $merge ? array_merge($fieldsData, $dataProcessingData ?? []) : $dataProcessingData; } /** From a0112ed10ab5f70ee22b5a54281890818733e2ec Mon Sep 17 00:00:00 2001 From: David Mellen Date: Tue, 9 May 2023 10:08:34 +0200 Subject: [PATCH 2/4] Fix possible undefined array key --- Classes/ContentObject/JsonContentObject.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Classes/ContentObject/JsonContentObject.php b/Classes/ContentObject/JsonContentObject.php index 18e4f4b8..4b85d49b 100755 --- a/Classes/ContentObject/JsonContentObject.php +++ b/Classes/ContentObject/JsonContentObject.php @@ -119,7 +119,7 @@ public function cObjGet(array $setup, string $addKey = ''): array if (!empty($conf['dataProcessing.'] ?? [])) { $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing( $conf, - $content[rtrim($theKey, '.')] + $content[rtrim($theKey, '.')] ?? null ); } } @@ -132,7 +132,7 @@ public function cObjGet(array $setup, string $addKey = ''): array if (!empty($theValue['dataProcessing.'] ?? [])) { $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing( $theValue, - $content[rtrim($theKey, '.')] + $content[rtrim($theKey, '.')] ?? null ); } } @@ -159,10 +159,9 @@ protected function filterByStringKeys(array $setupArr, bool $acceptAnyKeys = fal return array_unique($filteredKeys); } - protected function processFieldWithDataProcessing(array $conf, mixed $fieldsData = []): mixed + protected function processFieldWithDataProcessing(array $conf, mixed $fieldsData = null): mixed { $dataProcessing['dataProcessing.'] = $conf['dataProcessing.'] ?? []; - $merge = (int)($conf['merge'] ?? 0) === 1; $data = $this->contentDataProcessor->process( $this->cObj, @@ -181,8 +180,8 @@ protected function processFieldWithDataProcessing(array $conf, mixed $fieldsData $dataProcessingData = $data[$value]; } } - $fieldsData = is_array($fieldsData) ? $fieldsData : []; - return $merge ? array_merge($fieldsData, $dataProcessingData ?? []) : $dataProcessingData; + $merge = ((int)($conf['merge'] ?? 0) === 1) && is_array($fieldsData) && is_array($dataProcessingData); + return $merge ? array_merge($fieldsData, $dataProcessingData) : $dataProcessingData; } /** From 5a5c572efafd8cb27c014ff372bb12ba1ab8af7d Mon Sep 17 00:00:00 2001 From: David Mellen Date: Tue, 9 May 2023 10:59:20 +0200 Subject: [PATCH 3/4] Remove unnecessary argument --- Classes/ContentObject/JsonContentObject.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Classes/ContentObject/JsonContentObject.php b/Classes/ContentObject/JsonContentObject.php index 4b85d49b..857f67dd 100755 --- a/Classes/ContentObject/JsonContentObject.php +++ b/Classes/ContentObject/JsonContentObject.php @@ -117,10 +117,7 @@ public function cObjGet(array $setup, string $addKey = ''): array $content[$theKey] = null; } if (!empty($conf['dataProcessing.'] ?? [])) { - $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing( - $conf, - $content[rtrim($theKey, '.')] ?? null - ); + $content[rtrim($theKey, '.')] = $this->processFieldWithDataProcessing($conf); } } if ((string)$theKey && strpos($theKey, '.') > 0 && !isset($setup[rtrim($theKey, '.')])) { From 3e2362f1241dd5fbd57b2bfd014970e261b63bfa Mon Sep 17 00:00:00 2001 From: David Mellen Date: Tue, 9 May 2023 11:01:58 +0200 Subject: [PATCH 4/4] Add unit tests --- Tests/Unit/ContentObject/JsonContentObjectTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/Unit/ContentObject/JsonContentObjectTest.php b/Tests/Unit/ContentObject/JsonContentObjectTest.php index bc121ec0..b6f28d5a 100644 --- a/Tests/Unit/ContentObject/JsonContentObjectTest.php +++ b/Tests/Unit/ContentObject/JsonContentObjectTest.php @@ -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<<>>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']])], ]; } }