TYPO3 CMS  TYPO3_7-6
TypoScriptToJsonConverter.php
Go to the documentation of this file.
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 
19 
26 {
28  'BUTTON',
29  'CHECKBOX',
30  'CHECKBOXGROUP',
31  'FIELDSET',
32  'FILEUPLOAD',
33  'HEADER',
34  'HIDDEN',
35  'IMAGEBUTTON',
36  'OPTGROUP',
37  'OPTION',
38  'PASSWORD',
39  'RADIO',
40  'RADIOGROUP',
41  'RESET',
42  'SELECT',
43  'SUBMIT',
44  'TEXTAREA',
45  'TEXTBLOCK',
46  'TEXTLINE'
47  ];
48 
52  protected $nameMapping = [
53  'checkboxgroup' => 'CheckboxGroup',
54  'radiogroup' => 'RadioGroup',
55  ];
56 
60  protected $validationRules;
61 
68  public function convert(array $typoscript)
69  {
70  $this->setValidationRules($typoscript);
71  $jsonObject = $this->createElement('form', $typoscript);
72  return $jsonObject;
73  }
74 
84  public function createElement($class, array $arguments = [])
85  {
86  $class = strtolower((string)$class);
87  if (!empty($this->nameMapping[$class])) {
88  $class = $this->nameMapping[$class];
89  }
90  $className = 'TYPO3\\CMS\\Form\\Domain\\Model\Json\\' . ucfirst($class) . 'JsonElement';
91  $this->addValidationRules($arguments);
92 
93  if (!class_exists($className)) {
94  throw new \RuntimeException('Class "' . $className . '" does not exist', 1440779351);
95  }
96 
98  $object = GeneralUtility::makeInstance($className);
99  $object->setParameters($arguments);
100  if ($object->childElementsAllowed()) {
101  $this->getChildElementsByIntegerKey($object, $arguments);
102  }
103  return $object;
104  }
105 
114  protected function getChildElementsByIntegerKey(AbstractJsonElement $parentElement, array $typoscript)
115  {
116  if (is_array($typoscript)) {
118  foreach ($keys as $key) {
119  $class = $typoscript[$key];
120  if ((int)$key && strpos($key, '.') === false) {
121  if (isset($typoscript[$key . '.'])) {
122  $elementArguments = $typoscript[$key . '.'];
123  } else {
124  $elementArguments = [];
125  }
126  $this->setElementType($parentElement, $class, $elementArguments);
127  }
128  }
129  }
130  }
131 
143  private function setElementType(AbstractJsonElement $parentElement, $class, array $arguments)
144  {
145  if (in_array($class, $this->registeredElementNames)) {
146  if (strstr($arguments['class'], 'predefined-name')) {
147  $class = 'NAME';
148  }
149  $this->addElement($parentElement, $class, $arguments);
150  }
151  }
152 
161  public function addElement(AbstractJsonElement $parentElement, $class, array $arguments)
162  {
163  try {
164  $element = $this->createElement($class, $arguments);
165  $parentElement->addElement($element);
166  } catch (\RuntimeException $exception) {
167  // Catch missing classes or element types
168  // There are elements that can be used the
169  // TypoScript-like declaration, which don't
170  // have a counterpart in the ExtJS wizard.
171  }
172  }
173 
180  protected function setValidationRules(array $typoscript)
181  {
182  if (isset($typoscript['rules.']) && is_array($typoscript['rules.'])) {
183  $this->validationRules = $typoscript['rules.'];
184  }
185  }
186 
197  protected function addValidationRules(array &$arguments)
198  {
199  if (!empty($this->validationRules) && isset($arguments['name'])) {
200  foreach ($this->validationRules as $key => $ruleName) {
201  if ((int)$key && strpos($key, '.') === false) {
202  if (isset($this->validationRules[$key . '.'])) {
203  $ruleConfiguration = $this->validationRules[$key . '.'];
204  if (isset($ruleConfiguration['element']) && $ruleConfiguration['element'] === $arguments['name']) {
205  $arguments['validation'][$ruleName] = $ruleConfiguration;
206  }
207  }
208  }
209  }
210  }
211  }
212 }
setElementType(AbstractJsonElement $parentElement, $class, array $arguments)
static sortedKeyList($setupArr, $acceptOnlyProperties=false)
getChildElementsByIntegerKey(AbstractJsonElement $parentElement, array $typoscript)
addElement(AbstractJsonElement $parentElement, $class, array $arguments)