SchemaBoolAttribute::isNegativeMatch()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
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 SchemaBoolAttribute extends SchemaAttribute
11
{
12
    protected array $positiveMatches;
13
    protected array $negativeMatches;
14
15
    public function __construct(Schema $schema, string $name)
16
    {
17
        parent::__construct($schema, $name);
18
        $this->positiveMatches = [];
19
        $this->negativeMatches = [];
20
    }
21
22
    public function positives(array $matches): self
23
    {
24
        $this->positiveMatches = $matches;
25
        return $this;
26
    }
27
28
    public function negatives(array $matches): self
29
    {
30
        $this->negativeMatches = $matches;
31
        return $this;
32
    }
33
34
    protected function isNegativeMatch($value): bool
35
    {
36
        return array_reduce($this->negativeMatches, fn($carry, $current) => $carry || $current == $value ? true : false, false);
37
    }
38
39
    protected function isPositiveMatch($value): bool
40
    {
41
        return array_reduce($this->positiveMatches, fn($carry, $current) => $carry || $current == $value ? true : false, false);
42
    }
43
44
    public function parseContextual($value)
45
    {
46
        if (is_integer($value)) {
47
            return boolval($value);
48
        }
49
50
        if (is_bool($value)) {
51
            return $value;
52
        }
53
54
        if ($this->isNegativeMatch($value)) {
55
            return false;
56
        }
57
58
        if ($this->isPositiveMatch($value)) {
59
            return true;
60
        }
61
62
        throw new SchemaAttributeParseException($this, "Provided value '$value' is not a boolean");
63
    }
64
}