Completed
Push — master ( 81dd50...6a4de4 )
by Derek Stephen
06:29
created

AbstractForm::getValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 19/11/2016
5
 * Time: 12:13
6
 */
7
8
namespace Del\Form;
9
10
use Del\Form\Collection\FieldCollection;
11
use Del\Form\Field\FieldInterface;
12
13
abstract class AbstractForm implements FormInterface
14
{
15
    /** @var FieldCollection $fieldCollection */
16
    private $fieldCollection;
17
18
    /** @var string $name */
19
    private $name;
20
21
    public function __construct($name)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

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

Loading history...
22
    {
23
        $this->fieldCollection = new FieldCollection();
24
        $this->init();
25
    }
26
27
    abstract public function init();
28
29
    /**
30
     * @return bool
31
     */
32
    public function isValid()
33
    {
34
        return false;
35
    }
36
37
    /**
38
     * @return array
39
     */
40
    public function getValues()
41
    {
42
        return [];
43
    }
44
45
    /**
46
     * @param array $data
47
     * @return $this
48
     */
49
    public function populate(array $data)
50
    {
51
        /** @var FieldInterface $field */
52
        foreach ($this->fieldCollection as $field) {
53
            $name = $field->getName();
54
            if (isset($data[$name])) {
55
                $field->setValue($data[$name]);
56
            }
57
        }
58
        return $this;
59
    }
60
61
    public function getField($name)
62
    {
63
        return $this->fieldCollection->findByName($name);
64
    }
65
66
    public function getFields()
67
    {
68
        return $this->fieldCollection;
69
    }
70
71
    public function addField(FieldInterface $field)
72
    {
73
        $this->fieldCollection->append($field);
74
        return $this;
75
    }
76
77
78
    public function render()
79
    {
80
        return '<form name="'.$this->name.'">Render Fields Here</form>';
81
    }
82
83
}