Completed
Pull Request — master (#93)
by Michele
03:04 queued 01:21
created

Po   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 7
Bugs 2 Features 1
Metric Value
wmc 20
c 7
b 2
f 1
lcom 0
cbo 3
dl 0
loc 109
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C toString() 0 58 14
A multilineQuote() 0 15 3
A addLines() 0 14 3
1
<?php
2
3
namespace Gettext\Generators;
4
5
use Gettext\Translations;
6
use Gettext\Utils\Strings;
7
8
class Po extends Generator implements GeneratorInterface
9
{
10
    /**
11
     * {@parentDoc}.
12
     */
13
    public static function toString(Translations $translations)
14
    {
15
        $lines = array('msgid ""', 'msgstr ""');
16
17
        $headers = $translations->getHeaders();
18
        $headers['PO-Revision-Date'] = date('c');
19
20
        foreach ($headers as $name => $value) {
21
            $lines[] = '"'.$name.': '.$value.'\\n"';
22
        }
23
24
        $lines[] = '';
25
26
        //Translations
27
        foreach ($translations as $translation) {
28
            if ($translation->hasComments()) {
29
                foreach ($translation->getComments() as $comment) {
30
                    $lines[] = '# '.$comment;
31
                }
32
            }
33
34
            if ($translation->hasExtractedComments()) {
35
                foreach ($translation->getExtractedComments() as $comment) {
36
                    $lines[] = '#. '.$comment;
37
                }
38
            }
39
40
            if ($translation->hasReferences()) {
41
                foreach ($translation->getReferences() as $reference) {
42
                    $lines[] = '#: '.$reference[0].(!is_null($reference[1]) ? ':'.$reference[1] : null);
43
                }
44
            }
45
46
            if ($translation->hasFlags()) {
47
                $lines[] = '#, '.implode(',', $translation->getFlags());
48
            }
49
50
            if ($translation->hasContext()) {
51
                $lines[] = 'msgctxt '.Strings::toPo($translation->getContext());
52
            }
53
54
            self::addLines($lines, 'msgid', $translation->getOriginal());
55
            if ($translation->hasPlural()) {
56
                self::addLines($lines, 'msgid_plural', $translation->getPlural());
57
                self::addLines($lines, 'msgstr[0]', $translation->getTranslation());
58
59
                foreach ($translation->getPluralTranslation() as $k => $v) {
60
                    self::addLines($lines, 'msgstr['.($k + 1).']', $v);
61
                }
62
            } else {
63
                self::addLines($lines, 'msgstr', $translation->getTranslation());
64
            }
65
66
            $lines[] = '';
67
        }
68
69
        return implode("\n", $lines);
70
    }
71
72
    /**
73
     * Escapes and adds double quotes to a string.
74
     *
75
     * @param string $string
76
     *
77
     * @return string
78
     */
79
    private static function multilineQuote($string)
80
    {
81
        $lines = explode("\n", $string);
82
        $last = count($lines) - 1;
83
84
        foreach ($lines as $k => $line) {
85
            if ($k === $last) {
86
                $lines[$k] = Strings::toPo($line);
87
            } else {
88
                $lines[$k] = Strings::toPo($line."\n");
89
            }
90
        }
91
92
        return $lines;
93
    }
94
95
    /**
96
     * Add one or more lines depending whether the string is multiline or not.
97
     *
98
     * @param array  &$lines
99
     * @param string $name
100
     * @param string $value
101
     */
102
    private static function addLines(array &$lines, $name, $value)
103
    {
104
        $newLines = self::multilineQuote($value);
105
106
        if (count($newLines) === 1) {
107
            $lines[] = $name.' '.$newLines[0];
108
        } else {
109
            $lines[] = $name.' ""';
110
111
            foreach ($newLines as $line) {
112
                $lines[] = $line;
113
            }
114
        }
115
    }
116
}
117