[TASK] Use "bool" in @param notation
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Utility / TypoScriptToJsonConverter.php
1 <?php
2 namespace TYPO3\CMS\Form\Utility;
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 * Typoscript to JSON converter
19 *
20 * Takes the incoming Typoscript and converts it to Json
21 *
22 * @author Patrick Broens <patrick@patrickbroens.nl>
23 */
24 class TypoScriptToJsonConverter {
25
26 /**
27 * @var array
28 */
29 protected $validationRules;
30
31 /**
32 * Convert TypoScript string to JSON
33 *
34 * @param string $typoscript TypoScript string containing all configuration for the form
35 * @return string The JSON for the form
36 */
37 public function convert(array $typoscript) {
38 $this->setValidationRules($typoscript);
39 $jsonObject = $this->createElement('form', $typoscript);
40 return $jsonObject;
41 }
42
43 /**
44 * Create element by loading class
45 * and instantiating the object
46 *
47 * @param string $class Type of element
48 * @param array $arguments Configuration array
49 * @return \TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement
50 */
51 public function createElement($class, array $arguments = array()) {
52 $class = strtolower((string) $class);
53 $className = 'TYPO3\\CMS\\Form\\Domain\\Model\Json\\' . ucfirst($class) . 'JsonElement';
54 $this->addValidationRules($arguments);
55 /** @var $object \TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement */
56 $object = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className);
57 $object->setParameters($arguments);
58 if ($object->childElementsAllowed()) {
59 $this->getChildElementsByIntegerKey($object, $arguments);
60 }
61 return $object;
62 }
63
64 /**
65 * Rendering of a "numerical array" of Form objects from TypoScript
66 * Creates new object for each element found
67 *
68 * @param \TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement Parent model object
69 * @param array $arguments Configuration array
70 * @return void
71 */
72 protected function getChildElementsByIntegerKey(\TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement, array $typoscript) {
73 if (is_array($typoscript)) {
74 $keys = \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList($typoscript);
75 foreach ($keys as $key) {
76 $class = $typoscript[$key];
77 if ((int)$key && strpos($key, '.') === FALSE) {
78 if (isset($typoscript[$key . '.'])) {
79 $elementArguments = $typoscript[$key . '.'];
80 } else {
81 $elementArguments = array();
82 }
83 $this->setElementType($parentElement, $class, $elementArguments);
84 }
85 }
86 }
87 }
88
89 /**
90 * Set the element type of the object
91 *
92 * Checks if the typoscript object is part of the FORM or has a predefined
93 * class for name or header object
94 *
95 * @param \TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement The parent object
96 * @param string $class A predefined class
97 * @param array $arguments Configuration array
98 * @return void
99 */
100 private function setElementType(\TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement, $class, array $arguments) {
101 if (in_array($class, \TYPO3\CMS\Form\Utility\FormUtility::getInstance()->getFormObjects())) {
102 if (strstr($arguments['class'], 'predefined-name')) {
103 $class = 'NAME';
104 }
105 $this->addElement($parentElement, $class, $arguments);
106 }
107 }
108
109 /**
110 * Add child object to this element
111 *
112 * @param \TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement The parent object
113 * @param string $class Type of element
114 * @param array $arguments Configuration array
115 * @return void
116 */
117 public function addElement(\TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement, $class, array $arguments) {
118 $element = $this->createElement($class, $arguments);
119 $parentElement->addElement($element);
120 }
121
122 /**
123 * Set the validation rules
124 *
125 * @param array $typoscript Configuration array
126 * @return void
127 */
128 protected function setValidationRules(array $typoscript) {
129 if (isset($typoscript['rules.']) && is_array($typoscript['rules.'])) {
130 $this->validationRules = $typoscript['rules.'];
131 }
132 }
133
134 /**
135 * Add validation rules to an element if available
136 *
137 * In TypoScript the validation rules belong to the form and are connected
138 * to the elements by name. However, in the wizard, they are added to the
139 * element for usability
140 *
141 * @param array $arguments The element arguments
142 * @return void
143 */
144 protected function addValidationRules(array &$arguments) {
145 $validationRulesAvailable = FALSE;
146 if (!empty($this->validationRules) && isset($arguments['name'])) {
147 foreach ($this->validationRules as $key => $ruleName) {
148 if ((int)$key && strpos($key, '.') === FALSE) {
149 if (isset($this->validationRules[$key . '.'])) {
150 $ruleConfiguration = $this->validationRules[$key . '.'];
151 if (isset($ruleConfiguration['element']) && $ruleConfiguration['element'] === $arguments['name']) {
152 $arguments['validation'][$ruleName] = $ruleConfiguration;
153 }
154 }
155 }
156 }
157 }
158 }
159
160 }