Total Complexity | 7 |
Total Lines | 46 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
15 | final class Structures |
||
16 | { |
||
17 | private static $defaults; |
||
18 | private $structures; |
||
19 | |||
20 | 26 | public function __construct(string ...$structures) |
|
21 | { |
||
22 | 26 | $structures = Stream::of('string', ...$structures); |
|
23 | |||
24 | 26 | if ($structures->size() === 0) { |
|
25 | 23 | $structures = self::defaults(); |
|
26 | } |
||
27 | |||
28 | 26 | $structures->foreach(static function(string $structure): void { |
|
29 | 26 | $refl = new \ReflectionClass($structure); |
|
30 | |||
31 | 26 | if (!$refl->implementsInterface(Structure::class)) { |
|
32 | 1 | throw new DomainException($structure); |
|
33 | } |
||
34 | 26 | }); |
|
35 | |||
36 | 25 | $this->structures = $structures; |
|
37 | 25 | } |
|
38 | |||
39 | 19 | public function build(array $schema, Properties $properties): Structure |
|
40 | { |
||
41 | 19 | foreach ($this->structures as $structure) { |
|
42 | try { |
||
43 | 19 | return [$structure, 'build']($schema, $this, $properties); |
|
44 | 19 | } catch (SchemaNotParseable $e) { |
|
45 | //pass |
||
46 | } |
||
47 | } |
||
48 | |||
49 | 4 | throw new SchemaNotParseable('', 0, $e ?? null); |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return StreamInterface<Structure> |
||
54 | */ |
||
55 | 24 | public static function defaults(): StreamInterface |
|
61 | ); |
||
62 | } |
||
63 | } |
||
64 |