[TASK] Functional test for tt_content rendering
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Framework / Frontend / Renderer.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\Framework\Frontend;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Section renderer for frontend responses.
19 */
20 class Renderer implements \TYPO3\CMS\Core\SingletonInterface
21 {
22 /**
23 * @var array
24 */
25 protected $sections = [];
26
27 /**
28 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
29 */
30 public $cObj;
31
32 /**
33 * @param string $content
34 * @param NULL|array $configuration
35 * @return void
36 */
37 public function parseValues($content, array $configuration = null)
38 {
39 if (empty($content)) {
40 return;
41 }
42
43 $values = json_decode($content, true);
44
45 if (empty($values) || !is_array($values)) {
46 return;
47 }
48
49 $asPrefix = (!empty($configuration['as']) ? $configuration['as'] . ':' : null);
50 foreach ($values as $identifier => $structure) {
51 $parser = $this->createParser();
52 $parser->parse($structure);
53
54 $section = [
55 'structure' => $structure,
56 'structurePaths' => $parser->getPaths(),
57 'records' => $parser->getRecords(),
58 ];
59
60 $this->addSection($section, $asPrefix . $identifier);
61 }
62 }
63
64 /**
65 * Possible structure of $configuration:
66 * {
67 * values {
68 * propertyA.data = tsfe:id
69 * propertyB.children {
70 * propertyB1.data = page:id
71 * propertyB2.data = page:pid
72 * propertyB2.intval = 1
73 * }
74 * }
75 * as = CustomData
76 * }
77 *
78 * @param string $content
79 * @param array|null $configuration
80 */
81 public function renderValues($content, array $configuration = null)
82 {
83 if (empty($configuration['values.'])) {
84 return;
85 }
86
87 $as = (!empty($configuration['as']) ? $configuration['as'] : null);
88 $this->addSection($this->stdWrapValues($configuration['values.']), $as);
89 }
90
91 /**
92 * @param array $section
93 * @param NULL|string $as
94 */
95 public function addSection(array $section, $as = null)
96 {
97 if (!empty($as)) {
98 $this->sections[$as] = $section;
99 } else {
100 $this->sections[] = $section;
101 }
102 }
103
104 /**
105 * @param string $content
106 * @param NULL|array $configuration
107 * @return string
108 */
109 public function renderSections($content, array $configuration = null)
110 {
111 $content = json_encode($this->sections);
112 return $content;
113 }
114
115 /**
116 * Possible structure of $values:
117 * {
118 * propertyA.data = tsfe:id
119 * propertyB.children {
120 * propertyB1.data = page:id
121 * propertyB2.data = page:pid
122 * propertyB2.intval = 1
123 * }
124 * }
125 *
126 * @param array $values
127 * @return array
128 */
129 protected function stdWrapValues(array $values)
130 {
131 $renderedValues = [];
132
133 foreach ($values as $propertyName => $propertyInstruction)
134 {
135 $plainPropertyName = rtrim($propertyName, '.');
136 if (!empty($propertyInstruction['children.'])) {
137 $renderedValues[$plainPropertyName] = $this->stdWrapValues(
138 $propertyInstruction['children.']
139 );
140 } else {
141 $renderedValues[$plainPropertyName] = $this->cObj->stdWrap(
142 '',
143 $propertyInstruction
144 );
145 }
146 }
147
148 return $renderedValues;
149 }
150
151 /**
152 * @return Parser
153 */
154 protected function createParser()
155 {
156 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
157 \TYPO3\CMS\Core\Tests\Functional\Framework\Frontend\Parser::class
158 );
159 }
160 }