Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| TranslationCollector | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| createList | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\Translator; |
| 3 | |
| 4 | use Apie\Core\ContextBuilders\ContextBuilderFactory; |
| 5 | use Apie\Core\Translator\Lists\TranslationStringSet; |
| 6 | |
| 7 | final class TranslationCollector |
| 8 | { |
| 9 | /** |
| 10 | * @var array<int, TranslationStringProviderInterface> |
| 11 | */ |
| 12 | private array $translationStringProviders; |
| 13 | |
| 14 | public function __construct( |
| 15 | private readonly ContextBuilderFactory $contextBuilderFactory, |
| 16 | TranslationStringProviderInterface... $translationStringProviders |
| 17 | ) { |
| 18 | $this->translationStringProviders = $translationStringProviders; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * For Symfony tagged_iterator can not be variadic |
| 23 | * |
| 24 | * @param iterable<string|int, TranslationStringProviderInterface> $translationStringProviders |
| 25 | */ |
| 26 | public static function create( |
| 27 | ContextBuilderFactory $contextBuilderFactory, |
| 28 | iterable $translationStringProviders |
| 29 | ): static { |
| 30 | return new static( |
| 31 | $contextBuilderFactory, |
| 32 | ...$translationStringProviders |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | public function createList(): TranslationStringSet |
| 37 | { |
| 38 | $collected = []; |
| 39 | $context = $this->contextBuilderFactory->createGeneralContext([ |
| 40 | 'translation_list' => true, |
| 41 | TranslationCollector::class => $this |
| 42 | ]); |
| 43 | foreach ($this->translationStringProviders as $translationStringProvider) { |
| 44 | $added = $translationStringProvider->provideStringTranslations($context)->toArray(); |
| 45 | foreach ($added as $addedTranslation) { |
| 46 | $collected[] = $addedTranslation; |
| 47 | } |
| 48 | } |
| 49 | return new TranslationStringSet($collected); |
| 50 | } |
| 51 | } |