XmlWriter::addLabel()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.5866
cc 7
nc 6
nop 3
1
<?php
2
3
/**
4
 * XML writer.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Localization\Writer;
9
10
use HDNET\Autoloader\Utility\FileUtility;
11
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
14
/**
15
 * XML writer.
16
 */
17
class XmlWriter extends AbstractLocalizationWriter
18
{
19
    /**
20
     * Get the base file content.
21
     *
22
     * @param string $extensionKey
23
     *
24
     * @return string
25
     */
26
    public function getBaseFileContent($extensionKey)
27
    {
28
        return '<?xml version="1.0"?>
29
<T3locallang>
30
	<meta type="array">
31
	<type>database</type>
32
	<description>Language file is created via the autoloader for the ' . $extensionKey . ' extension on ' . date(DATE_COOKIE) . '</description>
33
	</meta>
34
	<data type="array">
35
		<languageKey index="default" type="array">
36
		</languageKey>
37
	</data>
38
</T3locallang>';
39
    }
40
41
    /**
42
     * Get the absolute file name.
43
     *
44
     * @param string $extensionKey
45
     *
46
     * @return string
47
     */
48
    public function getAbsoluteFilename($extensionKey)
49
    {
50
        return ExtensionManagementUtility::extPath($extensionKey, 'Resources/Private/Language/' . $this->getLanguageBaseName() . '.xml');
51
    }
52
53
    /**
54
     * Add the label to a XML file.
55
     *
56
     * @param string $extensionKey
57
     * @param string $key
58
     * @param string $default
59
     *
60
     * @return bool|void
61
     */
62
    public function addLabel($extensionKey, $key, $default)
63
    {
64
        // Exclude
65
        if (!mb_strlen($default)) {
66
            return;
67
        }
68
        if (!mb_strlen($key)) {
69
            return;
70
        }
71
        if (!mb_strlen($extensionKey)) {
72
            return;
73
        }
74
        if (GeneralUtility::isFirstPartOfStr($key, 'LLL:')) {
75
            return;
76
        }
77
        $absolutePath = $this->getAbsoluteFilename($extensionKey);
78
        $content = GeneralUtility::getUrl($absolutePath);
79
        if (false !== mb_strpos($content, ' index="' . $key . '"') || '' === trim($content)) {
80
            return;
81
        }
82
        $replace = '<languageKey index="default" type="array">' . LF . TAB . TAB . TAB . '<label index="' . $key . '">' . $this->wrapCdata($default) . '</label>';
83
        $content = str_replace('<languageKey index="default" type="array">', $replace, $content);
84
        FileUtility::writeFileAndCreateFolder($absolutePath, $content);
85
        $this->clearCache();
86
    }
87
}
88