Passed
Push — main ( aa2ba1...352c83 )
by Breno
01:51
created

MaybeBelongsToField::getField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation;
5
6
use BrenoRoosevelt\Validation\Rules\NotEmpty;
7
8
trait MaybeBelongsToField
9
{
10
    private ?string $field = null;
11
12
    public function getField(): ?string
13
    {
14
        return $this->field;
15
    }
16
17
    /**
18
     * @throws ValidationException
19
     */
20
    public function setField(?string $field): static
21
    {
22
        if ($field !== null) {
23
            (new NotEmpty('When provided, the field cannot be left blank'))->validateOrFail($field);
24
        }
25
26
        $this->field = $field;
27
        return $this;
28
    }
29
30
    public function belongsToField(): bool
31
    {
32
        return $this->field !== null;
33
    }
34
35
    private function newEmptyValidationResult(): ValidationResult|ValidationResultByField
36
    {
37
        return
38
            $this->belongsToField() ?
39
                new ValidationResultByField($this->getField()) :
40
                ValidationResult::everythingIsOk();
41
    }
42
}
43