Error   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 6
eloc 9
dl 0
loc 53
ccs 6
cts 10
cp 0.6
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCode() 0 3 1
A getIterator() 0 3 1
A __construct() 0 6 1
A getMessage() 0 3 1
A toArray() 0 6 1
A getField() 0 3 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