[Translator] fix metadata

- fix '0' problem
- only access metadata when implementing interface
- refactor code
- clarify phpdoc
This commit is contained in:
Tobias Schultze 2013-02-14 10:33:24 +01:00
parent 3b71000a6d
commit 09920322ea
2 changed files with 33 additions and 22 deletions

View File

@ -176,8 +176,10 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
$this->addResource($resource);
}
$metadata = $catalogue->getMetadata('', '');
$this->addMetadata($metadata);
if ($catalogue instanceof MetadataAwareInterface) {
$metadata = $catalogue->getMetadata('', '');
$this->addMetadata($metadata);
}
}
/**
@ -238,19 +240,21 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
*/
public function getMetadata($key = '', $domain = 'messages')
{
if (empty($domain)) {
if ('' == $domain) {
return $this->metadata;
}
if (isset($this->metadata[$domain])) {
if (!empty($key)) {
if (isset($this->metadata[$domain][$key])) {
return $this->metadata[$domain][$key];
}
} else {
if ('' == $key) {
return $this->metadata[$domain];
}
if (isset($this->metadata[$domain][$key])) {
return $this->metadata[$domain][$key];
}
}
return null;
}
/**
@ -266,15 +270,13 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
*/
public function deleteMetadata($key = '', $domain = 'messages')
{
if (empty($domain)) {
if ('' == $domain) {
$this->metadata = array();
}
if (empty($key)) {
} elseif ('' == $key) {
unset($this->metadata[$domain]);
} else {
unset($this->metadata[$domain][$key]);
}
unset($this->metadata[$domain][$key]);
}
/**

View File

@ -19,27 +19,36 @@ namespace Symfony\Component\Translation;
interface MetadataAwareInterface
{
/**
* Gets meta data for given domain and key.
* Gets metadata for the given domain and key.
*
* Passing an empty domain will return an array with all metadata indexed by
* domain and then by key. Passing an empty key will return an array with all
* metadata for the given domain.
*
* @param string $domain The domain name
* @param string $key Key
* @param string $key The key
*
* @return mixed The value that was set or an array with the domains/keys or null
*/
public function getMetadata($key = '', $domain = 'messages');
/**
* Adds meta data to a message domain.
* Adds metadata to a message domain.
*
* @param string $key Key
* @param string|array $value Value
* @param string $domain The domain name
* @param string $key The key
* @param mixed $value The value
* @param string $domain The domain name
*/
public function setMetadata($key, $value, $domain = 'messages');
/**
* Deletes meta data for given key and domain.
* Deletes metadata for the given key and domain.
*
* Passing an empty domain will delete all metadata. Passing an empty key will
* delete all metadata for the given domain.
*
* @param string $domain The domain name
* @param string $key Key
* @param string $key The key
*/
public function deleteMetadata($key = '', $domain = 'messages');
}