Passed
Push — master ( 2fd788...dffb04 )
by
unknown
20:10
created

TypoScriptService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 20
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTypoScriptFrontendController() 0 3 1
A __construct() 0 3 1
A resolvePossibleTypoScriptConfiguration() 0 6 1
A resolveTypoScriptConfiguration() 0 17 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Form\Mvc\Configuration;
19
20
use TYPO3\CMS\Core\TypoScript\TypoScriptService as CoreTypoScriptService;
21
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
22
23
/**
24
 * Utilities to manage and convert TypoScript
25
 *
26
 * Scope: frontend
27
 */
28
class TypoScriptService
29
{
30
    protected CoreTypoScriptService $coreTypoScriptService;
31
32
    public function __construct(CoreTypoScriptService $coreTypoScriptService)
33
    {
34
        $this->coreTypoScriptService = $coreTypoScriptService;
35
    }
36
37
    /**
38
     * Parse a configuration with ContentObjectRenderer::cObjGetSingle()
39
     * and return the result.
40
     *
41
     * @param array $configuration
42
     * @return array
43
     * @internal
44
     */
45
    public function resolvePossibleTypoScriptConfiguration(array $configuration = []): array
46
    {
47
        $configuration = $this->coreTypoScriptService->convertPlainArrayToTypoScriptArray($configuration);
48
        $configuration = $this->resolveTypoScriptConfiguration($configuration);
49
        $configuration = $this->coreTypoScriptService->convertTypoScriptArrayToPlainArray($configuration);
50
        return $configuration;
51
    }
52
53
    /**
54
     * Parse a configuration with ContentObjectRenderer::cObjGetSingle()
55
     * if there is an array key without and with a dot at the end.
56
     * This sample would be identified as a TypoScript parsable configuration
57
     * part:
58
     *
59
     * [
60
     *   'example' => 'TEXT'
61
     *   'example.' => [
62
     *     'value' => 'some value'
63
     *   ]
64
     * ]
65
     *
66
     * @param array $configuration
67
     * @return array
68
     */
69
    protected function resolveTypoScriptConfiguration(array $configuration = []): array
70
    {
71
        foreach ($configuration as $key => $value) {
72
            $keyWithoutDot = rtrim((string)$key, '.');
73
            if (isset($configuration[$keyWithoutDot]) && isset($configuration[$keyWithoutDot . '.'])) {
74
                $value = $this->getTypoScriptFrontendController()->cObj->cObjGetSingle(
75
                    $configuration[$keyWithoutDot],
76
                    $configuration[$keyWithoutDot . '.'],
77
                    $keyWithoutDot
78
                );
79
                $configuration[$keyWithoutDot] = $value;
80
            } elseif (!isset($configuration[$keyWithoutDot]) && isset($configuration[$keyWithoutDot . '.'])) {
81
                $configuration[$keyWithoutDot] = $this->resolveTypoScriptConfiguration($value);
82
            }
83
            unset($configuration[$keyWithoutDot . '.']);
84
        }
85
        return $configuration;
86
    }
87
88
    /**
89
     * @return TypoScriptFrontendController
90
     */
91
    protected function getTypoScriptFrontendController()
92
    {
93
        return $GLOBALS['TSFE'];
94
    }
95
}
96