Debian Codegraph @

lib/3rdParty/composer/symfony/translation/Dumper/MoFileDumper.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Translation\Dumper;
13
14 use Symfony\Component\Translation\Loader\MoFileLoader;
15 use Symfony\Component\Translation\MessageCatalogue;
16
17 /**
18 * MoFileDumper generates a gettext formatted string representation of a message catalogue.
19 *
20 * @author Stealth35
21 */
22 class MoFileDumper extends FileDumper
23 {
24 public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string
25 {
26 $sources = $targets = $sourceOffsets = $targetOffsets = '';
27 $offsets = [];
28 $size = 0;
29
30 foreach ($messages->all($domain) as $source => $target) {
31 $offsets[] = array_map('strlen', [$sources, $source, $targets, $target]);
32 $sources .= "\0".$source;
33 $targets .= "\0".$target;
34 ++$size;
35 }
36
37 $header = [
38 'magicNumber' => MoFileLoader::MO_LITTLE_ENDIAN_MAGIC,
39 'formatRevision' => 0,
40 'count' => $size,
41 'offsetId' => MoFileLoader::MO_HEADER_SIZE,
42 'offsetTranslated' => MoFileLoader::MO_HEADER_SIZE + (8 * $size),
43 'sizeHashes' => 0,
44 'offsetHashes' => MoFileLoader::MO_HEADER_SIZE + (16 * $size),
45 ];
46
47 $sourcesSize = \strlen($sources);
48 $sourcesStart = $header['offsetHashes'] + 1;
49
50 foreach ($offsets as $offset) {
51 $sourceOffsets .= $this->writeLong($offset[1])
52 .$this->writeLong($offset[0] + $sourcesStart);
53 $targetOffsets .= $this->writeLong($offset[3])
54 .$this->writeLong($offset[2] + $sourcesStart + $sourcesSize);
55 }
56
57 return implode('', array_map($this->writeLong(...), $header))
58 .$sourceOffsets
59 .$targetOffsets
60 .$sources
61 .$targets;
62 }
63
64 protected function getExtension(): string
65 {
66 return 'mo';
67 }
68
69 private function writeLong(mixed $str): string
70 {
71 return pack('V*', $str);
72 }
73 }