Completed
Push — 0.3 ( da43ab...625b56 )
by Ben
96:17 queued 52:29
created

Fields::convertToKeyedArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Thinktomorrow\Chief\Fields;
4
5
use ArrayIterator;
6
use Thinktomorrow\Chief\Fields\Types\Field;
7
8
class Fields implements \ArrayAccess, \IteratorAggregate
9
{
10
    /** @var array */
11
    private $fields;
12
13 104
    public function __construct(array $fields = [])
14
    {
15 104
        $this->validateFields($fields);
16
17 104
        $this->fields = $this->convertToKeyedArray($fields);
18 104
    }
19
20 83
    public function all(): array
21
    {
22 83
        return $this->fields;
23
    }
24
25 5
    public function first(): ?Field
26
    {
27 5
        if (!$this->any()) {
28
            return null;
29
        }
30
31 5
        return reset($this->fields);
32
    }
33
34 5
    public function any(): bool
35
    {
36 5
        return count($this->all()) > 0;
37
    }
38
39
    public function isEmpty(): bool
40
    {
41
        return !$this->any();
42
    }
43
44 6
    public function keys(): array
45
    {
46 6
        return array_keys($this->fields);
47
    }
48
49 84
    public function validate(array $data)
50
    {
51 84
        foreach ($this->fields as $field) {
52 84
            $field->validator($data)->validate();
53
        }
54 76
    }
55
56 5
    public function filterBy($key, $value = null)
57
    {
58 5
        $fields = [];
59
60 5
        foreach ($this->fields as $i => $field) {
61 5
            if ($key instanceof \Closure) {
62 2
                if (true == $key($field)) {
63 2
                    $fields[] = $field;
64
                }
65
66 2
                continue;
67
            }
68
69
            // Reject from list if value does not match expected one
70 3
            if ($value && $value == $field->$key) {
71 3
                $fields[] = $field;
72
            }
73
74
            // Reject from list if key returns null (key not present on field)
75 3
            elseif (!$value && !is_null($field->$key)) {
76 3
                $fields[] = $field;
77
            }
78
        }
79
80 5
        return new static($fields);
81
    }
82
83 75
    public function add(Field ...$fields): Fields
84
    {
85 75
        return new Fields(array_merge($this->fields, $fields));
86
    }
87
88 69
    public function merge(Fields $fields): Fields
89
    {
90 69
        return new Fields(array_merge($this->fields, $fields->all()));
91
    }
92
93
    public function remove($keys = null)
94
    {
95
        if (!$keys) {
96
            return $this;
97
        }
98
99
        if (is_string($keys)) {
100
            $keys = func_get_args();
101
        }
102
103
        foreach ($this->fields as $k => $field) {
104
            if (in_array($field->key, $keys)) {
105
                unset($this->fields[$k]);
106
            }
107
        }
108
109
        return $this;
110
    }
111
112 21
    public function offsetExists($offset)
113
    {
114 21
        return isset($this->fields[$offset]);
115
    }
116
117 21
    public function offsetGet($offset)
118
    {
119 21
        return (isset($this->fields[$offset]))
120 21
                ? $this->fields[$offset]
121 21
                : null;
122
    }
123
124 77
    public function offsetSet($offset, $value)
125
    {
126 77
        if (! $value instanceof Field) {
127
            throw new \InvalidArgumentException('Passed value must be of type ' . Field::class);
128
        }
129
130 77
        $this->fields[$offset] = $value;
131 77
    }
132
133 3
    public function offsetUnset($offset)
134
    {
135 3
        unset($this->fields[$offset]);
136 3
    }
137
138 90
    public function getIterator()
139
    {
140 90
        return new ArrayIterator($this->fields);
141
    }
142
143 104
    private function convertToKeyedArray(array $fields): array
144
    {
145 104
        $keyedFields = [];
146
147 104
        foreach ($fields as $field) {
148 100
            $keyedFields[$field->key] = $field;
149
        }
150
151 104
        return $keyedFields;
152
    }
153
154 104
    private function validateFields(array $fields)
155
    {
156
        array_map(function (Field $field) {
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

156
        array_map(function (/** @scrutinizer ignore-unused */ Field $field) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157 104
        }, $fields);
158 104
    }
159
}
160