Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 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\Exception\LogicException; |
| 15 | use Symfony\Component\Translation\MessageCatalogue; |
| 16 | use Symfony\Component\Translation\Util\ArrayConverter; |
| 17 | use Symfony\Component\Yaml\Yaml; |
| 18 | |
| 19 | /** |
| 20 | * YamlFileDumper generates yaml files from a message catalogue. |
| 21 | * |
| 22 | * @author Michel Salib <michelsalib@hotmail.com> |
| 23 | */ |
| 24 | class YamlFileDumper extends FileDumper |
| 25 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 26 | private string $extension; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 27 | |
| 28 | public function __construct(string $extension = 'yml') |
| 29 | { |
| 30 | $this->extension = $extension; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * {@inheritdoc} |
| 35 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 36 | public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 37 | { |
| 38 | if (!class_exists(Yaml::class)) { |
| 39 | throw new LogicException('Dumping translations in the YAML format requires the Symfony Yaml component.'); |
| 40 | } |
| 41 | |
| 42 | $data = $messages->all($domain); |
| 43 | |
| 44 | if (isset($options['as_tree']) && $options['as_tree']) { |
| 45 | $data = ArrayConverter::expandToTree($data); |
| 46 | } |
| 47 | |
| 48 | if (isset($options['inline']) && ($inline = (int) $options['inline']) > 0) { |
| 49 | return Yaml::dump($data, $inline); |
| 50 | } |
| 51 | |
| 52 | return Yaml::dump($data); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * {@inheritdoc} |
| 57 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 58 | protected function getExtension(): string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 59 | { |
| 60 | return $this->extension; |
| 61 | } |
| 62 | } |