Completed
Push — master ( 5ff101...73de08 )
by Gabor
08:09
created

AbstractForm::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
use WebHemi\Form\Element\FormElement;
16
17
/**
18
 * Class AbstractForm
19
 */
20
abstract class AbstractForm implements FormInterface, Iterator
21
{
22
    /** @var array<FormElement> */
23
    protected $form;
24
    /** @var string */
25
    protected $name;
26
    /** @var string */
27
    protected $action;
28
    /** @var string */
29
    protected $method;
30
31
    /** @var string */
32
    protected $uniqueFormNamePostfix = '';
33
34
    /**
35
     * AbstractForm constructor. Should creates <FORM> element automatically.
36
     *
37
     * @param string $name
38
     * @param string $action
39
     * @param string $method
40
     */
41
    final public function __construct($name, $action = '', $method = 'POST')
42
    {
43
        $form = new FormElement('form', $name);
44
        $form->setAttribute('action', $action)
45
            ->setAttribute('method', strtoupper($method));
46
47
        // for simplicity in rendering (twig macro), we store it in an array.
48
        $this->form[0] = $form;
49
50
        $this->initForm();
51
    }
52
53
    /**
54
     * Initialize form.
55
     *
56
     * @return void
57
     */
58
    abstract protected function initForm();
59
60
    /**
61
     * Set unique identifier for the form.
62
     *
63
     * @param string $salt
64
     * @return AbstractForm
65
     */
66
    protected function setNameSalt($salt)
67
    {
68
        $name = $this->form[0]->getName();
69
        $this->form[0]->setName($name.'_'.md5($salt));
70
71
        return $this;
72
    }
73
74
    /**
75
     * Sets form action.
76
     *
77
     * @param string $action
78
     * @return AbstractForm
79
     */
80
    protected function setAction($action)
81
    {
82
        $this->form[0]->setAttribute('action', $action);
83
84
        return $this;
85
    }
86
87
    /**
88
     * Sets form submit.
89
     *
90
     * @param string $method
91
     * @return AbstractForm
92
     */
93
    protected function setMethod($method = 'POST')
94
    {
95
        $this->form[0]->setAttribute('method', $method);
96
97
        return $this;
98
    }
99
100
    /**
101
     * Sets form autocomplete option.
102
     *
103
     * @param bool $autocomplete
104
     * @return AbstractForm
105
     */
106
    protected function setAutocomplete($autocomplete = true)
107
    {
108
        $this->form[0]->setAttribute('autocomplete', $autocomplete);
109
110
        return $this;
111
    }
112
113
    /**
114
     * Sets form encryption type.
115
     *
116
     * @param string $enctype
117
     * @return AbstractForm
118
     */
119
    protected function setEnctype($enctype = 'application/x-www-form-urlencoded')
120
    {
121
        $this->form[0]->setAttribute('enctype', $enctype);
122
123
        return $this;
124
    }
125
126
    /**
127
     * Adds a form element to the form.
128
     *
129
     * @param FormElement $formElement
130
     * @return AbstractForm
131
     */
132
    protected function addChildNode(FormElement $formElement)
133
    {
134
        $formElement->setParentNode($this->form[0]);
135
136
        $this->form[0]->addChildNode($formElement);
137
138
        return $this;
139
    }
140
141
    /**
142
     * Gets the form elements.
143
     *
144
     * @return array<FormElement>
145
     */
146
    public function getChildNodes()
147
    {
148
        return $this->form[0]->getChildNodes();
149
    }
150
151
    /**
152
     * Validates the form.
153
     *
154
     * @return boolean
155
     */
156
    public function isValid()
157
    {
158
        return $this->form[0]->isValid();
159
    }
160
161
    /**
162
     * Sets form data.
163
     *
164
     * @param array $data
165
     * @return FormInterface
166
     */
167
    public function setData(array $data)
168
    {
169
        // TODO: TBD
170
171
        // content to avoid phpmd warning until the real function logic is created...
172
        if (!empty($data)) {
173
            $this->isValid();
174
        }
175
176
        return $this;
177
    }
178
179
    /**
180
     * Returns the form data.
181
     *
182
     * @return array
183
     */
184
    public function getData()
185
    {
186
        return $this->form[0]->getValue();
187
    }
188
189
    /**
190
     * Return the current element.
191
     *
192
     * @return FormElement
193
     */
194
    final public function current()
195
    {
196
        return current($this->form);
197
    }
198
199
    /**
200
     * Moves the pointer forward to next element.
201
     *
202
     * @return void
203
     */
204
    final public function next()
205
    {
206
        next($this->form);
207
    }
208
209
    /**
210
     * Returns the key of the current element.
211
     *
212
     * @return mixed
213
     */
214
    final public function key()
215
    {
216
        return key($this->form);
217
    }
218
219
    /**
220
     * Checks if current position is valid.
221
     *
222
     * @return boolean
223
     */
224
    final public function valid()
225
    {
226
        $key = key($this->form);
227
228
        return ($key !== null && $key !== false);
229
    }
230
231
    /**
232
     * Rewinds the Iterator to the first element.
233
     *
234
     * @return void
235
     */
236
    final public function rewind()
237
    {
238
        reset($this->form);
239
    }
240
}
241