Minor dependency updates
[yaffs-website] / vendor / symfony / translation / Dumper / MoFileDumper.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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\MessageCatalogue;
15 use Symfony\Component\Translation\Loader\MoFileLoader;
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     /**
25      * {@inheritdoc}
26      */
27     public function format(MessageCatalogue $messages, $domain = 'messages')
28     {
29         @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use the formatCatalogue() method instead.', E_USER_DEPRECATED);
30
31         return $this->formatCatalogue($messages, $domain);
32     }
33
34     /**
35      * {@inheritdoc}
36      */
37     public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
38     {
39         $sources = $targets = $sourceOffsets = $targetOffsets = '';
40         $offsets = array();
41         $size = 0;
42
43         foreach ($messages->all($domain) as $source => $target) {
44             $offsets[] = array_map('strlen', array($sources, $source, $targets, $target));
45             $sources .= "\0".$source;
46             $targets .= "\0".$target;
47             ++$size;
48         }
49
50         $header = array(
51             'magicNumber' => MoFileLoader::MO_LITTLE_ENDIAN_MAGIC,
52             'formatRevision' => 0,
53             'count' => $size,
54             'offsetId' => MoFileLoader::MO_HEADER_SIZE,
55             'offsetTranslated' => MoFileLoader::MO_HEADER_SIZE + (8 * $size),
56             'sizeHashes' => 0,
57             'offsetHashes' => MoFileLoader::MO_HEADER_SIZE + (16 * $size),
58         );
59
60         $sourcesSize = strlen($sources);
61         $sourcesStart = $header['offsetHashes'] + 1;
62
63         foreach ($offsets as $offset) {
64             $sourceOffsets .= $this->writeLong($offset[1])
65                           .$this->writeLong($offset[0] + $sourcesStart);
66             $targetOffsets .= $this->writeLong($offset[3])
67                           .$this->writeLong($offset[2] + $sourcesStart + $sourcesSize);
68         }
69
70         $output = implode(array_map(array($this, 'writeLong'), $header))
71                .$sourceOffsets
72                .$targetOffsets
73                .$sources
74                .$targets
75                 ;
76
77         return $output;
78     }
79
80     /**
81      * {@inheritdoc}
82      */
83     protected function getExtension()
84     {
85         return 'mo';
86     }
87
88     private function writeLong($str)
89     {
90         return pack('V*', $str);
91     }
92 }