TYPO3 CMS  TYPO3_6-2
TypoScriptToJsonConverter.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Form\Utility;
3 
25 
29  protected $validationRules;
30 
37  public function convert(array $typoscript) {
38  $this->setValidationRules($typoscript);
39  $jsonObject = $this->createElement('form', $typoscript);
40  return $jsonObject;
41  }
42 
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 
55  if (!class_exists($className)) {
56  throw new \RuntimeException('Class "' . $className . '" does not exist', 1440779351);
57  }
58 
59  $this->addValidationRules($arguments);
62  $object->setParameters($arguments);
63  if ($object->childElementsAllowed()) {
64  $this->getChildElementsByIntegerKey($object, $arguments);
65  }
66  return $object;
67  }
68 
77  protected function getChildElementsByIntegerKey(\TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement, array $typoscript) {
78  if (is_array($typoscript)) {
80  foreach ($keys as $key) {
81  $class = $typoscript[$key];
82  if ((int)$key && strpos($key, '.') === FALSE) {
83  if (isset($typoscript[$key . '.'])) {
84  $elementArguments = $typoscript[$key . '.'];
85  } else {
86  $elementArguments = array();
87  }
88  $this->setElementType($parentElement, $class, $elementArguments);
89  }
90  }
91  }
92  }
93 
105  private function setElementType(\TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement, $class, array $arguments) {
106  if (in_array($class, \TYPO3\CMS\Form\Utility\FormUtility::getInstance()->getFormObjects())) {
107  if (strstr($arguments['class'], 'predefined-name')) {
108  $class = 'NAME';
109  }
110  $this->addElement($parentElement, $class, $arguments);
111  }
112  }
113 
122  public function addElement(\TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement, $class, array $arguments) {
123  try {
124  $element = $this->createElement($class, $arguments);
125  $parentElement->addElement($element);
126  } catch (\RuntimeException $exception) {
127  // Catch missing classes or element types
128  // There are elements that can be used the
129  // TypoScript-like declaration, which don't
130  // have a counterpart in the ExtJS wizard.
131  }
132  }
133 
140  protected function setValidationRules(array $typoscript) {
141  if (isset($typoscript['rules.']) && is_array($typoscript['rules.'])) {
142  $this->validationRules = $typoscript['rules.'];
143  }
144  }
145 
156  protected function addValidationRules(array &$arguments) {
157  $validationRulesAvailable = FALSE;
158  if (!empty($this->validationRules) && isset($arguments['name'])) {
159  foreach ($this->validationRules as $key => $ruleName) {
160  if ((int)$key && strpos($key, '.') === FALSE) {
161  if (isset($this->validationRules[$key . '.'])) {
162  $ruleConfiguration = $this->validationRules[$key . '.'];
163  if (isset($ruleConfiguration['element']) && $ruleConfiguration['element'] === $arguments['name']) {
164  $arguments['validation'][$ruleName] = $ruleConfiguration;
165  }
166  }
167  }
168  }
169  }
170  }
171 
172 }
addElement(\TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement, $class, array $arguments)
setElementType(\TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement, $class, array $arguments)
getChildElementsByIntegerKey(\TYPO3\CMS\Form\Domain\Model\Json\AbstractJsonElement $parentElement, array $typoscript)
static sortedKeyList($setupArr, $acceptOnlyProperties=FALSE)