Completed
Push — master ( 54a9d8...e3bbff )
by Tobias
03:08
created

SymfonyPort::hasMetadataArrayInfo()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 5
nop 2
crap 4
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\Port;
13
14
use Symfony\Component\Translation\MessageCatalogue;
15
16
/**
17
 * This code is moved from the Symfony 3.4 repo. It will be removed when
18
 * we drop support for Symfony 3.3 and below.
19
 *
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
class SymfonyPort
23
{
24 2
    public function dumpXliff2($defaultLocale, MessageCatalogue $messages, $domain, array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26 2
        $dom = new \DOMDocument('1.0', 'utf-8');
27 2
        $dom->formatOutput = true;
28
29 2
        $xliff = $dom->appendChild($dom->createElement('xliff'));
30 2
        $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
31 2
        $xliff->setAttribute('version', '2.0');
32 2
        $xliff->setAttribute('srcLang', str_replace('_', '-', $defaultLocale));
33 2
        $xliff->setAttribute('trgLang', str_replace('_', '-', $messages->getLocale()));
34
35 2
        $xliffFile = $xliff->appendChild($dom->createElement('file'));
36 2
        $xliffFile->setAttribute('id', $domain.'.'.$messages->getLocale());
37
38 2
        foreach ($messages->all($domain) as $source => $target) {
39 2
            $translation = $dom->createElement('unit');
40 2
            $translation->setAttribute('id', strtr(substr(base64_encode(hash('sha256', $source, true)), 0, 7), '/+', '._'));
41 2
            $name = $source;
42 2
            if (strlen($source) > 80) {
43
                $name = substr(md5($source), -7);
44
            }
45 2
            $translation->setAttribute('name', $name);
46 2
            $metadata = $messages->getMetadata($source, $domain);
47
48
            // Add notes section
49 2
            if ($this->hasMetadataArrayInfo('notes', $metadata)) {
50 1
                $notesElement = $dom->createElement('notes');
51 1
                foreach ($metadata['notes'] as $note) {
52 1
                    $n = $dom->createElement('note');
53 1
                    $n->appendChild($dom->createTextNode(isset($note['content']) ? $note['content'] : ''));
54 1
                    unset($note['content']);
55
56 1
                    foreach ($note as $name => $value) {
57 1
                        $n->setAttribute($name, $value);
58
                    }
59 1
                    $notesElement->appendChild($n);
60
                }
61 1
                $translation->appendChild($notesElement);
62
            }
63
64 2
            $segment = $translation->appendChild($dom->createElement('segment'));
65
66 2
            $s = $segment->appendChild($dom->createElement('source'));
67 2
            $s->appendChild($dom->createTextNode($source));
68
69
            // Does the target contain characters requiring a CDATA section?
70 2
            $text = 1 === preg_match('/[&<>]/', $target) ? $dom->createCDATASection($target) : $dom->createTextNode(
71 2
                $target
72
            );
73
74 2
            $targetElement = $dom->createElement('target');
75 2
            if ($this->hasMetadataArrayInfo('target-attributes', $metadata)) {
76
                foreach ($metadata['target-attributes'] as $name => $value) {
77
                    $targetElement->setAttribute($name, $value);
78
                }
79
            }
80
81 2
            $t = $segment->appendChild($targetElement);
82 2
            $t->appendChild($text);
83
84 2
            $xliffFile->appendChild($translation);
85
        }
86
87 2
        return $dom->saveXML();
88
    }
89
90
    /**
91
     * @param string     $key
92
     * @param array|null $metadata
93
     *
94
     * @return bool
95
     */
96 2
    private function hasMetadataArrayInfo($key, $metadata = null)
97
    {
98 2
        return null !== $metadata &&
99 2
            array_key_exists($key, $metadata) &&
100 2
            ($metadata[$key] instanceof \Traversable || is_array($metadata[$key]));
101
    }
102
}
103