Passed
Push — master ( 042e95...d37488 )
by Chris
02:46
created

FormFieldRepository::getFieldNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace WebTheory\Saveyour\Controllers;
4
5
use WebTheory\Saveyour\Contracts\FormFieldControllerInterface;
6
use WebTheory\Saveyour\Contracts\FormFieldRepositoryInterface;
7
8
class FormFieldRepository implements FormFieldRepositoryInterface
9
{
10
    /**
11
     * @var FormFieldControllerInterface[]
12
     */
13
    protected $fields = [];
14
15
    /**
16
     *
17
     */
18
    public function __construct(FormFieldControllerInterface ...$fields)
19
    {
20
        array_map([$this, 'addField'], $fields);
21
    }
22
23
    /**
24
     * Get the value of fields
25
     *
26
     * @return mixed
27
     */
28
    public function getFields(string ...$fields): array
29
    {
30
        return empty($fields) ? $this->fields : $this->getDefinedFields(...$fields);
31
    }
32
33
    /**
34
     *
35
     */
36
    public function getField(string $field): ?FormFieldControllerInterface
37
    {
38
        return $this->fields[$field] ?? null;
39
    }
40
41
    /**
42
     *
43
     */
44
    public function getDefinedFields(string ...$fields): array
45
    {
46
        return array_filter(
47
            $this->fields,
48
            function (FormFieldControllerInterface $field) use ($fields) {
49
                return in_array($field->getRequestVar(), $fields);
50
            }
51
        );
52
    }
53
54
    /**
55
     *
56
     */
57
    public function addField(FormFieldControllerInterface $field)
58
    {
59
        $this->fields[$field->getRequestVar()] = $field;
60
    }
61
62
    /**
63
     * @return string[]
64
     */
65
    public function getFieldNames(): array
66
    {
67
        return array_keys($this->fields);
68
    }
69
}
70