Skip to content
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
4 changes: 2 additions & 2 deletions Classes/ContentObject/BooleanContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class BooleanContentObject extends AbstractContentObject
{
/**
* Rendering the cObject, JSON
* @param array $conf Array of TypoScript properties
* @return bool
* @param array<string, mixed> $conf Array of TypoScript properties
*/
public function render($conf = []): bool
{
if (!is_array($conf)) {
return false;
}

$content = false;
if (isset($conf['value'])) {
$content = $conf['value'];
Expand Down
4 changes: 2 additions & 2 deletions Classes/ContentObject/IntegerContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class IntegerContentObject extends AbstractContentObject
{
/**
* Rendering the cObject, JSON
* @param array $conf Array of TypoScript properties
* @return int
* @param array<string, mixed> $conf Array of TypoScript properties
*/
public function render($conf = []): int
{
if (!is_array($conf)) {
return 0;
}

$content = 0;
if (isset($conf['value'])) {
$content = $conf['value'];
Expand Down
29 changes: 24 additions & 5 deletions Classes/ContentObject/JsonContentContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function render($conf = []): string
/**
* @param array<string, mixed> $contentElements
* @param array<string, mixed> $conf
* @return array<string,<array<int, mixed>>
* @return array<string, array<int, mixed>>
*/
protected function groupContentElementsByColPos(array $contentElements, array $conf): array
{
Expand All @@ -168,11 +168,12 @@ protected function groupContentElementsByColPos(array $contentElements, array $c
$element = $this->headlessUserInt->wrap($element);
}

$element = json_decode($element, true);
$decoded = json_decode($element, true);

if ($element === []) {
if (!is_array($decoded) || $decoded === []) {
continue;
}
$element = $decoded;

$colPos = $this->getColPosFromElement($groupingEnabled, $element);

Expand All @@ -184,11 +185,14 @@ protected function groupContentElementsByColPos(array $contentElements, array $c
}

if ($groupingEnabled && $this->isSortByBackendLayoutEnabled($conf)) {
$backendLayout = $this->backendLayoutView->getSelectedBackendLayout($this->request->getAttribute('routing')->getPageId());
$routing = $this->request->getAttribute('routing');
$backendLayout = $routing !== null
? $this->backendLayoutView->getSelectedBackendLayout($routing->getPageId())
: null;

$sorted = [];
foreach ($backendLayout['__colPosList'] ?? [] as $value) {
$sorted['colPos' . $value] = $data['colPos' . $value];
$sorted['colPos' . $value] = $data['colPos' . $value] ?? [];
}

$data = $sorted;
Expand All @@ -199,6 +203,9 @@ protected function groupContentElementsByColPos(array $contentElements, array $c
return $data;
}

/**
* @var array<string, int>
*/
private array $recordRegister = [];

/**
Expand Down Expand Up @@ -315,21 +322,33 @@ private function prepareValue(array $conf): array
return $theValue;
}

/**
* @param array<string, mixed> $conf
*/
private function isSortByBackendLayoutEnabled(array $conf): bool
{
return isset($conf['sortByBackendLayout']) && (int)$conf['sortByBackendLayout'] === 1;
}

/**
* @param array<string, mixed> $conf
*/
private function isColPolsGroupingEnabled(array $conf): bool
{
return !isset($conf['doNotGroupByColPos']) || (int)$conf['doNotGroupByColPos'] === 0;
}

/**
* @param array<string, mixed> $conf
*/
private function returnSingleRowEnabled(array $conf): bool
{
return isset($conf['returnSingleRow']) && (int)$conf['returnSingleRow'] === 1;
}

/**
* @param array<string, mixed> $element
*/
private function getColPosFromElement(bool $groupingEnabled, array $element): int
{
if ($groupingEnabled && !array_key_exists('colPos', $element)) {
Expand Down
29 changes: 16 additions & 13 deletions Classes/ContentObject/JsonContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace FriendsOfTYPO3\Headless\ContentObject;

use FriendsOfTYPO3\Headless\Json\JsonDecoderInterface;
use FriendsOfTYPO3\Headless\Json\JsonEncoder;
use FriendsOfTYPO3\Headless\Json\JsonEncoderInterface;
use FriendsOfTYPO3\Headless\Utility\HeadlessUserInt;
use Generator;
use Psr\Log\LoggerAwareInterface;
Expand All @@ -30,32 +30,35 @@ class JsonContentObject extends AbstractContentObject implements LoggerAwareInte
{
use LoggerAwareTrait;

private array $conf;
/**
* @var array<string, mixed>
*/
private array $conf = [];

public function __construct(
protected ContentDataProcessor $contentDataProcessor,
protected JsonEncoder $jsonEncoder,
protected JsonEncoderInterface $jsonEncoder,
protected JsonDecoderInterface $jsonDecoder,
protected HeadlessUserInt $headlessUserInt
) {}

/**
* Rendering the cObject, JSON
* @param array $conf Array of TypoScript properties
* @param array<string, mixed>|null $conf Array of TypoScript properties
* @return string The HTML output
*/
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 = [];
}

$this->conf = $conf;

if (isset($conf['fields.'])) {
Expand All @@ -82,9 +85,9 @@ 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 array<string, mixed> $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.
* @return array<string, mixed> Rendered output from the cObjects in the array.
* @see cObjGetSingle()
*/
public function cObjGet(array $setup, string $addKey = ''): array
Expand Down Expand Up @@ -139,9 +142,9 @@ public function cObjGet(array $setup, string $addKey = ''): array
/**
* Takes a TypoScript array as input and returns an array which contains all string properties found which had a value (not only properties).
*
* @param array $setupArr TypoScript array with string array in
* @param array<string, mixed> $setupArr TypoScript array with string array in
* @param bool $acceptAnyKeys If set, then a value is not required - the properties alone will be enough.
* @return array An array with all string properties.
* @return array<int, string> An array with all string properties.
*/
protected function filterByStringKeys(array $setupArr, bool $acceptAnyKeys = false): array
{
Expand All @@ -156,7 +159,7 @@ protected function filterByStringKeys(array $setupArr, bool $acceptAnyKeys = fal
}

/**
* @param array $dataProcessing
* @param array<string, mixed> $dataProcessing
*/
protected function processFieldWithDataProcessing(array $dataProcessing): mixed
{
Expand Down
11 changes: 5 additions & 6 deletions Classes/DataProcessing/DataProcessingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
trait DataProcessingTrait
{
/**
* @param array $processorConfiguration
* @param array $processedData
* @return array
* @param array<string, mixed> $processorConfiguration
* @param array<string, mixed> $processedData
* @return array<string, mixed>
*/
protected function removeDataIfnotAppendInConfiguration(array $processorConfiguration, array $processedData): array
{
Expand All @@ -42,14 +42,13 @@ protected function removeDataIfnotAppendInConfiguration(array $processorConfigur

protected function isMenuProcessor(): bool
{
return __CLASS__ === MenuProcessor::class;
return $this instanceof MenuProcessor;
}

/**
* Removes recursively "data" in children nodes
*
* @param array $children
* @param string $nodeName
* @param array<int|string, array<string, mixed>> $children
*/
private function removeDataInChildrenNodes(array &$children, string $nodeName = 'children'): void
{
Expand Down
39 changes: 22 additions & 17 deletions Classes/DataProcessing/DatabaseQueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

use function json_decode;

use const JSON_THROW_ON_ERROR;

/**
* Fetch records from the database, using the default .select syntax from TypoScript.
*
Expand Down Expand Up @@ -63,7 +65,10 @@ class DatabaseQueryProcessor implements DataProcessorInterface
public function __construct(protected ContentDataProcessor $contentDataProcessor, protected TypoScriptService $typoScriptService) {}

/**
* @inheritDoc
* @param array<string, mixed> $contentObjectConfiguration
* @param array<string, mixed> $processorConfiguration
* @param array<string, mixed> $processedData
* @return array<string, mixed>
*/
public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData): array
{
Expand Down Expand Up @@ -91,30 +96,30 @@ public function process(ContentObjectRenderer $cObj, array $contentObjectConfigu
$request = $cObj->getRequest();
$processedRecordVariables = [];

$flattenRow = null;
$objConf = [];
$objName = '< ' . $tableName;
if (isset($processorConfiguration['fields.'])) {
$objName = 'JSON';
$fields = $this->typoScriptService->convertTypoScriptArrayToPlainArray($processorConfiguration['fields.']);
$objConf = $this->typoScriptService->convertPlainArrayToTypoScriptArray(['fields' => $fields, '_typoScriptNodeValue' => 'JSON']);
}

$overrideJsonCE = null;
if (isset($processorConfiguration['overrideFields.'])) {
$overrideFields = $this->typoScriptService->convertTypoScriptArrayToPlainArray($processorConfiguration['overrideFields.']);
$overrideJsonCE = $this->typoScriptService->convertPlainArrayToTypoScriptArray(['fields' => $overrideFields, '_typoScriptNodeValue' => 'JSON']);
}

foreach ($records as $key => $record) {
$recordContentObjectRenderer = $this->createContentObjectRenderer();
$recordContentObjectRenderer->setRequest($request);
$recordContentObjectRenderer->start($record, $tableName);

$objConf = [];
$objName = '< ' . $tableName;

if (isset($processorConfiguration['fields.'])) {
$objName = 'JSON';
$fields = $this->typoScriptService->convertTypoScriptArrayToPlainArray($processorConfiguration['fields.']);
$objConf = $this->typoScriptService->convertPlainArrayToTypoScriptArray(['fields' => $fields, '_typoScriptNodeValue' => 'JSON']);
}

$processedRecordVariables[$key] = $objConf !== [] ? json_decode($recordContentObjectRenderer->cObjGetSingle($objName, $objConf), true) : $record;
$processedRecordVariables[$key] = $objConf !== [] ? json_decode($recordContentObjectRenderer->cObjGetSingle($objName, $objConf), true, 512, JSON_THROW_ON_ERROR) : $record;
$processedRecordVariables[$key] = $this->contentDataProcessor->process($recordContentObjectRenderer, $processorConfiguration, $processedRecordVariables[$key]);

if (isset($processorConfiguration['overrideFields.'])) {
$overrideFields = $this->typoScriptService->convertTypoScriptArrayToPlainArray($processorConfiguration['overrideFields.']);
$jsonCE = $this->typoScriptService->convertPlainArrayToTypoScriptArray(['fields' => $overrideFields, '_typoScriptNodeValue' => 'JSON']);
$record = json_decode($recordContentObjectRenderer->cObjGetSingle('JSON', $jsonCE), true);

if ($overrideJsonCE !== null) {
$record = json_decode($recordContentObjectRenderer->cObjGetSingle('JSON', $overrideJsonCE), true, 512, JSON_THROW_ON_ERROR);
foreach ($record as $fieldName => $overrideData) {
$processedRecordVariables[$key][$fieldName] = $overrideData;
}
Expand Down
24 changes: 14 additions & 10 deletions Classes/DataProcessing/ExtractPropertyProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ class ExtractPropertyProcessor implements DataProcessorInterface
* Extract a single (maybe nested) property from a given array
*
* @param ContentObjectRenderer $cObj The content object renderer, which contains data of the content element
* @param array $contentObjectConfiguration The configuration of Content Object
* @param array $processorConfiguration The configuration of this processor
* @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
* @return array the processed data as key/value store
* @param array<string, mixed> $contentObjectConfiguration The configuration of Content Object
* @param array<string, mixed> $processorConfiguration The configuration of this processor
* @param array<string, mixed> $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
* @return array<string, mixed> the processed data as key/value store
*/
public function process(
ContentObjectRenderer $cObj,
array $contentObjectConfiguration,
array $processorConfiguration,
array $processedData
) {
): array {
if (empty($processorConfiguration['as'])) {
throw new Exception('Please specify property \'as\'');
}
Expand All @@ -64,13 +64,17 @@ public function process(

$key = GeneralUtility::trimExplode('.', $processorConfiguration['key'], true);

// Extract (nested) property
do {
$processedData = $processedData[array_shift($key)] ?? null;
} while (count($key));
$value = $processedData;
foreach ($key as $segment) {
if (!is_array($value)) {
$value = null;
break;
}
$value = $value[$segment] ?? null;
}

return [
$targetFieldName => $processedData,
$targetFieldName => $value,
];
}
}
Loading
Loading