Sepia\PoParser::compile PHP Method

compile() public method

Compiles entries into a string
public compile ( ) : string
return string
    public function compile()
    {
        $output = '';
        if (count($this->headers) > 0) {
            $output .= "msgid \"\"\n";
            $output .= "msgstr \"\"\n";
            foreach ($this->headers as $header) {
                $output .= $header . "\n";
            }
            $output .= "\n";
        }
        $entriesCount = count($this->entries);
        $counter = 0;
        foreach ($this->entries as $entry) {
            $isObsolete = isset($entry['obsolete']) && $entry['obsolete'];
            $isPlural = isset($entry['msgid_plural']);
            if (isset($entry['previous'])) {
                foreach ($entry['previous'] as $key => $data) {
                    if (is_string($data)) {
                        $output .= "#| " . $key . " " . $this->cleanExport($data) . "\n";
                    } elseif (is_array($data) && count($data) > 0) {
                        foreach ($data as $line) {
                            $output .= "#| " . $key . " " . $this->cleanExport($line) . "\n";
                        }
                    }
                }
            }
            if (isset($entry['tcomment'])) {
                foreach ($entry['tcomment'] as $comment) {
                    $output .= "# " . $comment . "\n";
                }
            }
            if (isset($entry['ccomment'])) {
                foreach ($entry['ccomment'] as $comment) {
                    $output .= '#. ' . $comment . "\n";
                }
            }
            if (isset($entry['reference'])) {
                foreach ($entry['reference'] as $ref) {
                    $output .= '#: ' . $ref . "\n";
                }
            }
            if (isset($entry['flags']) && !empty($entry['flags'])) {
                $output .= "#, " . implode(', ', $entry['flags']) . "\n";
            }
            if (isset($entry['@'])) {
                $output .= "#@ " . $entry['@'] . "\n";
            }
            if (isset($entry['msgctxt'])) {
                $output .= 'msgctxt ' . $this->cleanExport($entry['msgctxt'][0]) . "\n";
            }
            if ($isObsolete) {
                $output .= "#~ ";
            }
            if (isset($entry['msgid'])) {
                // Special clean for msgid
                if (is_string($entry['msgid'])) {
                    $msgid = explode("\n", $entry['msgid']);
                } elseif (is_array($entry['msgid'])) {
                    $msgid = $entry['msgid'];
                } else {
                    throw new \Exception('msgid not string or array');
                }
                $output .= 'msgid ';
                foreach ($msgid as $i => $id) {
                    if ($i > 0 && $isObsolete) {
                        $output .= "#~ ";
                    }
                    $output .= $this->cleanExport($id) . "\n";
                }
            }
            if (isset($entry['msgid_plural'])) {
                // Special clean for msgid_plural
                if (is_string($entry['msgid_plural'])) {
                    $msgidPlural = explode("\n", $entry['msgid_plural']);
                } elseif (is_array($entry['msgid_plural'])) {
                    $msgidPlural = $entry['msgid_plural'];
                } else {
                    throw new \Exception('msgid_plural not string or array');
                }
                $output .= 'msgid_plural ';
                foreach ($msgidPlural as $plural) {
                    $output .= $this->cleanExport($plural) . "\n";
                }
            }
            if (count(preg_grep('/^msgstr/', array_keys($entry)))) {
                // checks if there is a key starting with msgstr
                if ($isPlural) {
                    $noTranslation = true;
                    foreach ($entry as $key => $value) {
                        if (strpos($key, 'msgstr[') === false) {
                            continue;
                        }
                        $output .= $key . " ";
                        $noTranslation = false;
                        foreach ($value as $i => $t) {
                            $output .= $this->cleanExport($t) . "\n";
                        }
                    }
                    if ($noTranslation) {
                        $output .= 'msgstr[0] ' . $this->cleanExport('') . "\n";
                        $output .= 'msgstr[1] ' . $this->cleanExport('') . "\n";
                    }
                } else {
                    foreach ((array) $entry['msgstr'] as $i => $t) {
                        if ($i == 0) {
                            if ($isObsolete) {
                                $output .= "#~ ";
                            }
                            $output .= 'msgstr ' . $this->cleanExport($t) . "\n";
                        } else {
                            if ($isObsolete) {
                                $output .= "#~ ";
                            }
                            $output .= $this->cleanExport($t) . "\n";
                        }
                    }
                }
            }
            $counter++;
            // Avoid inserting an extra newline at end of file
            if ($counter < $entriesCount) {
                $output .= "\n";
            }
        }
        return $output;
    }