Completed
Pull Request — master (#4)
by
unknown
06:18
created

ValidationState::offsetExists()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Albert221\Validation;
6
7
use ArrayAccess;
8
use OutOfBoundsException;
9
use OutOfRangeException;
10
11
class ValidationState implements ArrayAccess
12
{
13
    const FLAT = 0;
14
    const GROUPED = 1;
15
16
    /**
17
     * @var Verdicts
18
     */
19
    private $verdicts;
20
21
    /**
22
     * ValidationState constructor.
23
     *
24
     * @param array $verdicts
25
     */
26
    public function __construct(array $verdicts)
27
    {
28
        $this->verdicts = new Verdicts($verdicts);
29
    }
30
31
    /**
32
     * @return bool
33
     */
34
    public function isValid(): bool
35
    {
36
        return count($this->verdicts->failing()) === 0;
37
    }
38
39
    /**
40
     * @param int $strategy
0 ignored issues
show
Bug introduced by
There is no parameter named $strategy. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
41
     *
42
     * @return Verdict[]
43
     */
44
    public function all(): array
45
    {
46
        return $this->verdicts->toArray();
47
    }
48
49
    /**
50
     * @param string $name
51
     *
52
     * @return bool
53
     */
54
    public function isFieldValid(string $name): bool
55
    {
56
        $verdictsForField = $this->verdicts->forField($name);
57
        if ($verdictsForField->count() === 0) {
58
            throw new \InvalidArgumentException(sprintf(
59
                'Field "%s" does not exist in the validation state.',
60
                $name
61
            ));
62
        }
63
64
        return $verdictsForField->failing()->count === 0;
0 ignored issues
show
Bug introduced by
The property count does not seem to exist in Albert221\Validation\Verdicts.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
65
    }
66
67
    /**
68
     * @param string $name
69
     *
70
     * @return Verdict[]
71
     */
72
    public function field(string $name): array
73
    {
74
        return $this->verdicts->forField($name)->toArray();
75
    }
76
77
    /**
78
     * @param int|string $offset
79
     *
80
     * @return bool
81
     */
82
    public function offsetExists($offset): bool
83
    {
84
        if (is_int($offset)) {
85
            return isset($this->verdicts->toArray()[$offset]);
86
        }
87
88
        if (!is_string($offset)) {
89
            throw new OutOfRangeException(sprintf(
90
                'Offset should be an integer index for FLAT strategy or string for GROUPED, %s given.',
91
                is_scalar($offset) ? gettype($offset): get_class($offset)
92
            ));
93
        }
94
95
        return $this->verdicts->forField($offset)->count() > 0;
96
    }
97
98
    /**
99
     * @param int|string $offset
100
     *
101
     * @return array
102
     */
103
    public function offsetGet($offset): array
104
    {
105
        if (!$this->offsetExists($offset)) {
106
            throw new OutOfBoundsException(sprintf(
107
                'Item with offset "%s" does not exist.',
108
                $offset
109
            ));
110
        }
111
112
        if (is_int($offset)) {
113
            return $this->all()[$offset];
114
        }
115
116
        return $this->field($offset);
117
    }
118
119
    public function offsetSet($offset, $value)
120
    {
121
        // You cannot set any value.
122
    }
123
124
    public function offsetUnset($offset)
125
    {
126
        // You cannot unset any value.
127
    }
128
}
129