Error::getCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace kalanis\Restful\Validation;
4
5
6
use ArrayIterator;
7
use IteratorAggregate;
8
use Traversable;
9
10
11
/**
12
 * Validation error caret
13
 * @package kalanis\Restful\Validation
14
 * @--implements IteratorAggregate<int, array<string, string|int>>
15
 */
16 1
class Error implements IteratorAggregate
17
{
18
19 1
    public function __construct(
20
        private readonly string $field,
21
        private readonly string $message,
22
        private readonly int    $code,
23
    )
24
    {
25 1
    }
26
27
    /**
28
     * Get error code
29
     */
30
    public function getCode(): int
31
    {
32 1
        return $this->code;
33
    }
34
35
    /**
36
     * Get error field name
37
     */
38
    public function getField(): string
39
    {
40 1
        return $this->field;
41
    }
42
43
    /**
44
     * Get validation error message
45
     */
46
    public function getMessage(): string
47
    {
48 1
        return $this->message;
49
    }
50
51
    /**
52
     * Iterate through error data to convert it
53
     */
54
    public function getIterator(): Traversable
55
    {
56
        return new ArrayIterator($this->toArray());
57
    }
58
59
    /**
60
     * Converts error caret to an array
61
     * @return array<string, string|int>
62
     */
63
    public function toArray(): array
64
    {
65
        return [
66
            'field' => $this->field,
67
            'message' => $this->message,
68
            'code' => $this->code,
69
        ];
70
    }
71
}
72