SchemaEnumAttribute::matchesLoose()   A
last analyzed

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
/**
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
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...
50
    }
51
}