Passed
Push — main ( 50ef61...1ecca1 )
by Breno
01:36
created

BelongsToField   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A belongsToField() 0 3 1
A getField() 0 3 1
A newEmptyResult() 0 3 2
A field() 0 5 1
A setField() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation;
5
6
trait BelongsToField
7
{
8
    private ?string $field = null;
9
10
    public function getField(): ?string
11
    {
12
        return $this->field;
13
    }
14
15
    public function field(?string $field): static
16
    {
17
        $instance = clone $this;
18
        $instance->setField($field);
19
        return $instance;
20
    }
21
22
    private function setField(?string $field): void
23
    {
24
        $this->field = $field;
25
    }
26
27
    public function belongsToField(): bool
28
    {
29
        return $this->field !== null;
30
    }
31
32
    private function newEmptyResult(): ValidationResult
33
    {
34
        return $this->belongsToField() ? ValidationResult::of($this->getField()) : ValidationResult::ok();
35
    }
36
}
37