| Total Complexity | 6 |
| Total Lines | 44 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 13 | final class Types |
||
| 14 | { |
||
| 15 | private static $defaults; |
||
| 16 | private $types; |
||
| 17 | |||
| 18 | 7 | public function __construct(string ...$types) |
|
| 19 | { |
||
| 20 | 7 | $types = Stream::of('string', ...$types); |
|
| 21 | |||
| 22 | 7 | if ($types->size() === 0) { |
|
| 23 | 6 | $types = self::defaults(); |
|
| 24 | } |
||
| 25 | |||
| 26 | 7 | $this->types = $types; |
|
| 27 | 7 | } |
|
| 28 | |||
| 29 | 7 | public function load(Str $value): Type |
|
| 30 | { |
||
| 31 | 7 | foreach ($this->types as $type) { |
|
| 32 | try { |
||
| 33 | 7 | $build = $type.'::fromString'; |
|
| 34 | |||
| 35 | 7 | return $build($value); |
|
| 36 | 6 | } catch (ValueNotSupported $e) { |
|
| 37 | //pass |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | 1 | throw new ValueNotSupported((string) $value); |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return StreamInterface<string> |
||
| 46 | */ |
||
| 47 | 7 | public static function defaults(): StreamInterface |
|
| 57 | ); |
||
| 58 | } |
||
| 59 | } |
||
| 60 |