Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
40 / 40 |
|
100.00% |
21 / 21 |
|
100.00% |
18 / 18 |
|
100.00% |
15 / 15 |
CRAP | |
100.00% |
1 / 1 |
| Config | |
100.00% |
40 / 40 |
|
100.00% |
21 / 21 |
|
100.00% |
18 / 18 |
|
100.00% |
15 / 15 |
18 | |
100.00% |
1 / 1 |
| asList | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| extra | |
100.00% |
7 / 7 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| coercionMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| rule | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| rules | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| coercer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| coercers | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| message | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| messages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| ruleParser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| definition | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| messageFormatter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolvedRuleRegistry | |
100.00% |
4 / 4 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| resolvedCoercerRegistry | |
100.00% |
4 / 4 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| resolvedRuleParser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celemas\Sire; |
| 6 | |
| 7 | use Closure; |
| 8 | use ValueError; |
| 9 | |
| 10 | /** @internal */ |
| 11 | final class Config |
| 12 | { |
| 13 | private bool $list = false; |
| 14 | private Extra $extra = Extra::Ignore; |
| 15 | private CoercionMode $coercionMode = CoercionMode::Coerce; |
| 16 | /** @var array<string, string> */ |
| 17 | private array $messages = []; |
| 18 | private ?Contract\RuleRegistry $ruleRegistry = null; |
| 19 | /** @var array<string, Contract\Rule> */ |
| 20 | private array $rules = []; |
| 21 | private ?Contract\CoercerRegistry $coercerRegistry = null; |
| 22 | /** @var array<string, Contract\Coercer> */ |
| 23 | private array $coercers = []; |
| 24 | private ?Contract\RuleParser $ruleParser = null; |
| 25 | |
| 26 | public function asList(bool $list = true): void |
| 27 | { |
| 28 | $this->list = $list; |
| 29 | } |
| 30 | |
| 31 | public function extra(Extra|string $extra): void |
| 32 | { |
| 33 | if ($extra instanceof Extra) { |
| 34 | $this->extra = $extra; |
| 35 | |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $this->extra = Extra::tryFrom($extra) ?? throw new ValueError(sprintf( |
| 40 | 'Invalid extra mode "%s"', |
| 41 | $extra, |
| 42 | )); |
| 43 | } |
| 44 | |
| 45 | public function coercionMode(CoercionMode $mode): void |
| 46 | { |
| 47 | $this->coercionMode = $mode; |
| 48 | } |
| 49 | |
| 50 | public function rule(string $name, Contract\Rule $rule): void |
| 51 | { |
| 52 | $this->rules[$name] = $rule; |
| 53 | } |
| 54 | |
| 55 | public function rules(Contract\RuleRegistry $registry): void |
| 56 | { |
| 57 | $this->ruleRegistry = $registry; |
| 58 | $this->rules = []; |
| 59 | } |
| 60 | |
| 61 | public function coercer(string $name, Contract\Coercer $coercer): void |
| 62 | { |
| 63 | $this->coercers[$name] = $coercer; |
| 64 | } |
| 65 | |
| 66 | public function coercers(Contract\CoercerRegistry $registry): void |
| 67 | { |
| 68 | $this->coercerRegistry = $registry; |
| 69 | $this->coercers = []; |
| 70 | } |
| 71 | |
| 72 | public function message(string $key, string $message): void |
| 73 | { |
| 74 | $this->messages[$key] = $message; |
| 75 | } |
| 76 | |
| 77 | /** @param array<string, string> $messages */ |
| 78 | public function messages(array $messages): void |
| 79 | { |
| 80 | $this->messages = array_replace($this->messages, $messages); |
| 81 | } |
| 82 | |
| 83 | public function ruleParser(Contract\RuleParser $parser): void |
| 84 | { |
| 85 | $this->ruleParser = $parser; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param array<string, Field> $fields |
| 90 | * @param list<Closure(array<array-key, mixed>): array<array-key, mixed>> $prepareCallbacks |
| 91 | * @param list<Closure(Review): void> $reviewCallbacks |
| 92 | */ |
| 93 | public function definition( |
| 94 | array $fields, |
| 95 | array $prepareCallbacks, |
| 96 | array $reviewCallbacks, |
| 97 | ): ShapeDefinition { |
| 98 | return new ShapeDefinition( |
| 99 | $this->list, |
| 100 | $this->extra, |
| 101 | $this->coercionMode, |
| 102 | $fields, |
| 103 | $this->resolvedRuleRegistry(), |
| 104 | $this->resolvedCoercerRegistry(), |
| 105 | $this->resolvedRuleParser(), |
| 106 | $this->messageFormatter(), |
| 107 | $prepareCallbacks, |
| 108 | $reviewCallbacks, |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | private function messageFormatter(): MessageFormatter |
| 113 | { |
| 114 | return new MessageFormatter($this->messages); |
| 115 | } |
| 116 | |
| 117 | private function resolvedRuleRegistry(): Contract\RuleRegistry |
| 118 | { |
| 119 | $this->ruleRegistry ??= RuleRegistry::withDefaults(); |
| 120 | |
| 121 | if ($this->rules === []) { |
| 122 | return $this->ruleRegistry; |
| 123 | } |
| 124 | |
| 125 | return new RuleRegistry($this->rules, $this->ruleRegistry); |
| 126 | } |
| 127 | |
| 128 | private function resolvedCoercerRegistry(): Contract\CoercerRegistry |
| 129 | { |
| 130 | $this->coercerRegistry ??= CoercerRegistry::withDefaults(); |
| 131 | |
| 132 | if ($this->coercers === []) { |
| 133 | return $this->coercerRegistry; |
| 134 | } |
| 135 | |
| 136 | return new CoercerRegistry($this->coercers, $this->coercerRegistry); |
| 137 | } |
| 138 | |
| 139 | private function resolvedRuleParser(): Contract\RuleParser |
| 140 | { |
| 141 | return $this->ruleParser ??= new RuleParser(); |
| 142 | } |
| 143 | } |