From bfdc35439be31bbfd72faf1b4ff8cb72a93a4a76 Mon Sep 17 00:00:00 2001 From: Gennady Telegin Date: Mon, 25 May 2015 17:59:42 +0300 Subject: [PATCH] [Translation] added option flags to JsonFileDumper. It's passed to json_encode function second argument. --- .../Component/Translation/CHANGELOG.md | 2 +- .../Translation/Dumper/JsonFileDumper.php | 16 ++++++++++- .../Tests/Dumper/JsonFileDumperTest.php | 28 ++++++++++++++++--- .../Tests/fixtures/resources.dump.json | 1 + 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 src/Symfony/Component/Translation/Tests/fixtures/resources.dump.json diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index d02919d9da9ac..41f99cb4e923d 100644 --- a/src/Symfony/Component/Translation/CHANGELOG.md +++ b/src/Symfony/Component/Translation/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * deprecated Translator::getMessages(), rely on TranslatorBagInterface::getCatalogue() instead. + * added option `json_encoding` to JsonFileDumper * added options `as_tree`, `inline` to YamlFileDumper * added support for XLIFF target and tool attributes. * added message parameters to DataCollectorTranslator. @@ -13,7 +14,6 @@ CHANGELOG so the class name is misleading. The `TargetOperation` class should be used for this use-case instead. - 2.7.0 ----- diff --git a/src/Symfony/Component/Translation/Dumper/JsonFileDumper.php b/src/Symfony/Component/Translation/Dumper/JsonFileDumper.php index 7ad35184cd55e..2f4156d8990a7 100644 --- a/src/Symfony/Component/Translation/Dumper/JsonFileDumper.php +++ b/src/Symfony/Component/Translation/Dumper/JsonFileDumper.php @@ -25,7 +25,21 @@ class JsonFileDumper extends FileDumper */ public function format(MessageCatalogue $messages, $domain = 'messages') { - return json_encode($messages->all($domain), defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0); + return $this->formatCatalogue($messages, $domain); + } + + /** + * {@inheritdoc} + */ + protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array()) + { + if (isset($options['json_encoding'])) { + $flags = $options['json_encoding']; + } else { + $flags = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0; + } + + return json_encode($messages->all($domain), $flags); } /** diff --git a/src/Symfony/Component/Translation/Tests/Dumper/JsonFileDumperTest.php b/src/Symfony/Component/Translation/Tests/Dumper/JsonFileDumperTest.php index 697cd939f607a..0d6ffd58f9f59 100644 --- a/src/Symfony/Component/Translation/Tests/Dumper/JsonFileDumperTest.php +++ b/src/Symfony/Component/Translation/Tests/Dumper/JsonFileDumperTest.php @@ -16,6 +16,18 @@ class JsonFileDumperTest extends \PHPUnit_Framework_TestCase { + private $tempDir; + + protected function setUp() + { + $this->tempDir = sys_get_temp_dir(); + } + + protected function tearDown() + { + unlink($this->tempDir.'/messages.en.json'); + } + public function testDump() { if (PHP_VERSION_ID < 50400) { @@ -25,12 +37,20 @@ public function testDump() $catalogue = new MessageCatalogue('en'); $catalogue->add(array('foo' => 'bar')); - $tempDir = sys_get_temp_dir(); $dumper = new JsonFileDumper(); - $dumper->dump($catalogue, array('path' => $tempDir)); + $dumper->dump($catalogue, array('path' => $this->tempDir)); - $this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.json'), file_get_contents($tempDir.'/messages.en.json')); + $this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.json'), file_get_contents($this->tempDir.'/messages.en.json')); + } + + public function testDumpWithCustomEncoding() + { + $catalogue = new MessageCatalogue('en'); + $catalogue->add(array('foo' => '"bar"')); + + $dumper = new JsonFileDumper(); + $dumper->dump($catalogue, array('path' => $this->tempDir, 'json_encoding' => JSON_HEX_QUOT)); - unlink($tempDir.'/messages.en.json'); + $this->assertEquals(file_get_contents(__DIR__.'/../fixtures/resources.dump.json'), file_get_contents($this->tempDir.'/messages.en.json')); } } diff --git a/src/Symfony/Component/Translation/Tests/fixtures/resources.dump.json b/src/Symfony/Component/Translation/Tests/fixtures/resources.dump.json new file mode 100644 index 0000000000000..335965d592cab --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/fixtures/resources.dump.json @@ -0,0 +1 @@ +{"foo":"\u0022bar\u0022"} \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy