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

FormFieldRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 60
ccs 0
cts 27
cp 0
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getField() 0 3 1
A getDefinedFields() 0 6 1
A addField() 0 3 1
A getFields() 0 3 2
A getFieldNames() 0 3 1
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