/
ChainSegmentFactory.php
42 lines (34 loc) · 1.15 KB
/
ChainSegmentFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
namespace Pheature\Core\Toggle\Read;
use Pheature\Core\Toggle\Exception\InvalidSegmentTypeGiven;
final class ChainSegmentFactory implements SegmentFactory
{
/** @var SegmentFactory[] */
private array $segmentFactories;
public function __construct(SegmentFactory ...$segmentFactories)
{
$this->segmentFactories = $segmentFactories;
}
/**
* @param string $segmentId
* @param string $segmentType
* @param array<string, mixed> $criteria
* @return Segment
*/
public function create(string $segmentId, string $segmentType, array $criteria): Segment
{
foreach ($this->segmentFactories as $segmentFactory) {
if (in_array($segmentType, $segmentFactory->types(), true)) {
return $segmentFactory->create($segmentId, $segmentType, $criteria);
}
}
throw InvalidSegmentTypeGiven::withType($segmentType);
}
public function types(): array
{
return array_merge(
...array_map(static fn(SegmentFactory $segmentFactory) => $segmentFactory->types(), $this->segmentFactories)
);
}
}