Skip to content

Commit cea6344

Browse files
Added Metadata to MessageCatalogue.
Reran latest php-cs-fixer. Used correct exceptions.
1 parent 023dbf8 commit cea6344

File tree

3 files changed

+202
-14
lines changed

3 files changed

+202
-14
lines changed

src/Symfony/Component/Translation/MessageCatalogue.php

+87
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
class MessageCatalogue implements MessageCatalogueInterface
2424
{
2525
private $messages = array();
26+
private $metaData = array();
2627
private $locale;
2728
private $resources;
2829
private $fallbackCatalogue;
@@ -175,6 +176,9 @@ public function addCatalogue(MessageCatalogueInterface $catalogue)
175176
foreach ($catalogue->getResources() as $resource) {
176177
$this->addResource($resource);
177178
}
179+
180+
$meta = $catalogue->getMetaData();
181+
$this->addMetaData($meta);
178182
}
179183

180184
/**
@@ -231,4 +235,87 @@ public function addResource(ResourceInterface $resource)
231235
{
232236
$this->resources[] = $resource;
233237
}
238+
239+
/**
240+
* {@inheritdoc}
241+
*
242+
* @api
243+
*/
244+
public function getMetaData($domain = '', $key = '')
245+
{
246+
if (empty($domain)) {
247+
return $this->metaData;
248+
}
249+
250+
if (!is_string($domain)) {
251+
throw new \InvalidArgumentException("Domain should be an string.");
252+
}
253+
if (!is_string($key)) {
254+
throw new \InvalidArgumentException("Key should be an string.");
255+
}
256+
if (isset($this->metaData[$domain])) {
257+
if (!empty($key)) {
258+
if (isset($this->metaData[$domain][$key])) {
259+
return $this->metaData[$domain][$key];
260+
}
261+
} else {
262+
return $this->metaData[$domain];
263+
}
264+
}
265+
}
266+
267+
/**
268+
* {@inheritdoc}
269+
*
270+
* @api
271+
*/
272+
public function setMetaData($key, $value, $domain = 'messages')
273+
{
274+
if (!is_string($key)) {
275+
throw new \InvalidArgumentException("Key should be an string.");
276+
}
277+
if (!isset($this->metaData[$domain])) {
278+
$this->metaData[$domain] = array();
279+
}
280+
$this->metaData[$domain][$key] = $value;
281+
}
282+
283+
/**
284+
* {@inheritdoc}
285+
*
286+
* @api
287+
*/
288+
public function deleteMetaData($domain = '', $key = '')
289+
{
290+
if (empty($domain)) {
291+
$this->metaData = array();
292+
}
293+
if (!is_string($domain)) {
294+
throw new \InvalidArgumentException("Domain should be an string.");
295+
}
296+
if (empty($key)) {
297+
unset($this->metaData[$domain]);
298+
}
299+
if (!is_string($key)) {
300+
throw new \InvalidArgumentException("Key should be an string.");
301+
}
302+
unset($this->metaData[$domain][$key]);
303+
}
304+
305+
/**
306+
* Adds or overwrite current values with the new values.
307+
*
308+
* TODO: do we want to overwrite values?!?
309+
*
310+
* @param array $values Values to add
311+
*/
312+
private function addMetaData(array $values)
313+
{
314+
foreach ($values as $domain => $keys) {
315+
foreach ($keys as $key => $value) {
316+
$this->setMetaData($key, $value, $domain);
317+
}
318+
}
319+
}
320+
234321
}

src/Symfony/Component/Translation/MessageCatalogueInterface.php

+40-14
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface MessageCatalogueInterface
2929
*
3030
* @api
3131
*/
32-
function getLocale();
32+
public function getLocale();
3333

3434
/**
3535
* Gets the domains.
@@ -38,7 +38,7 @@ function getLocale();
3838
*
3939
* @api
4040
*/
41-
function getDomains();
41+
public function getDomains();
4242

4343
/**
4444
* Gets the messages within a given domain.
@@ -51,7 +51,7 @@ function getDomains();
5151
*
5252
* @api
5353
*/
54-
function all($domain = null);
54+
public function all($domain = null);
5555

5656
/**
5757
* Sets a message translation.
@@ -62,7 +62,7 @@ function all($domain = null);
6262
*
6363
* @api
6464
*/
65-
function set($id, $translation, $domain = 'messages');
65+
public function set($id, $translation, $domain = 'messages');
6666

6767
/**
6868
* Checks if a message has a translation.
@@ -74,7 +74,7 @@ function set($id, $translation, $domain = 'messages');
7474
*
7575
* @api
7676
*/
77-
function has($id, $domain = 'messages');
77+
public function has($id, $domain = 'messages');
7878

7979
/**
8080
* Checks if a message has a translation (it does not take into account the fallback mechanism).
@@ -86,7 +86,7 @@ function has($id, $domain = 'messages');
8686
*
8787
* @api
8888
*/
89-
function defines($id, $domain = 'messages');
89+
public function defines($id, $domain = 'messages');
9090

9191
/**
9292
* Gets a message translation.
@@ -98,7 +98,7 @@ function defines($id, $domain = 'messages');
9898
*
9999
* @api
100100
*/
101-
function get($id, $domain = 'messages');
101+
public function get($id, $domain = 'messages');
102102

103103
/**
104104
* Sets translations for a given domain.
@@ -108,7 +108,7 @@ function get($id, $domain = 'messages');
108108
*
109109
* @api
110110
*/
111-
function replace($messages, $domain = 'messages');
111+
public function replace($messages, $domain = 'messages');
112112

113113
/**
114114
* Adds translations for a given domain.
@@ -118,7 +118,7 @@ function replace($messages, $domain = 'messages');
118118
*
119119
* @api
120120
*/
121-
function add($messages, $domain = 'messages');
121+
public function add($messages, $domain = 'messages');
122122

123123
/**
124124
* Merges translations from the given Catalogue into the current one.
@@ -129,7 +129,7 @@ function add($messages, $domain = 'messages');
129129
*
130130
* @api
131131
*/
132-
function addCatalogue(MessageCatalogueInterface $catalogue);
132+
public function addCatalogue(MessageCatalogueInterface $catalogue);
133133

134134
/**
135135
* Merges translations from the given Catalogue into the current one
@@ -141,7 +141,7 @@ function addCatalogue(MessageCatalogueInterface $catalogue);
141141
*
142142
* @api
143143
*/
144-
function addFallbackCatalogue(MessageCatalogueInterface $catalogue);
144+
public function addFallbackCatalogue(MessageCatalogueInterface $catalogue);
145145

146146
/**
147147
* Gets the fallback catalogue.
@@ -150,7 +150,7 @@ function addFallbackCatalogue(MessageCatalogueInterface $catalogue);
150150
*
151151
* @api
152152
*/
153-
function getFallbackCatalogue();
153+
public function getFallbackCatalogue();
154154

155155
/**
156156
* Returns an array of resources loaded to build this collection.
@@ -159,7 +159,7 @@ function getFallbackCatalogue();
159159
*
160160
* @api
161161
*/
162-
function getResources();
162+
public function getResources();
163163

164164
/**
165165
* Adds a resource for this collection.
@@ -168,5 +168,31 @@ function getResources();
168168
*
169169
* @api
170170
*/
171-
function addResource(ResourceInterface $resource);
171+
public function addResource(ResourceInterface $resource);
172+
173+
/**
174+
* Gets meta data for given domain and key.
175+
*
176+
* @param string $key Key to set
177+
* @param string $domain The domain name
178+
*/
179+
public function getMetaData($domain = '', $key = '');
180+
181+
/**
182+
* Adds meta data to a message domain.
183+
*
184+
* @param string $key Key to set
185+
* @param string|array $value Value to store
186+
* @param string $domain The domain name
187+
*/
188+
public function setMetaData($key, $value, $domain = 'messages');
189+
190+
/**
191+
* Deletes meta data for given key and domain
192+
*
193+
* @param string $key Key to set
194+
* @param string $domain The domain name
195+
*/
196+
public function deleteMetaData($domain = '', $key = '');
197+
172198
}

src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php

+75
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,79 @@ public function testGetAddResource()
170170

171171
$this->assertEquals(array($r, $r1), $catalogue->getResources());
172172
}
173+
174+
public function testMetaDataDelete()
175+
{
176+
$catalogue = new MessageCatalogue('en');
177+
$expected = array();
178+
$actual = $catalogue->getMetaData();
179+
$this->assertEquals($expected, $actual, 'Metadata is empty');
180+
$catalogue->deleteMetaData('messages', 'key');
181+
$catalogue->deleteMetaData('messages');
182+
$catalogue->deleteMetaData();
183+
}
184+
185+
public function testMetaDataSetGetDelete()
186+
{
187+
$catalogue = new MessageCatalogue('en');
188+
$catalogue->setMetaData('key', 'value');
189+
$expected = 'value';
190+
$actual = $catalogue->getMetaData('messages', 'key');
191+
$this->assertEquals($expected, $actual, "Metadata 'key' = 'value'");
192+
193+
$catalogue->setMetaData('key2', array());
194+
$expected = array();
195+
$actual = $catalogue->getMetaData('messages', 'key2');
196+
$this->assertEquals($expected, $actual, 'Metadata key2 is array');
197+
198+
$catalogue->deleteMetaData('messages', 'key2');
199+
$expected = null;
200+
$actual = $catalogue->getMetaData('messages', 'key2');
201+
$this->assertEquals($expected, $actual, 'Metadata key2 should is deleted.');
202+
203+
$catalogue->deleteMetaData('domain', 'key2');
204+
$expected = null;
205+
$actual = $catalogue->getMetaData('domain', 'key2');
206+
$this->assertEquals($expected, $actual, 'Metadata key2 should is deleted.');
207+
208+
}
209+
210+
/**
211+
* @expectedException \InvalidArgumentException
212+
*/
213+
public function testMetaDataExceptionsKey()
214+
{
215+
$catalogue = new MessageCatalogue('en');
216+
$catalogue->deleteMetaData('messages', array());
217+
}
218+
219+
/**
220+
* @expectedException \InvalidArgumentException
221+
*/
222+
public function testMetaDataExceptionsDomain()
223+
{
224+
$catalogue = new MessageCatalogue('en');
225+
$catalogue->deleteMetaData(array());
226+
}
227+
228+
public function testMetaDataMerge()
229+
{
230+
$cat1 = new MessageCatalogue('en');
231+
$cat1->setMetaData('a','b');
232+
$expected = array('messages'=>array('a'=>'b'));
233+
$actual = $cat1->getMetaData();
234+
$this->assertEquals($expected, $actual, 'Cat1 contains messages metadata.');
235+
236+
$cat2 = new MessageCatalogue('en');
237+
$cat2->setMetaData('b', 'c', 'domain');
238+
$expected = array('domain'=>array('b'=>'c'));
239+
$actual = $cat2->getMetaData();
240+
$this->assertEquals($expected, $actual, 'Cat2 contains domain metadata.');
241+
242+
$cat1->addCatalogue($cat2);
243+
$expected = array('messages'=>array('a'=>'b'),'domain'=>array('b'=>'c'));
244+
$actual = $cat1->getMetaData();
245+
$this->assertEquals($expected, $actual, 'Cat1 contains merged metadata.');
246+
247+
}
173248
}

0 commit comments

Comments
 (0)