Completed
Push — master ( d902d2...5712dc )
by Oscar
02:01
created

Xliff   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 64
rs 10
wmc 14
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
C fromString() 0 58 14
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
     * {@inheritdoc}
16
     */
17
    public static function fromString($string, Translations $translations, array $options = [])
18
    {
19
        $xml = new SimpleXMLElement($string, null, false);
20
        
21
        foreach ($xml->file as $file) {
22
            if (isset($file->notes)) {
23
                foreach ($file->notes->note as $note) {
24
                    $translations->setHeader($note['id'], (string) $note);
25
                }
26
            }
27
28
            foreach ($file->unit as $unit) {
29
                foreach ($unit->segment as $segment) {
30
                    $targets = [];
31
32
                    foreach ($segment->target as $target) {
33
                        $targets[] = (string) $target;
34
                    }
35
36
37
                    $translation = new Translation(null, (string) $segment->source);
38
                    $translation->setTranslation(array_shift($targets));
39
                    $translation->setPluralTranslations($targets);
40
41
                    if (isset($unit->notes)) {
42
                        foreach ($unit->notes->note as $note) {
43
                            switch ($note['category']) {
44
                                case 'context':
45
                                    $translation = $translation->getClone((string) $note);
46
                                    break;
47
48
                                case 'extracted-comment':
49
                                    $translation->addExtractedComment((string) $note);
50
                                    break;
51
52
                                case 'flag':
53
                                    $translation->addFlag((string) $note);
54
                                    break;
55
56
                                case 'reference':
57
                                    $ref = explode(':', (string) $note, 2);
58
                                    $translation->addReference($ref[0], isset($ref[1]) ? $ref[1] : null);
59
                                    break;
60
                                
61
                                default:
62
                                    $translation->addComment((string) $note);
63
                                    break;
64
                            }
65
                        }
66
                    }
67
68
                    $translations[] = $translation;
69
                }
70
            }
71
        }
72
73
        return $translations;
74
    }
75
}
76