Completed
Pull Request — master (#92)
by Michele
04:35
created

Po::removeEOT()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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