Completed
Push — master ( 006012...05b9c2 )
by Tobias
05:02
created

XliffDumper::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 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\Dumper;
13
14
use Nyholm\NSA;
15
use Symfony\Component\Translation\Dumper\XliffFileDumper;
16
use Symfony\Component\Translation\MessageCatalogue;
17
use Symfony\Component\Translation\Exception\InvalidArgumentException;
18
use Translation\SymfonyStorage\Dumper\Port\SymfonyPort;
19
20
/**
21
 * XliffFileDumper generates xliff files from a message catalogue.
22
 *
23
 * This class provides support for both SF2.7 and SF3.0+
24
 *
25
 * @author Tobias Nyholm <[email protected]>
26
 * @author Michel Salib <[email protected]>
27
 */
28
final class XliffDumper extends XliffFileDumper
29
{
30
    /**
31
     * @var SymfonyPort|null
32
     */
33
    private $sfPort;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3
    public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
39
    {
40 3
        $xliffVersion = '1.2';
41 3
        if (array_key_exists('xliff_version', $options)) {
42 3
            $xliffVersion = $options['xliff_version'];
43 3
        }
44
45 3
        if (array_key_exists('default_locale', $options)) {
46
            $defaultLocale = $options['default_locale'];
47
        } else {
48 3
            $defaultLocale = \Locale::getDefault();
49
        }
50
51 3
        if ('1.2' === $xliffVersion) {
52 1
            if (method_exists($this, 'dumpXliff1')) {
53 1
                return NSA::invokeMethod($this, 'dumpXliff1', $defaultLocale, $messages, $domain, $options);
54
            } else {
55
                // Symfony 2.7
56
                return $this->format($messages, $domain);
57
            }
58
        }
59
60 2
        if ('2.0' === $xliffVersion) {
61 2
            if (null === $this->sfPort) {
62 2
                $this->sfPort = new SymfonyPort();
63 2
            }
64
65 2
            return $this->sfPort->dumpXliff2($defaultLocale, $messages, $domain, $options);
66
        }
67
68
        throw new InvalidArgumentException(
69
            sprintf('No support implemented for dumping XLIFF version "%s".', $xliffVersion)
70 1
        );
71
    }
72
73
    /**
74
     * To support Symfony 2.7.
75
     */
76
    protected function format(MessageCatalogue $messages, $domain)
77
    {
78
        return $this->formatCatalogue($messages, $domain, ['xliff_version' => '2.0']);
79
    }
80
}
81