From 4f5b44bd3bd490309eb2ba7b44df4769816ba729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sat, 3 Aug 2013 19:26:54 +0200 Subject: twig implementation --- .../Translation/Catalogue/AbstractOperation.php | 146 +++++++++++++++++++++ .../Translation/Catalogue/DiffOperation.php | 49 +++++++ .../Translation/Catalogue/MergeOperation.php | 45 +++++++ .../Translation/Catalogue/OperationInterface.php | 63 +++++++++ 4 files changed, 303 insertions(+) create mode 100644 vendor/symfony/translation/Symfony/Component/Translation/Catalogue/AbstractOperation.php create mode 100644 vendor/symfony/translation/Symfony/Component/Translation/Catalogue/DiffOperation.php create mode 100644 vendor/symfony/translation/Symfony/Component/Translation/Catalogue/MergeOperation.php create mode 100644 vendor/symfony/translation/Symfony/Component/Translation/Catalogue/OperationInterface.php (limited to 'vendor/symfony/translation/Symfony/Component/Translation/Catalogue') diff --git a/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/AbstractOperation.php b/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/AbstractOperation.php new file mode 100644 index 00000000..062056b7 --- /dev/null +++ b/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/AbstractOperation.php @@ -0,0 +1,146 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Catalogue; + +use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Translation\MessageCatalogueInterface; + +/** + * Base catalogues binary operation class. + * + * @author Jean-François Simon + */ +abstract class AbstractOperation implements OperationInterface +{ + /** + * @var MessageCatalogueInterface + */ + protected $source; + + /** + * @var MessageCatalogueInterface + */ + protected $target; + + /** + * @var MessageCatalogue + */ + protected $result; + + /** + * @var null|array + */ + private $domains; + + /** + * @var array + */ + protected $messages; + + /** + * @param MessageCatalogueInterface $source + * @param MessageCatalogueInterface $target + * + * @throws \LogicException + */ + public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target) + { + if ($source->getLocale() !== $target->getLocale()) { + throw new \LogicException('Operated catalogues must belong to the same locale.'); + } + + $this->source = $source; + $this->target = $target; + $this->result = new MessageCatalogue($source->getLocale()); + $this->domains = null; + $this->messages = array(); + } + + /** + * {@inheritdoc} + */ + public function getDomains() + { + if (null === $this->domains) { + $this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains()))); + } + + return $this->domains; + } + + /** + * {@inheritdoc} + */ + public function getMessages($domain) + { + if (!in_array($domain, $this->getDomains())) { + throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain)); + } + + if (!isset($this->messages[$domain]['all'])) { + $this->processDomain($domain); + } + + return $this->messages[$domain]['all']; + } + + /** + * {@inheritdoc} + */ + public function getNewMessages($domain) + { + if (!in_array($domain, $this->getDomains())) { + throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain)); + } + + if (!isset($this->messages[$domain]['new'])) { + $this->processDomain($domain); + } + + return $this->messages[$domain]['new']; + } + + /** + * {@inheritdoc} + */ + public function getObsoleteMessages($domain) + { + if (!in_array($domain, $this->getDomains())) { + throw new \InvalidArgumentException(sprintf('Invalid domain: %s.', $domain)); + } + + if (!isset($this->messages[$domain]['obsolete'])) { + $this->processDomain($domain); + } + + return $this->messages[$domain]['obsolete']; + } + + /** + * {@inheritdoc} + */ + public function getResult() + { + foreach ($this->getDomains() as $domain) { + if (!isset($this->messages[$domain])) { + $this->processDomain($domain); + } + } + + return $this->result; + } + + /** + * @param string $domain + */ + abstract protected function processDomain($domain); +} diff --git a/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/DiffOperation.php b/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/DiffOperation.php new file mode 100644 index 00000000..1672d121 --- /dev/null +++ b/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/DiffOperation.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Catalogue; + +/** + * Diff operation between two catalogues. + * + * @author Jean-François Simon + */ +class DiffOperation extends AbstractOperation +{ + /** + * {@inheritdoc} + */ + protected function processDomain($domain) + { + $this->messages[$domain] = array( + 'all' => array(), + 'new' => array(), + 'obsolete' => array(), + ); + + foreach ($this->source->all($domain) as $id => $message) { + if ($this->target->has($id, $domain)) { + $this->messages[$domain]['all'][$id] = $message; + $this->result->add(array($id => $message), $domain); + } else { + $this->messages[$domain]['obsolete'][$id] = $message; + } + } + + foreach ($this->target->all($domain) as $id => $message) { + if (!$this->source->has($id, $domain)) { + $this->messages[$domain]['all'][$id] = $message; + $this->messages[$domain]['new'][$id] = $message; + $this->result->add(array($id => $message), $domain); + } + } + } +} diff --git a/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/MergeOperation.php b/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/MergeOperation.php new file mode 100644 index 00000000..0052363e --- /dev/null +++ b/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/MergeOperation.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Catalogue; + +/** + * Merge operation between two catalogues. + * + * @author Jean-François Simon + */ +class MergeOperation extends AbstractOperation +{ + /** + * {@inheritdoc} + */ + protected function processDomain($domain) + { + $this->messages[$domain] = array( + 'all' => array(), + 'new' => array(), + 'obsolete' => array(), + ); + + foreach ($this->source->all($domain) as $id => $message) { + $this->messages[$domain]['all'][$id] = $message; + $this->result->add(array($id => $message), $domain); + } + + foreach ($this->target->all($domain) as $id => $message) { + if (!$this->source->has($id, $domain)) { + $this->messages[$domain]['all'][$id] = $message; + $this->messages[$domain]['new'][$id] = $message; + $this->result->add(array($id => $message), $domain); + } + } + } +} diff --git a/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/OperationInterface.php b/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/OperationInterface.php new file mode 100644 index 00000000..d72378a3 --- /dev/null +++ b/vendor/symfony/translation/Symfony/Component/Translation/Catalogue/OperationInterface.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Catalogue; + +use Symfony\Component\Translation\MessageCatalogueInterface; + +/** + * Represents an operation on catalogue(s). + * + * @author Jean-François Simon + */ +interface OperationInterface +{ + /** + * Returns domains affected by operation. + * + * @return array + */ + public function getDomains(); + + /** + * Returns all valid messages after operation. + * + * @param string $domain + * + * @return array + */ + public function getMessages($domain); + + /** + * Returns new messages after operation. + * + * @param string $domain + * + * @return array + */ + public function getNewMessages($domain); + + /** + * Returns obsolete messages after operation. + * + * @param string $domain + * + * @return array + */ + public function getObsoleteMessages($domain); + + /** + * Returns resulting catalogue. + * + * @return MessageCatalogueInterface + */ + public function getResult(); +} -- cgit v1.2.3