Test Failed
Push — master ( 97fe68...3f86cd )
by Kirill
42s
created

HasFields::withoutField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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\ProvidesFields;
13
use Railt\Reflection\Contracts\Definition\Dependent\FieldDefinition;
14
15
/**
16
 * Trait HasFields
17
 */
18
trait HasFields
19
{
20
    /**
21
     * @var array|FieldDefinition[]
22
     */
23
    protected $fields = [];
24
25
    /**
26
     * @return iterable|FieldDefinition[]
27
     */
28
    public function getFields(): iterable
29
    {
30
        return \array_values($this->fields);
31
    }
32
33
    /**
34
     * @param string $name
35
     * @return bool
36
     */
37
    public function hasField(string $name): bool
38
    {
39
        return isset($this->fields[$name]);
40
    }
41
42
    /**
43
     * @param string $name
44
     * @return null|FieldDefinition
45
     */
46
    public function getField(string $name): ?FieldDefinition
47
    {
48
        return $this->fields[$name] ?? null;
49
    }
50
51
    /**
52
     * @param FieldDefinition ...$fields
53
     * @return ProvidesFields|$this
54
     */
55
    public function withField(FieldDefinition ...$fields): ProvidesFields
56
    {
57
        foreach ($fields as $field) {
58
            $this->fields[$field->getName()] = $field;
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param string|FieldDefinition ...$fields
66
     * @return ProvidesFields|$this
67
     */
68
    public function withoutField(...$fields): ProvidesFields
69
    {
70
        foreach ($fields as $field) {
71
            unset($this->fields[$this->nameOf($field)]);
72
        }
73
74
        return $this;
75
    }
76
}
77