Passed
Branch master (e4e723)
by Lucas
02:36
created

SchemaEnumAttribute::matchesStrict()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Ipag\Sdk\Model\Schema;
4
5
use Ipag\Sdk\Model\Schema\Exception\SchemaAttributeParseException;
6
7
class SchemaEnumAttribute extends SchemaAttribute
8
{
9
    protected array $values;
10
11
    public function __construct(Schema $schema, string $name)
12
    {
13
        parent::__construct($schema, $name);
14
        $this->values = [];
15
    }
16
17
    public function values(array $values): self
18
    {
19
        $this->values = $values;
20
        return $this;
21
    }
22
23
    private function matchesLoose($value)
24
    {
25
        return array_reduce($this->values, fn($x, $y) => $x ?? ($y == $value ? $y : null), null);
26
    }
27
28
    public function parseContextual($value)
29
    {
30
        $value = $this->matchesLoose($value);
31
        if (!is_null($value)) {
32
            return $value;
33
        }
34
35
        $many = $this->getValuesVerbose();
36
        throw new SchemaAttributeParseException($this, "Provided value is not one of {$many}");
37
    }
38
39
    private function getValuesVerbose(): string
40
    {
41
        return implode(', ', $this->values);
42
    }
43
44
    public static function from(Schema $schema, string $name): self
45
    {
46
        return parent::from($schema, $name);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::from($schema, $name) returns the type Ipag\Sdk\Model\Schema\SchemaAttribute which includes types incompatible with the type-hinted return Ipag\Sdk\Model\Schema\SchemaEnumAttribute.
Loading history...
47
    }
48
}