[CLEANUP] Adjust code to coding guidelines
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Service / TypoScriptService.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009 Christian Müller <christian@kitsunet.de>
8 * (c) 2011 Bastian Waidelich <bastian@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Utilities to manage and convert TypoScript
29 */
30 class TypoScriptService implements \TYPO3\CMS\Core\SingletonInterface {
31
32 /**
33 * Removes all trailing dots recursively from TS settings array
34 *
35 * Extbase converts the "classical" TypoScript (with trailing dot) to a format without trailing dot,
36 * to be more future-proof and not to have any conflicts with Fluid object accessor syntax.
37 *
38 * @param array $typoScriptArray The TypoScript array (e.g. array('foo' => 'TEXT', 'foo.' => array('bar' => 'baz')))
39 * @return array
40 * @api
41 */
42 public function convertTypoScriptArrayToPlainArray(array $typoScriptArray) {
43 foreach ($typoScriptArray as $key => &$value) {
44 if (substr($key, -1) === '.') {
45 $keyWithoutDot = substr($key, 0, -1);
46 $hasNodeWithoutDot = array_key_exists($keyWithoutDot, $typoScriptArray);
47 $typoScriptNodeValue = $hasNodeWithoutDot ? $typoScriptArray[$keyWithoutDot] : NULL;
48 if (is_array($value)) {
49 $typoScriptArray[$keyWithoutDot] = $this->convertTypoScriptArrayToPlainArray($value);
50 if (!is_null($typoScriptNodeValue)) {
51 $typoScriptArray[$keyWithoutDot]['_typoScriptNodeValue'] = $typoScriptNodeValue;
52 }
53 unset($typoScriptArray[$key]);
54 } else {
55 $typoScriptArray[$keyWithoutDot] = NULL;
56 }
57 }
58 }
59 return $typoScriptArray;
60 }
61
62 /**
63 * Returns an array with Typoscript the old way (with dot).
64 *
65 * Extbase converts the "classical" TypoScript (with trailing dot) to a format without trailing dot,
66 * to be more future-proof and not to have any conflicts with Fluid object accessor syntax.
67 * However, if you want to call legacy TypoScript objects, you somehow need the "old" syntax (because this is what TYPO3 is used to).
68 * With this method, you can convert the extbase TypoScript to classical TYPO3 TypoScript which is understood by the rest of TYPO3.
69 *
70 * @param array $plainArray An TypoScript Array with Extbase Syntax (without dot but with _typoScriptNodeValue)
71 * @return array array with TypoScript as usual (with dot)
72 * @api
73 */
74 public function convertPlainArrayToTypoScriptArray(array $plainArray) {
75 $typoScriptArray = array();
76 foreach ($plainArray as $key => $value) {
77 if (is_array($value)) {
78 if (isset($value['_typoScriptNodeValue'])) {
79 $typoScriptArray[$key] = $value['_typoScriptNodeValue'];
80 unset($value['_typoScriptNodeValue']);
81 }
82 $typoScriptArray[$key . '.'] = $this->convertPlainArrayToTypoScriptArray($value);
83 } else {
84 $typoScriptArray[$key] = $value;
85 }
86 }
87 return $typoScriptArray;
88 }
89 }
90
91 ?>