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

TestForm   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 181
rs 10
c 1
b 1
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
B initForm() 0 25 1
A getMainFieldset() 0 51 1
B getLocationFieldset() 0 85 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 WebHemi\Form\Element\FormElement;
15
16
/**
17
 * Class TestForm
18
 *
19
 * @codeCoverageIgnore - only for test purposes
20
 */
21
class TestForm extends AbstractForm
22
{
23
    /**
24
     * Initialize form.
25
     *
26
     * @return void
27
     */
28
    protected function initForm()
29
    {
30
        // The best way to avoid autocomplete fields is to give unique name to the fields on every page load.
31
        $this->setNameSalt(time());
32
        // Change some default attributes.
33
        $this->setAutocomplete(false);
34
35
36
        // Hidden field.
37
        $hiddenCsrf = new FormElement(FormElement::TAG_INPUT_HIDDEN, 'csrf');
38
        $hiddenCsrf->setValue('Some CSRF test value');
39
40
        // Fieldsets
41
        $fieldset1 = $this->getMainFieldset();
42
        $fieldset2 = $this->getLocationFieldset();
43
44
        // Submit button.
45
        $submit = new FormElement(FormElement::TAG_BUTTON_SUBMIT, 'submit', 'Login');
46
47
        // Assign elements and the fieldset to the form
48
        $this->addChildNode($hiddenCsrf)
49
            ->addChildNode($fieldset1)
50
            ->addChildNode($fieldset2)
51
            ->addChildNode($submit);
52
    }
53
54
    /**
55
     * Gets main fieldset
56
     *
57
     * @return FormElement
58
     */
59
    private function getMainFieldset()
60
    {
61
        // Fieldset
62
        $fieldset = new FormElement(FormElement::TAG_FIELDSET, 'info');
63
        // Legend for the fieldset.
64
        $legend = new FormElement(FormElement::TAG_LEGEND);
65
        $legend->setLabel('Login form');
66
67
        // Text input with custom attribute
68
        $loginname = new FormElement(FormElement::TAG_INPUT_TEXT, 'username', 'Username');
69
        $loginname->setAttribute('placeholder', 'Your login name');
70
71
        // Password input
72
        $password = new FormElement(FormElement::TAG_INPUT_PASSWORD, 'password', 'Password');
73
74
        // Single-option checkbox.
75
        // For single-option radio boxes the ->setValue(1) is equals to this: ->setAttribute('checked', true);
76
        $checkboxRememberMe = new FormElement(FormElement::TAG_INPUT_CHECKBOX, 'remember_me', 'Remember Me');
77
        $checkboxRememberMe->setValue(1);
78
79
        // Radio group.
80
        $radioLanguage = new FormElement(FormElement::TAG_INPUT_RADIO, 'language', 'Select language');
81
        $radioLanguage->setValue('es')
82
            ->setOptions(
83
                [
84
                    ['label' => 'English', 'value' => 'en'],
85
                    ['label' => 'German', 'value' => 'de'],
86
                    ['label' => 'Spanish', 'value' => 'es'],
87
                ]
88
            );
89
90
        // Checkbox group.
91
        $checkboxTerms = new FormElement(FormElement::TAG_INPUT_CHECKBOX, 'terms', 'Terms and conditions');
92
        $checkboxTerms->setOptions(
93
            [
94
                ['label' => 'I am human.', 'value' => 'human'],
95
                ['label' => 'I have read the T&C.', 'value' => 'terms_read', 'checked' => false],
96
                ['label' => 'I want newsletters.', 'value' => 'terms_newsletter', 'checked' => true],
97
            ]
98
        );
99
100
        // Assign elements to the fieldset
101
        $fieldset->addChildNode($legend)
102
            ->addChildNode($loginname)
103
            ->addChildNode($password)
104
            ->addChildNode($checkboxRememberMe)
105
            ->addChildNode($radioLanguage)
106
            ->addChildNode($checkboxTerms);
107
108
        return $fieldset;
109
    }
110
111
    /**
112
     * Gets location fieldset.
113
     *
114
     * @return FormElement
115
     */
116
    private function getLocationFieldset()
117
    {
118
        // Fieldset
119
        $fieldset = new FormElement(FormElement::TAG_FIELDSET, 'location');
120
        // Legend for the fieldset.
121
        $legend = new FormElement(FormElement::TAG_LEGEND);
122
        $legend->setLabel('Location');
123
124
        // Select box with no multi selection and with no option groups
125
        $select1 = new FormElement(FormElement::TAG_SELECT, 'country', 'I live in:');
126
        $select1->setOptions(
127
            [
128
                ['label' => 'Hungary', 'value' => 'hu'],
129
                ['label' => 'Germany', 'value' => 'de', 'checked' => true],
130
                ['label' => 'Austria', 'value' => 'at'],
131
            ]
132
        );
133
134
        // Select box with no multi selection and WITH option groups
135
        $select2 = new FormElement(FormElement::TAG_SELECT, 'dream', 'I\'d like live in:');
136
        $select2->setOptions(
137
            [
138
                ['label' => 'Anywhere', 'value' => '*'],
139
                ['label' => 'Hungary', 'value' => 'hu', 'group' => 'Europe'],
140
                ['label' => 'USA', 'value' => 'us', 'group' => 'America'],
141
                ['label' => 'Germany', 'value' => 'de', 'group' => 'Europe'],
142
                ['label' => 'Austria', 'value' => 'at', 'group' => 'Europe'],
143
                ['label' => 'Canada', 'value' => 'ca', 'group' => 'America'],
144
                ['label' => 'Switzerland', 'value' => 'ch', 'group' => 'Europe', 'checked' => true],
145
                ['label' => 'France', 'value' => 'fr', 'group' => 'Europe'],
146
                ['label' => 'Spain', 'value' => 'es', 'group' => 'Europe'],
147
            ]
148
        );
149
150
        // Select box WITH multi selection. The element is build with constructors.
151
        $select3 = new FormElement(FormElement::TAG_SELECT, 'past', 'I\'ve already lived in:');
152
        $select3->setAttribute('multiple', true)
153
            ->setAttribute('size', 10);
154
155
        $select3Group1 = new FormElement(FormElement::TAG_OPTION_GROUP);
156
        $select3Group1->setLabel('Europe');
157
158
        $option1 = new FormElement(FormElement::TAG_OPTION);
159
        $option1->setValue('hu')->setLabel('Hungary')->setAttribute('selected', true);
160
161
        $option2 = new FormElement(FormElement::TAG_OPTION);
162
        $option2->setValue('de')->setLabel('Germany')->setAttribute('selected', true);
163
164
        $option3 = new FormElement(FormElement::TAG_OPTION);
165
        $option3->setValue('ch')->setLabel('Switzerland');
166
167
        $option4 = new FormElement(FormElement::TAG_OPTION);
168
        $option4->setValue('fr')->setLabel('France');
169
170
        $select3Group1->setChildNodes([$option1, $option2, $option3, $option4]);
171
172
        $select3Group2 = new FormElement(FormElement::TAG_OPTION_GROUP);
173
        $select3Group2->setLabel('America');
174
175
        $option5 = new FormElement(FormElement::TAG_OPTION);
176
        $option5->setValue('us')->setLabel('USA');
177
178
        $option6 = new FormElement(FormElement::TAG_OPTION);
179
        $option6->setValue('ca')->setLabel('Canada');
180
181
        $select3Group2->setChildNodes([$option5, $option6]);
182
183
        $select3Group3 = new FormElement(FormElement::TAG_OPTION_GROUP);
184
        $select3Group3->setLabel('Asia');
185
186
        $option7 = new FormElement(FormElement::TAG_OPTION);
187
        $option7->setValue('jp')->setLabel('Japan');
188
189
        $select3Group3->setChildNodes([$option7]);
190
191
        $select3->setChildNodes([$select3Group1, $select3Group2, $select3Group3]);
192
193
        // Assign elements to the fieldset
194
        $fieldset->addChildNode($legend)
195
            ->addChildNode($select1)
196
            ->addChildNode($select2)
197
            ->addChildNode($select3);
198
199
        return $fieldset;
200
    }
201
}
202