Completed
Push — master ( 55caa0...3f2e9b )
by Oscar
02:05
created

Po::fixMultiLines()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 3
b 0
f 0
nc 3
nop 3
dl 0
loc 16
rs 8.8571
1
<?php
2
3
namespace Gettext\Extractors;
4
5
use Gettext\Translations;
6
use Gettext\Translation;
7
use Gettext\Utils\HeadersExtractorTrait;
8
9
/**
10
 * Class to get gettext strings from php files returning arrays.
11
 */
12
class Po extends Extractor implements ExtractorInterface
13
{
14
    use HeadersExtractorTrait;
15
16
    /**
17
     * Parses a .po file and append the translations found in the Translations instance.
18
     *
19
     * {@inheritdoc}
20
     */
21
    public static function fromString($string, Translations $translations, array $options = [])
22
    {
23
        $lines = explode("\n", $string);
24
        $i = 0;
25
26
        $translation = new Translation('', '');
27
28
        for ($n = count($lines); $i < $n; ++$i) {
29
            $line = trim($lines[$i]);
30
            $line = self::fixMultiLines($line, $lines, $i);
31
32
            if ($line === '') {
33
                if ($translation->is('', '')) {
34
                    self::extractHeaders($translation->getTranslation(), $translations);
35
                } elseif ($translation->hasOriginal()) {
36
                    $translations[] = $translation;
37
                }
38
39
                $translation = new Translation('', '');
40
                continue;
41
            }
42
43
            $splitLine = preg_split('/\s+/', $line, 2);
44
            $key = $splitLine[0];
45
            $data = isset($splitLine[1]) ? $splitLine[1] : '';
46
47
            switch ($key) {
48
                case '#':
49
                    $translation->addComment($data);
50
                    $append = null;
51
                    break;
52
53
                case '#.':
54
                    $translation->addExtractedComment($data);
55
                    $append = null;
56
                    break;
57
58
                case '#,':
59
                    foreach (array_map('trim', explode(',', trim($data))) as $value) {
60
                        $translation->addFlag($value);
61
                    }
62
                    $append = null;
63
                    break;
64
65
                case '#:':
66
                    foreach (preg_split('/\s+/', trim($data)) as $value) {
67
                        if (preg_match('/^(.+)(:(\d*))?$/U', $value, $matches)) {
68
                            $translation->addReference($matches[1], isset($matches[3]) ? $matches[3] : null);
69
                        }
70
                    }
71
                    $append = null;
72
                    break;
73
74
                case 'msgctxt':
75
                    $translation = $translation->getClone(self::convertString($data));
76
                    $append = 'Context';
77
                    break;
78
79
                case 'msgid':
80
                    $translation = $translation->getClone(null, self::convertString($data));
81
                    $append = 'Original';
82
                    break;
83
84
                case 'msgid_plural':
85
                    $translation->setPlural(self::convertString($data));
86
                    $append = 'Plural';
87
                    break;
88
89
                case 'msgstr':
90
                case 'msgstr[0]':
91
                    $translation->setTranslation(self::convertString($data));
92
                    $append = 'Translation';
93
                    break;
94
95
                case 'msgstr[1]':
96
                    $translation->setPluralTranslations([self::convertString($data)]);
97
                    $append = 'PluralTranslation';
98
                    break;
99
100
                default:
101 View Code Duplication
                    if (strpos($key, 'msgstr[') === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
                        $p = $translation->getPluralTranslations();
103
                        $p[] = self::convertString($data);
104
105
                        $translation->setPluralTranslations($p);
106
                        $append = 'PluralTranslation';
107
                        break;
108
                    }
109
110
                    if (isset($append)) {
111
                        if ($append === 'Context') {
112
                            $translation = $translation->getClone($translation->getContext()."\n".self::convertString($data));
113
                            break;
114
                        }
115
116
                        if ($append === 'Original') {
117
                            $translation = $translation->getClone(null, $translation->getOriginal()."\n".self::convertString($data));
118
                            break;
119
                        }
120
121 View Code Duplication
                        if ($append === 'PluralTranslation') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
                            $p = $translation->getPluralTranslations();
123
                            $p[] = array_pop($p)."\n".self::convertString($data);
124
                            $translation->setPluralTranslations($p);
125
                            break;
126
                        }
127
128
                        $getMethod = 'get'.$append;
129
                        $setMethod = 'set'.$append;
130
                        $translation->$setMethod($translation->$getMethod()."\n".self::convertString($data));
131
                    }
132
                    break;
133
            }
134
        }
135
136
        if ($translation->hasOriginal() && !in_array($translation, iterator_to_array($translations))) {
137
            $translations[] = $translation;
138
        }
139
140
        return $translations;
141
    }
142
143
    /**
144
     * Gets one string from multiline strings.
145
     *
146
     * @param string $line
147
     * @param array  $lines
148
     * @param int    &$i
149
     *
150
     * @return string
151
     */
152
    private static function fixMultiLines($line, array $lines, &$i)
153
    {
154
        for ($j = $i, $t = count($lines); $j < $t; ++$j) {
155
            if (substr($line, -1, 1) == '"'
156
                && isset($lines[$j + 1])
157
                && substr(trim($lines[$j + 1]), 0, 1) == '"'
158
            ) {
159
                $line = substr($line, 0, -1).substr(trim($lines[$j + 1]), 1);
160
            } else {
161
                $i = $j;
162
                break;
163
            }
164
        }
165
166
        return $line;
167
    }
168
169
    /**
170
     * Convert a string from its PO representation.
171
     *
172
     * @param string $value
173
     *
174
     * @return string
175
     */
176
    public static function convertString($value)
177
    {
178
        if (!$value) {
179
            return '';
180
        }
181
182
        if ($value[0] === '"') {
183
            $value = substr($value, 1, -1);
184
        }
185
186
        return strtr(
187
            $value,
188
            [
189
                '\\\\' => '\\',
190
                '\\a' => "\x07",
191
                '\\b' => "\x08",
192
                '\\t' => "\t",
193
                '\\n' => "\n",
194
                '\\v' => "\x0b",
195
                '\\f' => "\x0c",
196
                '\\r' => "\r",
197
                '\\"' => '"',
198
            ]
199
        );
200
    }
201
}
202