diff --git a/Classes/ContentObject/JsonContentObject.php b/Classes/ContentObject/JsonContentObject.php index ff08a25d..61dbdc3f 100755 --- a/Classes/ContentObject/JsonContentObject.php +++ b/Classes/ContentObject/JsonContentObject.php @@ -14,24 +14,18 @@ use FriendsOfTYPO3\Headless\Json\JsonDecoderInterface; use FriendsOfTYPO3\Headless\Json\JsonEncoder; use FriendsOfTYPO3\Headless\Utility\HeadlessUserInt; -use Generator; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; -use RecursiveArrayIterator; -use RecursiveIteratorIterator; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject; use TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor; use function is_array; -use function strpos; class JsonContentObject extends AbstractContentObject implements LoggerAwareInterface { use LoggerAwareTrait; - private array $conf; - public function __construct( protected ContentDataProcessor $contentDataProcessor, protected JsonEncoder $jsonEncoder, @@ -41,25 +35,28 @@ public function __construct( /** * Rendering the cObject, JSON + * * @param array $conf Array of TypoScript properties - * @return string The HTML output + * @return string JSON-encoded content */ public function render($conf = []): string { + if (!is_array($conf)) { + $conf = []; + } + if (!empty($conf['if.']) && !$this->cObj->checkIf($conf['if.'])) { return ''; } - $data = []; - - if (!is_array($conf)) { - $conf = []; - } + $nullableFieldsIfEmpty = array_flip( + GeneralUtility::trimExplode(',', $conf['nullableFieldsIfEmpty'] ?? '', true) + ); - $this->conf = $conf; + $data = []; if (isset($conf['fields.'])) { - $data = $this->cObjGet($conf['fields.']); + $data = $this->cObjGet($conf['fields.'], '', $nullableFieldsIfEmpty); } if (isset($conf['dataProcessing.'])) { $data = $this->processFieldWithDataProcessing($conf); @@ -79,80 +76,71 @@ public function render($conf = []): string } /** - * Rendering of a "string array" of cObjects from TypoScript - * Will call ->cObjGetSingle() for each cObject found and accumulate the output. - * - * @param array $setup array with cObjects as values. - * @param string $addKey A prefix for the debugging information - * @return array Rendered output from the cObjects in the array. - * @see cObjGetSingle() + * @param array $setup + * @param string $addKey + * @param array $nullableFieldsIfEmpty + * @return array */ - public function cObjGet(array $setup, string $addKey = ''): array + public function cObjGet(array $setup, string $addKey = '', array $nullableFieldsIfEmpty = []): array { $content = []; - $nullableFieldsIfEmpty = GeneralUtility::trimExplode(',', $this->conf['nullableFieldsIfEmpty'] ?? '', true); - $sKeyArray = $this->filterByStringKeys($setup); - foreach ($sKeyArray as $theKey) { - $theValue = $setup[$theKey]; - if ((string)$theKey && !str_contains($theKey, '.')) { + foreach ($setup as $theKey => $theValue) { + if (!is_string($theKey) || $theKey === '') { + continue; + } + + if (!str_contains($theKey, '.')) { $conf = $setup[$theKey . '.'] ?? []; - $contentDataProcessing['dataProcessing.'] = $conf['dataProcessing.'] ?? []; $content[$theKey] = $this->cObj->cObjGetSingle($theValue, $conf, $addKey . $theKey); - if ((isset($conf['intval']) && $conf['intval']) || $theValue === 'INT') { + + if (!empty($conf['intval']) || $theValue === 'INT') { $content[$theKey] = (int)$content[$theKey]; - } - if ((isset($conf['floatval']) && $conf['floatval']) || $theValue === 'FLOAT') { + } elseif (!empty($conf['floatval']) || $theValue === 'FLOAT') { $content[$theKey] = (float)$content[$theKey]; - } - if ((isset($conf['boolval']) && $conf['boolval']) || $theValue === 'BOOL') { + } elseif (!empty($conf['boolval']) || $theValue === 'BOOL') { $content[$theKey] = (bool)(int)$content[$theKey]; } - if ($theValue === 'USER_INT' || str_starts_with((string)$content[$theKey], '