diff --git a/src/WordFormatter/FormatterCombiner.php b/src/WordFormatter/FormatterCombiner.php index 512dc05..18b6865 100644 --- a/src/WordFormatter/FormatterCombiner.php +++ b/src/WordFormatter/FormatterCombiner.php @@ -14,11 +14,6 @@ final class FormatterCombiner extends ChainableFormatter */ private $wordFormatters; - /** - * @var bool Whether the result should only contain unique words. - */ - private $filterUnique; - /** * @param WordFormatter[] $wordFormatters Word formatters. * @param bool $includeUnformatted Whether the result should also include the words unformatted. @@ -26,12 +21,15 @@ final class FormatterCombiner extends ChainableFormatter */ public function __construct(array $wordFormatters, bool $includeUnformatted = true, bool $filterUnique = true) { - if ($includeUnformatted) { + if ($filterUnique) { + $formatterCombiner = new FormatterCombiner($wordFormatters, $includeUnformatted, false); + $formatterCombiner->setNext(new UniqueFilter()); + $wordFormatters = [$formatterCombiner]; + } elseif ($includeUnformatted) { $wordFormatters[] = new Unformatter(); } $this->wordFormatters = $wordFormatters; - $this->filterUnique = $filterUnique; } /** @@ -39,22 +37,6 @@ public function __construct(array $wordFormatters, bool $includeUnformatted = tr * @return Traversable The words formatted by all the word formatters in the formatter combiner. */ protected function applyCurrent(iterable $words): Traversable - { - $formatted = $this->applyFormatters($words); - - if ($this->filterUnique) { - $uniqueFilter = new UniqueFilter(); - yield from $uniqueFilter->apply($formatted); - } else { - yield from $formatted; - } - } - - /** - * @param iterable $words Words to format. - * @return Traversable The words formatted by all the word formatters in the formatter combiner. - */ - private function applyFormatters(iterable $words): Traversable { foreach ($this->wordFormatters as $wordFormatter) { yield from $wordFormatter->apply($words);