Completed
Push — master ( b9b773...5bbb0b )
by Gabor
64:05
created

AbstractForm::initForm()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Form;
13
14
use Iterator;
15
16
abstract class AbstractForm implements FormInterface, Iterator
17
{
18
    /** @var FormElement[] */
19
    protected $form;
20
21
    /**
22
     * FormInterface constructor. Creates <FORM> element automatically.
23
     *
24
     * @param string $name
25
     * @param string $action
26
     * @param string $method
27
     */
28
    public function __construct($name, $action = '', $method = 'POST')
29
    {
30
        $form = new FormElement('form', $name);
31
        $form->setAttribute('action', $action)
32
            ->setAttribute('method', strtoupper($method));
33
34
        // for simplicity, we store it in an array.
35
        $this->form[0] = $form;
36
37
        $this->initForm();
38
    }
39
40
    /**
41
     * Initialize form.
42
     *
43
     * @return void
44
     */
45
    abstract protected function initForm();
46
47
    /**
48
     * Adds a form element to the form.
49
     *
50
     * @param FormElement $formElement
51
     * @return AbstractForm
52
     */
53
    protected function addChildNode(FormElement $formElement)
54
    {
55
        $formElement->setParentNode($this->form[0]);
56
57
        $this->form[0]->addChildNode($formElement);
58
59
        return $this;
60
    }
61
62
    /**
63
     * Gets the form elements.
64
     *
65
     * @return FormElement[];
0 ignored issues
show
Documentation introduced by
The doc-type FormElement[]; could not be parsed: Expected "|" or "end of type", but got ";" at position 13. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
66
     */
67
    public function getChildNodes()
68
    {
69
        return $this->form[0]->getChildNodes();
70
    }
71
72
    /**
73
     * Validates the form.
74
     *
75
     * @return boolean
76
     */
77
    public function isValid()
78
    {
79
        $valid = true;
80
81
        // TODO: TBD
82
83
        return $valid;
84
    }
85
86
    /**
87
     * Sets form data.
88
     *
89
     * @param array $data
90
     * @return FormInterface
91
     */
92
    public function setData(array $data)
93
    {
94
        // TODO: TBD
95
96
        return $this;
97
    }
98
99
    /**
100
     * Returns the form data.
101
     *
102
     * @return array
103
     */
104
    public function getData()
105
    {
106
        // TODO: TBD
107
        return [];
108
    }
109
110
    /**
111
     * Return the current element.
112
     *
113
     * @return FormElement
114
     */
115
    final public function current()
116
    {
117
        return current($this->form);
118
    }
119
120
    /**
121
     * Moves the pointer forward to next element.
122
     *
123
     * @return void
124
     */
125
    final public function next()
126
    {
127
        next($this->form);
128
    }
129
130
    /**
131
     * Returns the key of the current element.
132
     *
133
     * @return mixed
134
     */
135
    final public function key()
136
    {
137
        return key($this->form);
138
    }
139
140
    /**
141
     * Checks if current position is valid.
142
     *
143
     * @return boolean
144
     */
145
    final public function valid()
146
    {
147
        $key = key($this->form);
148
149
        return ($key !== NULL && $key !== FALSE);
150
    }
151
152
    /**
153
     * Rewinds the Iterator to the first element.
154
     *
155
     * @return void
156
     */
157
    final public function rewind()
158
    {
159
        reset($this->form);
160
    }
161
}
162