1 | <?php |
||
2 | |||
3 | namespace Ipag\Sdk\Model\Schema; |
||
4 | |||
5 | use Ipag\Sdk\Model\Schema\Exception\SchemaAttributeParseException; |
||
6 | |||
7 | /** |
||
8 | * @codeCoverageIgnore |
||
9 | */ |
||
10 | class SchemaEnumAttribute extends SchemaAttribute |
||
11 | { |
||
12 | protected array $values; |
||
13 | |||
14 | public function __construct(Schema $schema, string $name) |
||
15 | { |
||
16 | parent::__construct($schema, $name); |
||
17 | $this->values = []; |
||
18 | } |
||
19 | |||
20 | public function values(array $values): self |
||
21 | { |
||
22 | $this->values = $values; |
||
23 | return $this; |
||
24 | } |
||
25 | |||
26 | private function matchesLoose($value) |
||
27 | { |
||
28 | return array_reduce($this->values, fn($x, $y) => $x ?? ($y == $value ? $y : null), null); |
||
29 | } |
||
30 | |||
31 | public function parseContextual($value) |
||
32 | { |
||
33 | $value = $this->matchesLoose($value); |
||
34 | if (!is_null($value)) { |
||
35 | return $value; |
||
36 | } |
||
37 | |||
38 | $many = $this->getValuesVerbose(); |
||
39 | throw new SchemaAttributeParseException($this, "Provided value is not one of {$many}"); |
||
40 | } |
||
41 | |||
42 | private function getValuesVerbose(): string |
||
43 | { |
||
44 | return implode(', ', $this->values); |
||
45 | } |
||
46 | |||
47 | public static function from(Schema $schema, string $name): self |
||
48 | { |
||
49 | return parent::from($schema, $name); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
50 | } |
||
51 | } |