Lines
94.11%
16 / 17
Methods
66.66%
2 / 3
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| fromArray | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| filterByCityNames | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 4 | ||
| 10 | readonly class ScraperCollection | |
| 11 | { | |
| 12 | /** | |
| 13 | * @var CityScraper[] | |
| 14 | */ | |
| 15 | private array $scrapers; | |
| 16 | ||
| 17 | public function __construct(CityScraper ...$scrapers) | |
| 18 | { | |
| 19 | $this->scrapers = $scrapers; | |
| 20 | } | |
| 21 | ||
| 22 | /** | |
| 23 | * @param CityScraper[] $scrapers | |
| 24 | */ | |
| 25 | public static function fromArray(array $scrapers): self | |
| 26 | { | |
| 27 | return new self(...$scrapers); | |
| 28 | } | |
| 29 | ||
| 30 | /** | |
| 31 | * @param string[] $names | |
| 32 | * | |
| 33 | * @return CityScraper[] | |
| 34 | */ | |
| 35 | public function filterByCityNames(array $names): array | |
| 36 | { | |
| 37 | if ($names === []) { | |
| 38 | return $this->scrapers; | |
| 39 | } | |
| 40 | ||
| 41 | $supportedNames = array_map( | |
| 42 | static fn ($scraper) => $scraper->getCityName(), | |
| 43 | $this->scrapers, | |
| 44 | ); | |
| 45 | foreach ($names as $name) { | |
| 46 | if (!in_array($name, $supportedNames, true)) { | |
| 47 | throw new InvalidArgumentException("unsupported city name: {$name}"); | |
| 48 | } | |
| 49 | } | |
| 50 | ||
| 51 | return array_values( | |
| 52 | array_filter( | |
| 53 | $this->scrapers, | |
| 54 | static fn ($scraper) => in_array($scraper->getCityName(), $names, true), | |
| 55 | ) | |
| 56 | ); | |
| 57 | } | |
| 58 | } |