XliffWriter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseFileContent() 0 11 1
A getAbsoluteFilename() 0 4 1
B addLabel() 0 23 6
1
<?php
2
3
/**
4
 * Xliff 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
 * Xliff writer.
16
 */
17
class XliffWriter 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" encoding="utf-8" standalone="yes" ?>
29
<xliff version="1.0">
30
	<file source-language="en" datatype="plaintext" original="messages" date="' . date('c') . '" product-name="' . $extensionKey . '">
31
		<header/>
32
		<body>
33
		</body>
34
	</file>
35
</xliff>';
36
    }
37
38
    /**
39
     * Get the absolute file name.
40
     *
41
     * @param string $extensionKey
42
     *
43
     * @return string
44
     */
45
    public function getAbsoluteFilename($extensionKey)
46
    {
47
        return ExtensionManagementUtility::extPath($extensionKey, 'Resources/Private/Language/' . $this->getLanguageBaseName() . '.xlf');
48
    }
49
50
    /**
51
     * Add the Label to the local lang XLIFF.
52
     *
53
     * @param string $extensionKey
54
     * @param string $key
55
     * @param string $default
56
     *
57
     * @return bool|void
58
     */
59
    public function addLabel($extensionKey, $key, $default)
60
    {
61
        // Exclude
62
        if (!mb_strlen($default)) {
63
            return;
64
        }
65
        if (!mb_strlen($key)) {
66
            return;
67
        }
68
        if (!mb_strlen($extensionKey)) {
69
            return;
70
        }
71
72
        $absolutePath = $this->getAbsoluteFilename($extensionKey);
73
        $content = GeneralUtility::getUrl($absolutePath);
74
        if (false !== mb_strpos($content, ' id="' . $key . '"') || '' === trim($content)) {
75
            return;
76
        }
77
        $replace = '<body>' . LF . TAB . TAB . TAB . '<trans-unit id="' . $key . '"><source>' . $this->wrapCdata($default) . '</source></trans-unit>';
78
        $content = str_replace('<body>', $replace, $content);
79
        FileUtility::writeFileAndCreateFolder($absolutePath, $content);
80
        $this->clearCache();
81
    }
82
}
83