XliffConverter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 23
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A contentToCatalogue() 0 7 1
A catalogueToContent() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\SymfonyStorage;
13
14
use Symfony\Component\Translation\Dumper\XliffFileDumper;
15
use Symfony\Component\Translation\Loader\XliffFileLoader;
16
use Symfony\Component\Translation\MessageCatalogue;
17
18
/**
19
 * Utility class to convert between a MessageCatalogue and XLIFF file content.
20
 *
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
final class XliffConverter
24
{
25
    /**
26
     * Create a catalogue from the contents of a XLIFF file.
27
     */
28 1
    public static function contentToCatalogue(string $content, string $locale, string $domain): MessageCatalogue
29
    {
30 1
        $file = sys_get_temp_dir().'/'.uniqid('xliff', true);
31 1
        file_put_contents($file, $content);
32
33 1
        return (new XliffFileLoader())->load($file, $locale, $domain);
34
    }
35
36 1
    public static function catalogueToContent(MessageCatalogue $catalogue, string $domain, array $options = []): string
37
    {
38 1
        if (!\array_key_exists('xliff_version', $options)) {
39
            // Set default value for xliff version.
40 1
            $options['xliff_version'] = '2.0';
41
        }
42
43 1
        return (new XliffFileDumper())->formatCatalogue($catalogue, $domain, $options);
44
    }
45
}
46