Test Failed
Push — master ( dbe410...b8c007 )
by Kirill
02:19
created

HasInputFields::getField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Reflection\Definition\Behaviour;
11
12
use Railt\Reflection\Contracts\Definition\Behaviour\ProvidesInputFields;
13
use Railt\Reflection\Contracts\Definition\Dependent\InputFieldDefinition;
14
use Railt\Reflection\Common\Verifiable;
15
16
/**
17
 * Trait HasInputFields
18
 * @mixin ProvidesInputFields
19
 */
20
trait HasInputFields
21
{
22
    /**
23
     * @var array|InputFieldDefinition[]
24
     */
25
    protected $fields = [];
26
27
    /**
28
     * @return iterable|InputFieldDefinition[]
29
     */
30
    public function getFields(): iterable
31
    {
32
        return \array_values($this->fields);
33
    }
34
35
    /**
36
     * @param string $name
37
     * @return bool
38
     */
39
    public function hasField(string $name): bool
40
    {
41
        return isset($this->fields[$name]);
42
    }
43
44
    /**
45
     * @param string $name
46
     * @return null|InputFieldDefinition
47
     */
48
    public function getField(string $name): ?InputFieldDefinition
49
    {
50
        return $this->fields[$name] ?? null;
51
    }
52
53
    /**
54
     * @param InputFieldDefinition ...$fields
55
     * @return ProvidesInputFields|$this
56
     */
57
    public function withField(InputFieldDefinition ...$fields): ProvidesInputFields
58
    {
59
        foreach ($fields as $field) {
60
            if ($field instanceof Verifiable) {
61
                $field->verify();
62
            }
63
64
            $this->fields[$field->getName()] = $field;
65
        }
66
67
        return $this;
68
    }
69
}
70