Completed
Pull Request — master (#219)
by David
01:14
created

Xliff::getUnitID()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Gettext\Generators;
4
5
use Gettext\Translations;
6
use DOMDocument;
7
8
class Xliff extends Generator implements GeneratorInterface
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    public static function toString(Translations $translations, array $options = [])
14
    {
15
        $dom = new DOMDocument('1.0', 'utf-8');
16
        $dom->formatOutput = true;
17
        $xliff = $dom->appendChild($dom->createElement('xliff'));
18
        $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0');
19
        $xliff->setAttribute('version', '2.0');
20
        $xliff->setAttribute('srcLang', $translations->getLanguage());
21
        $xliff->setAttribute('trgLang', $translations->getLanguage());
22
        $file = $xliff->appendChild($dom->createElement('file'));
23
        $file->setAttribute('id', $translations->getDomain().'.'.$translations->getLanguage());
24
25
        //Save headers as notes
26
        $notes = $dom->createElement('notes');
27
28
        foreach ($translations->getHeaders() as $name => $value) {
29
            $notes->appendChild(self::createTextNode($dom, 'note', $value))->setAttribute('id', $name);
30
        }
31
32
        if ($notes->hasChildNodes()) {
33
            $file->appendChild($notes);
34
        }
35
36
        foreach ($translations as $translation) {
37
            $unit = $dom->createElement('unit');
38
            $unit->setAttribute('id', md5($translation->getContext().$translation->getOriginal()));
39
40
            //Save comments as notes
41
            $notes = $dom->createElement('notes');
42
43
            $notes->appendChild(self::createTextNode($dom, 'note', $translation->getContext()))
44
                ->setAttribute('category', 'context');
45
46
            foreach ($translation->getComments() as $comment) {
47
                $notes->appendChild(self::createTextNode($dom, 'note', $comment))
48
                    ->setAttribute('category', 'comment');
49
            }
50
51
            foreach ($translation->getExtractedComments() as $comment) {
52
                $notes->appendChild(self::createTextNode($dom, 'note', $comment))
53
                    ->setAttribute('category', 'extracted-comment');
54
            }
55
56
            foreach ($translation->getFlags() as $flag) {
57
                $notes->appendChild(self::createTextNode($dom, 'note', $flag))
58
                    ->setAttribute('category', 'flag');
59
            }
60
61
            foreach ($translation->getReferences() as $reference) {
62
                $notes->appendChild(self::createTextNode($dom, 'note', $reference[0].':'.$reference[1]))
63
                    ->setAttribute('category', 'reference');
64
            }
65
66
            $unit->appendChild($notes);
67
68
            $segment = $unit->appendChild($dom->createElement('segment'));
69
            $segment->appendChild(self::createTextNode($dom, 'source', $translation->getOriginal()));
70
            $segment->appendChild(self::createTextNode($dom, 'target', $translation->getTranslation()));
71
72
            foreach ($translation->getPluralTranslations() as $plural) {
73
                if ($plural !== '') {
74
                    $segment->appendChild(self::createTextNode($dom, 'target', $plural));
75
                }
76
            }
77
78
            $file->appendChild($unit);
79
        }
80
81
        return $dom->saveXML();
82
    }
83
84
    private static function createTextNode(DOMDocument $dom, $name, $string)
85
    {
86
        $node = $dom->createElement($name);
87
        $text = (preg_match('/[&<>]/', $string) === 1)
88
             ? $dom->createCDATASection($string)
89
             : $dom->createTextNode($string);
90
        $node->appendChild($text);
91
92
        return $node;
93
    }
94
}
95