Skip to content

[MessageCatalogue] Add Metadata to MessageCatalogue #6349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 15, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion src/Symfony/Component/Translation/MessageCatalogue.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
*
* @api
*/
class MessageCatalogue implements MessageCatalogueInterface
class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
{
private $messages = array();
private $metadata = array();
private $locale;
private $resources;
private $fallbackCatalogue;
Expand Down Expand Up @@ -175,6 +176,9 @@ public function addCatalogue(MessageCatalogueInterface $catalogue)
foreach ($catalogue->getResources() as $resource) {
$this->addResource($resource);
}

$metadata = $catalogue->getMetadata('', '');
$this->addMetadata($metadata);
}

/**
Expand Down Expand Up @@ -231,4 +235,62 @@ public function addResource(ResourceInterface $resource)
{
$this->resources[] = $resource;
}

/**
* {@inheritdoc}
*/
public function getMetadata($key = '', $domain = 'messages')
{
if (empty($domain)) {
return $this->metadata;
}

if (isset($this->metadata[$domain])) {
if (!empty($key)) {
if (isset($this->metadata[$domain][$key])) {
return $this->metadata[$domain][$key];
}
} else {
return $this->metadata[$domain];
}
}
}

/**
* {@inheritdoc}
*/
public function setMetadata($key, $value, $domain = 'messages')
{
$this->metadata[$domain][$key] = $value;
}

/**
* {@inheritdoc}
*/
public function deleteMetadata($key = '', $domain = 'messages')
{
if (empty($domain)) {
$this->metadata = array();
}

if (empty($key)) {
unset($this->metadata[$domain]);
}

unset($this->metadata[$domain][$key]);
}

/**
* Adds current values with the new values.
*
* @param array $values Values to add
*/
private function addMetadata(array $values)
{
foreach ($values as $domain => $keys) {
foreach ($keys as $key => $value) {
$this->setMetadata($key, $value, $domain);
}
}
}
}
47 changes: 47 additions & 0 deletions src/Symfony/Component/Translation/MetadataAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation;

use Symfony\Component\Config\Resource\ResourceInterface;

/**
* MetadataAwareInterface.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface MetadataAwareInterface
{
/**
* Gets meta data for given domain and key.
*
* @param string $domain The domain name
* @param string $key Key
*/
public function getMetadata($key = '', $domain = 'messages');

/**
* Adds meta data to a message domain.
*
* @param string $key Key
* @param string|array $value Value
* @param string $domain The domain name
*/
public function setMetadata($key, $value, $domain = 'messages');

/**
* Deletes meta data for given key and domain.
*
* @param string $domain The domain name
* @param string $key Key
*/
public function deleteMetadata($key = '', $domain = 'messages');
}
39 changes: 39 additions & 0 deletions src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,43 @@ public function testGetAddResource()

$this->assertEquals(array($r, $r1), $catalogue->getResources());
}

public function testMetadataDelete()
{
$catalogue = new MessageCatalogue('en');
$this->assertEquals(array(), $catalogue->getMetadata('', ''), 'Metadata is empty');
$catalogue->deleteMetadata('key', 'messages');
$catalogue->deleteMetadata('', 'messages');
$catalogue->deleteMetadata();
}

public function testMetadataSetGetDelete()
{
$catalogue = new MessageCatalogue('en');
$catalogue->setMetadata('key', 'value');
$this->assertEquals('value', $catalogue->getMetadata('key', 'messages'), "Metadata 'key' = 'value'");

$catalogue->setMetadata('key2', array());
$this->assertEquals(array(), $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 is array');

$catalogue->deleteMetadata('key2', 'messages');
$this->assertEquals(null, $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');

$catalogue->deleteMetadata('key2', 'domain');
$this->assertEquals(null, $catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
}

public function testMetadataMerge()
{
$cat1 = new MessageCatalogue('en');
$cat1->setMetadata('a', 'b');
$this->assertEquals(array('messages' => array('a' => 'b')), $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');

$cat2 = new MessageCatalogue('en');
$cat2->setMetadata('b', 'c', 'domain');
$this->assertEquals(array('domain' => array('b' => 'c')), $cat2->getMetadata('', ''), 'Cat2 contains domain metadata.');

$cat1->addCatalogue($cat2);
$this->assertEquals(array('messages' => array('a' => 'b'), 'domain' => array('b' => 'c')), $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
}
}