Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
10 / 10 |
|
75.00% |
6 / 8 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| CoercerRegistry | |
100.00% |
12 / 12 |
|
100.00% |
10 / 10 |
|
75.00% |
6 / 8 |
|
100.00% |
5 / 5 |
7.77 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withDefaults | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| with | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| withMany | |
100.00% |
4 / 4 |
|
100.00% |
4 / 4 |
|
33.33% |
1 / 3 |
|
100.00% |
1 / 1 |
3.19 | |||
| get | |
100.00% |
3 / 3 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celemas\Sire; |
| 6 | |
| 7 | use Override; |
| 8 | |
| 9 | /** @api */ |
| 10 | final class CoercerRegistry implements Contract\CoercerRegistry |
| 11 | { |
| 12 | /** @param array<string, Contract\Coercer> $coercers */ |
| 13 | public function __construct( |
| 14 | private array $coercers = [], |
| 15 | private ?Contract\CoercerRegistry $fallback = null, |
| 16 | ) {} |
| 17 | |
| 18 | public static function withDefaults(): self |
| 19 | { |
| 20 | return new self([], new DefaultCoercers()); |
| 21 | } |
| 22 | |
| 23 | public function with(string $name, Contract\Coercer $coercer): self |
| 24 | { |
| 25 | $coercers = $this->coercers; |
| 26 | $coercers[$name] = $coercer; |
| 27 | |
| 28 | return new self($coercers, $this->fallback); |
| 29 | } |
| 30 | |
| 31 | /** @param array<string, Contract\Coercer> $coercers */ |
| 32 | public function withMany(array $coercers): self |
| 33 | { |
| 34 | $result = $this; |
| 35 | |
| 36 | foreach ($coercers as $name => $coercer) { |
| 37 | $result = $result->with($name, $coercer); |
| 38 | } |
| 39 | |
| 40 | return $result; |
| 41 | } |
| 42 | |
| 43 | #[Override] |
| 44 | public function get(string $name): ?Contract\Coercer |
| 45 | { |
| 46 | if (array_key_exists($name, $this->coercers)) { |
| 47 | return $this->coercers[$name]; |
| 48 | } |
| 49 | |
| 50 | return $this->fallback?->get($name); |
| 51 | } |
| 52 | } |