Xliff   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
C fromString() 0 64 16
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
use Gettext\Translation;
7
use SimpleXMLElement;
8
9
/**
10
 * Class to get gettext strings from xliff format.
11
 */
12
class Xliff extends Extractor implements ExtractorInterface
13
{
14
15
    public static $options = [
16
        'unitid_as_id' => false
17
    ];
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public static function fromString($string, Translations $translations, array $options = [])
23
    {
24
        $options += static::$options;
25
26
        $xml = new SimpleXMLElement($string, null, false);
27
28
        foreach ($xml->file as $file) {
29
            if (isset($file->notes)) {
30
                foreach ($file->notes->note as $note) {
31
                    $translations->setHeader($note['id'], (string) $note);
32
                }
33
            }
34
35
            foreach ($file->unit as $unit) {
36
                foreach ($unit->segment as $segment) {
37
                    $targets = [];
38
39
                    foreach ($segment->target as $target) {
40
                        $targets[] = (string) $target;
41
                    }
42
43
                    $translation = $translations->createNewTranslation(null, (string) $segment->source);
44
                    if (isset($unit['id'])) {
45
                        $unitId = (string) $unit['id'];
46
                        $translation->addComment("XLIFF_UNIT_ID: $unitId");
47
                        if ($options['unitid_as_id']) {
48
                            $translation->setId($unitId);
49
                        }
50
                    }
51
                    $translation->setTranslation(array_shift($targets));
52
                    $translation->setPluralTranslations($targets);
53
54
                    if (isset($unit->notes)) {
55
                        foreach ($unit->notes->note as $note) {
56
                            switch ($note['category']) {
57
                                case 'context':
58
                                    $translation = $translation->getClone((string) $note);
59
                                    break;
60
61
                                case 'extracted-comment':
62
                                    $translation->addExtractedComment((string) $note);
63
                                    break;
64
65
                                case 'flag':
66
                                    $translation->addFlag((string) $note);
67
                                    break;
68
69
                                case 'reference':
70
                                    $ref = explode(':', (string) $note, 2);
71
                                    $translation->addReference($ref[0], isset($ref[1]) ? $ref[1] : null);
72
                                    break;
73
74
                                default:
75
                                    $translation->addComment((string) $note);
76
                                    break;
77
                            }
78
                        }
79
                    }
80
81
                    $translations[] = $translation;
82
                }
83
            }
84
        }
85
    }
86
}
87