PoFile::createKeyFromEntry()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Geekwright\Po;
4
5
use Geekwright\Po\Exceptions\UnrecognizedInputException;
6
use Geekwright\Po\Exceptions\FileNotReadableException;
7
use Geekwright\Po\Exceptions\FileNotWritableException;
8
9
/**
10
 * PoFile - represent all entries in a GNU gettext style PO or POT file as a
11
 * collection of PoHeader and PoEntry objects.
12
 *
13
 * @category  File
14
 * @package   Po
15
 * @author    Richard Griffith <[email protected]>
16
 * @copyright 2015-2018 Richard Griffith
17
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
18
 * @link      https://github.com/geekwright/Po
19
 */
20
class PoFile
21
{
22
    /**
23
     * @var PoHeader $header
24
     */
25
    protected $header = null;
26
27
    /**
28
     * @var PoEntry[] $entries
29
     */
30
    protected $entries = array();
31
32
    /**
33
     * @var PoEntry[] $unkeyedEntries
34
     */
35
    protected $unkeyedEntries = array();
36
37
    /**
38
     * $var array() $unrecognizedInput
39
     *
40
     * If any lines that cannot be processed are found when reading a po file, the
41
     * unrecognized input will be recorded here, and an exception will be thrown.
42
     * No interface is supplied, but this debug data is an array in the form:
43
     * line number => input line
44
     */
45
    public $unrecognizedInput = array();
46
47
    /**
48
     * Build a PoFile, empty or with provided entries
49
     *
50
     * @param PoHeader|null $header         header object
51
     * @param PoEntry[]     $entries        associative array po entries
52
     * @param PoEntry[]     $unkeyedEntries indexed array of po entries. Unkeyed entries
53
     *                                      are usually comment only entries, such as for
54
     *                                      obsolete entries.
55
     */
56 39
    public function __construct(?array $header = null, array $entries = array(), array $unkeyedEntries = array())
57
    {
58 39
        $this->header = $header;
59 39
        $this->entries = $entries;
60 39
        $this->unkeyedEntries = $unkeyedEntries;
61 39
    }
62
63
    /**
64
     * Build the internal entries array key from id, context and plural id
65
     *
66
     * @param string|null $msgid        the untranslated message of the entry
67
     * @param string|null $msgctxt      the context of the entry, if any
68
     * @param string|null $msgid_plural the untranslated plural message of the entry, if any
69
     *
70
     * @return string
71
     */
72 18
    public static function createKey(?string $msgid, ?string $msgctxt = null, ?string $msgid_plural = null): string
73
    {
74 18
        $key = '';
75 18
        if (!empty($msgctxt)) {
76 4
            $key .= $msgctxt . '|';
77
        }
78 18
        $key .= (string) $msgid;
79 18
        if (!empty($msgid_plural)) {
80 10
            $key .= '|' . $msgid_plural;
81
        }
82 18
        return $key;
83
    }
84
85
    /**
86
     * Build an internal entries array key from a PoEntry
87
     *
88
     * @param PoEntry $entry the PoEntry to build key from
89
     *
90
     * @return string
91
     */
92 17
    public function createKeyFromEntry(PoEntry $entry): string
93
    {
94 17
        return $this->createKey(
95 17
            $entry->getAsString(PoTokens::MESSAGE),
96 17
            $entry->getAsString(PoTokens::CONTEXT),
97 17
            $entry->getAsString(PoTokens::PLURAL)
98
        );
99
    }
100
101
    /**
102
     * Replace any existing header with the provided PoHeader
103
     *
104
     * @param PoHeader $header header object
105
     *
106
     * @return void
107
     */
108 8
    public function setHeaderEntry(PoHeader $header): void
109
    {
110 8
        $this->header = $header;
111 8
    }
112
113
    /**
114
     * Get the current header entry
115
     *
116
     * @return PoHeader
117
     */
118 2
    public function getHeaderEntry(): PoHeader
119
    {
120 2
        return $this->header;
121
    }
122
123
    /**
124
     * Get an array of current entries
125
     *
126
     * @return PoEntry[]
127
     */
128 11
    public function getEntries(): array
129
    {
130 11
        return $this->entries;
131
    }
132
133
    /**
134
     * Replace any existing unkeyedEntries with new array of PoEntry objects
135
     *
136
     * @param PoEntry[] $entries po entries
137
     *
138
     * @return void
139
     */
140 1
    public function setUnkeyedEntries(array $entries): void
141
    {
142 1
        $this->unkeyedEntries = $entries;
143 1
    }
144
145
    /**
146
     * Get current array of unkeyed PoEntry objects
147
     *
148
     * @return PoEntry[]
149
     */
150 2
    public function getUnkeyedEntries(): array
151
    {
152 2
        return $this->unkeyedEntries;
153
    }
154
155
    /**
156
     * Add an entry to the PoFile using internal key
157
     *
158
     * @param PoEntry $entry   the PoEntry to add
159
     * @param boolean $replace true to replace any existing entry matching this key,
160
     *                         false to not change the PoFile for a duplicated key
161
     *
162
     * @return boolean true if added, false if not
163
     */
164 9
    public function addEntry(PoEntry $entry, bool $replace = true): bool
165
    {
166 9
        $key = $this->createKeyFromEntry($entry);
167
168
        // some entires, such as obsolete entries, have no key
169
        // for some uses, these are dead weight - need better strategy for that case
170 9
        if (empty($key)) {
171 4
            $this->unkeyedEntries[] = $entry;
172 4
            return true;
173
        }
174
175 7
        if (isset($this->entries[$key]) && !$replace) {
176 1
            return false;
177
        } else {
178 7
            $this->entries[$key] = $entry;
179 7
            return true;
180
        }
181
    }
182
183
    /**
184
     * Merge an entry with any existing entry with the same key. If the key does
185
     * not exist, add the entry, otherwise merge comments, references, and flags.
186
     *
187
     * This is intended for use in building a POT, where the handling of translated
188
     * strings is not a factor.
189
     *
190
     * @param PoEntry $newEntry the PoEntry to merge
191
     *
192
     * @return boolean true if merged or added, false if not
193
     */
194 7
    public function mergeEntry(PoEntry $newEntry): bool
195
    {
196 7
        $key = $this->createKeyFromEntry($newEntry);
197
198
        // keyed entries only
199 7
        if (empty($key)) {
200 1
            return false;
201
        }
202
203 7
        if (isset($this->entries[$key])) {
204 2
            $existingEntry = $this->entries[$key];
205 2
            $mergeTokens = array(PoTokens::REFERENCE, PoTokens::EXTRACTED_COMMENTS);
206 2
            foreach ($mergeTokens as $type) {
207 2
                $toMerge = $newEntry->get($type);
208 2
                if (!empty($toMerge)) {
209 2
                    $toMerge = is_array($toMerge) ? $toMerge : array($toMerge);
210 2
                    foreach ($toMerge as $value) {
211 2
                        $existingEntry->add($type, $value);
212
                    }
213
                }
214
            }
215
        } else {
216 7
            $this->entries[$key] = $newEntry;
217
        }
218 7
        return true;
219
    }
220
221
    /**
222
     * Get an entry based on key values - msgid, msgctxt and msgid_plural
223
     *
224
     * @param string      $msgid        the untranslated message of the entry
225
     * @param string|null $msgctxt      the context of the entry, if any
226
     * @param string|null $msgid_plural the untranslated plural message of the entry, if any
227
     *
228
     * @return PoEntry|null matching entry, or null if not found
229
     */
230 2
    public function findEntry(string $msgid, ?string $msgctxt = null, ?string $msgid_plural = null): ?PoEntry
231
    {
232 2
        $key = $this->createKey($msgid, $msgctxt, $msgid_plural);
233 2
        $entry = null;
234
235 2
        if (!empty($key) && isset($this->entries[$key])) {
236 2
            $entry = $this->entries[$key];
237
        }
238
239 2
        return $entry;
240
    }
241
242
    /**
243
     * Remove an entry from the PoFile
244
     *
245
     * In simple cases, the entry can be found by key. There are several cases
246
     * where it is not that easy to locate the PoEntry to be removed:
247
     *  - the PoEntry was altered, making the generated and stored key different
248
     *  - the entry is not keyed and is in unkeyedEntries
249
     *
250
     * In any of these cases, we must loop thru the entry arrays looking for an
251
     * exact object match, so the cost of the remove goes up
252
     *
253
     * @param PoEntry $entry the PoEntry to merge
254
     *
255
     * @return boolean true if remove, false if not
256
     */
257 1
    public function removeEntry(PoEntry $entry): bool
258
    {
259 1
        $key = $this->createKeyFromEntry($entry);
260
261
        // try by the key first.
262 1
        if (!empty($key) && isset($this->entries[$key])) {
263 1
            if ($entry === $this->entries[$key]) {
264 1
                unset($this->entries[$key]);
265 1
                return true;
266
            }
267
        }
268
269
        // the entry can't be matched by key, so we have to loop :(
270 1
        foreach ($this->entries as $key => $value) {
271 1
            if ($entry === $value) {
272 1
                unset($this->entries[$key]);
273 1
                return true;
274
            }
275
        }
276
277
        // no match found in main entries, try the unkeyedEntries
278 1
        foreach ($this->unkeyedEntries as $key => $value) {
279 1
            if ($entry === $value) {
280 1
                unset($this->unkeyedEntries[$key]);
281 1
                return true;
282
            }
283
        }
284
285 1
        return false;
286
    }
287
288
    /**
289
     * Write any current contents to a po file
290
     *
291
     * @param string $file po file to write
292
     *
293
     * @return void
294
     *
295
     * @throws FileNotWritableException
296
     */
297 2
    public function writePoFile(string $file): void
298
    {
299 2
        $source = $this->dumpString();
300 2
        $testName = file_exists($file) ? $file : dirname($file);
301 2
        $status = is_writable($testName);
302 2
        if ($status === true) {
303 1
            $status = file_put_contents($file, $source);
304
        }
305 2
        if (false === $status) {
306 1
            throw new FileNotWritableException($file);
307
        }
308 1
    }
309
310
    /**
311
     * Dump the current contents in PO format to a string
312
     *
313
     * @return string
314
     */
315 5
    public function dumpString(): string
316
    {
317 5
        if ($this->header === null) {
318 2
            $this->header = new PoHeader;
319 2
            $this->header->buildDefaultHeader();
320
        }
321 5
        $output = '';
322
323 5
        $output .= $this->header->dumpEntry();
324 5
        foreach ($this->entries as $entry) {
325 3
            $output .= $entry->dumpEntry();
326
        }
327 5
        foreach ($this->unkeyedEntries as $entry) {
328 2
            $output .= $entry->dumpEntry();
329
        }
330 5
        $output .= "\n";
331
332 5
        return $output;
333
    }
334
335
336
    /**
337
     * Replace any current contents with entries from a file
338
     *
339
     * @param string        $file    po file/stream to read
340
     * @param resource|null $context context for stream if required
341
     *
342
     * @return void
343
     *
344
     * @throws FileNotReadableException
345
     */
346 6
    public function readPoFile(string $file, $context = null): void
347
    {
348 6
        $oldEr = error_reporting(E_ALL ^ E_WARNING);
349 6
        $source = file_get_contents($file, false, $context);
350 6
        error_reporting($oldEr);
351 6
        if (false===$source) {
352 1
            throw new FileNotReadableException($file);
353
        }
354 5
        $this->parsePoSource($source);
355 4
    }
356
357
    /**
358
     * Replace any current contents with header and entries from PO souce string
359
     *
360
     * @param string $source po formatted string to parse
361
     *
362
     * @return void
363
     *
364
     * @throws UnrecognizedInputException
365
     */
366 7
    public function parsePoSource(string $source): void
367
    {
368
        /**
369
         * This is an incredibly ugly regex pattern that breaks a line of a po file into
370
         * pieces that can be analyzed and acted upon.
371
         *
372
         * The matches array in preg_match will break out like this:
373
         *  [0] full string
374
         *  [1] mostly useless broad match of initial token, including trailing space
375
         *  [2] bare token, or full msgstr[n] clause
376
         *  [3] 'n' of a msgstr[n] line
377
         *  [4] '"' if a data line
378
         *  [5] remaining line
379
         *  [6] a bare or malformed comment
380
         */
381 7
        $pattern = '/(^(#|#.|#;|#,|#\||msgid|msgid_plural|msgctxt|msgstr|msgstr\[([0-9]+)\])\s|(^"))(.+)|(^#.*)/';
382
383 7
        $source_lines = explode("\n", $source);
384
385 7
        $wsBreak = false;
386 7
        $inHeader = true;
387 7
        $headerEntry = new PoHeader;
388 7
        $entry = $headerEntry;
389 7
        $unrecognized = array();
390 7
        $lastKey = '';
391 7
        $currentPlural = 0;
392 7
        foreach ($source_lines as $line => $s) {
393 7
            $result = preg_match($pattern, $s, $matches);
394 7
            if (!$result) {
395 7
                $lastKey = '';
396 7
                if ($s=='' || ctype_space($s)) {
397 7
                    if ($inHeader) {
398 7
                        $this->setHeaderEntry($headerEntry);
399 7
                        $entry = null;
400 7
                        $inHeader = false;
401
                    }
402 7
                    if (!$wsBreak) {
403 7
                        if (!($entry === null)) {
404 6
                            $this->addEntry($entry);
405
                        }
406 7
                        $entry = null;
407 7
                        $wsBreak=true;
408
                    }
409
                } else {
410 2
                    $wsBreak=false;
411 7
                    $unrecognized[$line+1] = $s;
412
                }
413
            } else {
414 6
                if ($entry === null) {
415 6
                    $entry = new PoEntry;
416
                }
417 6
                $wsBreak=false;
418 6
                $currentKey = $matches[2];  // will be used to set last key
419 6
                switch ($matches[2]) {
420 6
                    case PoTokens::TRANSLATOR_COMMENTS:
421 6
                    case PoTokens::EXTRACTED_COMMENTS:
422 6
                    case PoTokens::REFERENCE:
423 6
                    case PoTokens::FLAG:
424 6
                    case PoTokens::OBSOLETE:
425 6
                    case PoTokens::PREVIOUS:
426 6
                        $entry->add($matches[2], $matches[5]);
427 6
                        break;
428 6
                    case PoTokens::CONTEXT:
429 6
                    case PoTokens::MESSAGE:
430 6
                    case PoTokens::PLURAL:
431 6
                    case PoTokens::TRANSLATED:
432 6
                        $entry->addQuoted($matches[2], $matches[5]);
433 6
                        break;
434
                    default:
435 6
                        if ($matches[4]==PoTokens::CONTINUED_DATA) {
436 6
                            $currentKey = $lastKey; // keep the previous key
437 6
                            if ($currentKey==PoTokens::TRANSLATED_PLURAL) {
438 5
                                $entry->addQuotedAtPosition(
439 5
                                    PoTokens::TRANSLATED,
440 5
                                    $currentPlural,
441 5
                                    '"' . $matches[5]
442
                                );
443
                            } else {
444 6
                                $entry->addQuoted($currentKey, '"' . $matches[5]);
445
                            }
446 6
                        } elseif (substr($matches[2], 0, 7)==PoTokens::TRANSLATED_PLURAL) {
447 5
                            $currentKey = PoTokens::TRANSLATED_PLURAL;
448 5
                            $currentPlural = $matches[3];
449 5
                            $entry->addQuotedAtPosition(
450 5
                                PoTokens::TRANSLATED,
451 5
                                $currentPlural,
452 5
                                $matches[5]
453
                            );
454 6
                        } elseif (isset($matches[6][0])
455 6
                            && $matches[6][0]==PoTokens::TRANSLATOR_COMMENTS) {
456 6
                            $value = substr($matches[6], 1);
457 6
                            $value = empty($value) ? '' : $value;
458 6
                            $entry->add(PoTokens::TRANSLATOR_COMMENTS, $value);
459
                        } else {
460
                            $unrecognized[$line+1] = $s;
461
                        }
462 6
                        break;
463
                }
464 7
                $lastKey = $currentKey;
465
            }
466
        }
467 7
        if (!($entry === null)) {
468
            $this->addEntry($entry);
469
        }
470
471
        // throw at the very end, anything recognized has been processed
472 7
        $this->unrecognizedInput = $unrecognized;
473 7
        if (count($unrecognized)) {
474 2
            throw new UnrecognizedInputException();
475
        }
476 5
    }
477
}
478