TYPO3 CMS  TYPO3_8-7
FlexFormService.php
Go to the documentation of this file.
1 <?php
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 
21 {
34  public function convertFlexFormContentToArray($flexFormContent, $languagePointer = 'lDEF', $valuePointer = 'vDEF')
35  {
36  $settings = [];
37  $flexFormArray = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($flexFormContent);
38  $flexFormArray = isset($flexFormArray['data']) ? $flexFormArray['data'] : [];
39  foreach (array_values($flexFormArray) as $languages) {
40  if (!is_array($languages[$languagePointer])) {
41  continue;
42  }
43  foreach ($languages[$languagePointer] as $valueKey => $valueDefinition) {
44  if (strpos($valueKey, '.') === false) {
45  $settings[$valueKey] = $this->walkFlexFormNode($valueDefinition, $valuePointer);
46  } else {
47  $valueKeyParts = explode('.', $valueKey);
48  $currentNode = &$settings;
49  foreach ($valueKeyParts as $valueKeyPart) {
50  $currentNode = &$currentNode[$valueKeyPart];
51  }
52  if (is_array($valueDefinition)) {
53  if (array_key_exists($valuePointer, $valueDefinition)) {
54  $currentNode = $valueDefinition[$valuePointer];
55  } else {
56  $currentNode = $this->walkFlexFormNode($valueDefinition, $valuePointer);
57  }
58  } else {
59  $currentNode = $valueDefinition;
60  }
61  }
62  }
63  }
64  return $settings;
65  }
66 
74  public function walkFlexFormNode($nodeArray, $valuePointer = 'vDEF')
75  {
76  if (is_array($nodeArray)) {
77  $return = [];
78  foreach ($nodeArray as $nodeKey => $nodeValue) {
79  if ($nodeKey === $valuePointer) {
80  return $nodeValue;
81  }
82  if (in_array($nodeKey, ['el', '_arrayContainer'])) {
83  return $this->walkFlexFormNode($nodeValue, $valuePointer);
84  }
85  if (($nodeKey[0] ?? '') === '_') {
86  continue;
87  }
88  if (strpos($nodeKey, '.')) {
89  $nodeKeyParts = explode('.', $nodeKey);
90  $currentNode = &$return;
91  $nodeKeyPartsCount = count($nodeKeyParts);
92  for ($i = 0; $i < $nodeKeyPartsCount - 1; $i++) {
93  $currentNode = &$currentNode[$nodeKeyParts[$i]];
94  }
95  $newNode = [next($nodeKeyParts) => $nodeValue];
96  $subVal = $this->walkFlexFormNode($newNode, $valuePointer);
97  $currentNode[key($subVal)] = current($subVal);
98  } elseif (is_array($nodeValue)) {
99  if (array_key_exists($valuePointer, $nodeValue)) {
100  $return[$nodeKey] = $nodeValue[$valuePointer];
101  } else {
102  $return[$nodeKey] = $this->walkFlexFormNode($nodeValue, $valuePointer);
103  }
104  } else {
105  $return[$nodeKey] = $nodeValue;
106  }
107  }
108  return $return;
109  }
110  return $nodeArray;
111  }
112 }
walkFlexFormNode($nodeArray, $valuePointer='vDEF')
convertFlexFormContentToArray($flexFormContent, $languagePointer='lDEF', $valuePointer='vDEF')
static xml2array($string, $NSprefix='', $reportDocTag=false)