Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Translation\Catalogue; Chris@0: Chris@17: use Symfony\Component\Translation\Exception\InvalidArgumentException; Chris@17: use Symfony\Component\Translation\Exception\LogicException; Chris@0: use Symfony\Component\Translation\MessageCatalogue; Chris@0: use Symfony\Component\Translation\MessageCatalogueInterface; Chris@0: Chris@0: /** Chris@0: * Base catalogues binary operation class. Chris@0: * Chris@0: * A catalogue binary operation performs operation on Chris@0: * source (the left argument) and target (the right argument) catalogues. Chris@0: * Chris@0: * @author Jean-François Simon Chris@0: */ Chris@0: abstract class AbstractOperation implements OperationInterface Chris@0: { Chris@0: protected $source; Chris@0: protected $target; Chris@0: protected $result; Chris@0: Chris@0: /** Chris@17: * @var array|null The domains affected by this operation Chris@0: */ Chris@0: private $domains; Chris@0: Chris@0: /** Chris@0: * This array stores 'all', 'new' and 'obsolete' messages for all valid domains. Chris@0: * Chris@0: * The data structure of this array is as follows: Chris@17: * Chris@17: * [ Chris@17: * 'domain 1' => [ Chris@17: * 'all' => [...], Chris@17: * 'new' => [...], Chris@17: * 'obsolete' => [...] Chris@17: * ], Chris@17: * 'domain 2' => [ Chris@17: * 'all' => [...], Chris@17: * 'new' => [...], Chris@17: * 'obsolete' => [...] Chris@17: * ], Chris@17: * ... Chris@17: * ] Chris@0: * Chris@0: * @var array The array that stores 'all', 'new' and 'obsolete' messages Chris@0: */ Chris@0: protected $messages; Chris@0: Chris@0: /** Chris@0: * @throws LogicException Chris@0: */ Chris@0: public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target) Chris@0: { Chris@0: if ($source->getLocale() !== $target->getLocale()) { Chris@0: throw new LogicException('Operated catalogues must belong to the same locale.'); Chris@0: } Chris@0: Chris@0: $this->source = $source; Chris@0: $this->target = $target; Chris@0: $this->result = new MessageCatalogue($source->getLocale()); Chris@17: $this->messages = []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getDomains() Chris@0: { Chris@0: if (null === $this->domains) { Chris@0: $this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains()))); Chris@0: } Chris@0: Chris@0: return $this->domains; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMessages($domain) Chris@0: { Chris@17: if (!\in_array($domain, $this->getDomains())) { Chris@0: throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain)); Chris@0: } Chris@0: Chris@0: if (!isset($this->messages[$domain]['all'])) { Chris@0: $this->processDomain($domain); Chris@0: } Chris@0: Chris@0: return $this->messages[$domain]['all']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getNewMessages($domain) Chris@0: { Chris@17: if (!\in_array($domain, $this->getDomains())) { Chris@0: throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain)); Chris@0: } Chris@0: Chris@0: if (!isset($this->messages[$domain]['new'])) { Chris@0: $this->processDomain($domain); Chris@0: } Chris@0: Chris@0: return $this->messages[$domain]['new']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getObsoleteMessages($domain) Chris@0: { Chris@17: if (!\in_array($domain, $this->getDomains())) { Chris@0: throw new InvalidArgumentException(sprintf('Invalid domain: %s.', $domain)); Chris@0: } Chris@0: Chris@0: if (!isset($this->messages[$domain]['obsolete'])) { Chris@0: $this->processDomain($domain); Chris@0: } Chris@0: Chris@0: return $this->messages[$domain]['obsolete']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getResult() Chris@0: { Chris@0: foreach ($this->getDomains() as $domain) { Chris@0: if (!isset($this->messages[$domain])) { Chris@0: $this->processDomain($domain); Chris@0: } Chris@0: } Chris@0: Chris@0: return $this->result; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Performs operation on source and target catalogues for the given domain and Chris@0: * stores the results. Chris@0: * Chris@0: * @param string $domain The domain which the operation will be performed for Chris@0: */ Chris@0: abstract protected function processDomain($domain); Chris@0: }