Completed
Push — master ( 96d15c...294c9d )
by Gabor
03:37
created

TestForm::getFormContainer()   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
 * 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\Web;
13
14
use WebHemi\Form\AbstractForm;
15
use WebHemi\Form\Element\Web;
16
17
/**
18
 * Class TestForm
19
 *
20
 * @codeCoverageIgnore - only for test purposes
21
 */
22
class TestForm extends AbstractForm
23
{
24
    /** @var  Web\FormElement */
25
    protected $form;
26
27
    /**
28
     * Returns the form container element. E.g.: for HTML forms it is the <form> tag.
29
     *
30
     * @return Web\FormElement
31
     */
32
    protected function getFormContainer()
33
    {
34
        return new Web\FormElement();
35
    }
36
37
    /**
38
     * Initialize form.
39
     *
40
     * @return void
41
     */
42
    protected function initForm()
43
    {
44
        // Hidden field.
45
        $hiddenCSRF = new Web\HiddenElement('csrf');
46
        $hiddenCSRF->setValue('Some CSRF test value');
47
48
        $xContent = <<<EOH
49
<h2>This is a static content</h2>
50
<p>
51
    Veritus invenire mei ne. No nam nullam probatus, mea te nostro delicatissimi. Ea deserunt pericula cum, no eos 
52
    tation rationibus scriptorem. Mel ut verterem invenire. Eu vim aperiam nonumes sententiae, ex quas sapientem has. 
53
    Mea autem adolescens ea, ea ridens oportere sit, ex dicta tacimates nec. Scaevola complectitur ad sed, ne ius 
54
    praesent argumentum.
55
</p>
56
<p>
57
    Pro no dicam aliquid bonorum. Inimicus imperdiet mei et. Ut hendrerit adversarium sed. Te cum everti delicata, 
58
    quis utamur argumentum pri id, oratio exerci everti eam te.
59
</p>
60
<p>
61
    No meliore deterruisset nec. An eros electram constituto duo, has reque corrumpit at. Nihil aperiri liberavisse in 
62
    has, eu qui nonumy nusquam. Mazim salutatus te nam, nobis putent mentitum mea ne. Mundi denique at ius, 
63
    illum quaestio vix id.
64
</p>
65
EOH;
66
        $fieldSetX = new Web\StaticContentElement('notice', 'Please Note');
67
        $fieldSetX->setValue($xContent);
68
69
        // Field sets
70
        $fieldSet1 = $this->getMainFieldSet();
71
        $fieldSet2 = $this->getLocationFieldSet();
72
73
        // Submit button.
74
        $submit = new Web\SubmitElement('submit', 'Login');
75
76
        // Assign elements and the field sets to the form
77
        $this->setNodes(
78
            [
0 ignored issues
show
Documentation introduced by
array($hiddenCSRF, $fiel...X, $fieldSet2, $submit) is of type array<integer,object<Web...\Web\\SubmitElement>"}>, but the function expects a array<integer,object<Web...\FormElementInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
                $hiddenCSRF,
80
                $fieldSet1,
81
                $fieldSetX,
82
                $fieldSet2,
83
                $submit
84
            ]
85
        );
86
    }
87
88
    /**
89
     * Gets main field set
90
     *
91
     * @return Web\FieldSetElement
92
     */
93
    private function getMainFieldSet()
94
    {
95
        // Field set
96
        $fieldSet = new Web\FieldSetElement('info', 'Login form');
97
98
        // Text input with custom attribute
99
        $loginname = new Web\TextElement('username', 'Username');
100
        $loginname->setAttributes(['placeholder' => 'Your login name']);
101
102
        // Password input
103
        $password = new Web\PasswordElement('password', 'Password');
104
        $password->setErrors(
105
            [
106
                'It just doesn\'t work, buddy...'
107
            ]
108
        );
109
110
        // HTML5 email input
111
        $email = new Web\InputElement('email', 'Email');
112
        $email->setType('email');
113
114
        // Single-option checkbox.
115
        // For single-option radio boxes the ->setValue(1) is equals to this: ->setAttribute('checked', true);
116
        $checkboxRememberMe = new Web\CheckboxElement('remember_me', 'Remember Me');
117
        $checkboxRememberMe->setValue('1');
118
119
        // Radio group.
120
        $radioLanguage = new Web\RadioElement('language', 'Select language');
121
        $radioLanguage->setOptions(
122
            [
123
                ['label' => 'English', 'value' => 'en', 'checked' => true],
124
                ['label' => 'German', 'value' => 'de', 'checked' => true],
125
                ['label' => 'Spanish', 'value' => 'es', 'checked' => true],
126
            ]
127
        );
128
129
        // Checkbox group.
130
        $checkboxTerms = new Web\CheckboxElement('terms', 'Terms and conditions');
131
        $checkboxTerms->setOptions(
132
            [
133
                ['label' => 'I am human.', 'value' => 'human'],
134
                ['label' => 'I have read the T&C.', 'value' => 'terms_read', 'checked' => false],
135
                ['label' => 'I want newsletters.', 'value' => 'terms_newsletter', 'checked' => true],
136
            ]
137
        );
138
139
        // Assign elements to the field set
140
        $fieldSet->setNodes(
141
            [
142
                $loginname,
143
                $password,
144
                $email,
145
                $checkboxRememberMe,
146
                $radioLanguage,
147
                $checkboxTerms
148
            ]
149
        );
150
151
        return $fieldSet;
152
    }
153
154
    /**
155
     * Gets location field set.
156
     *
157
     * @return Web\FieldSetElement
158
     */
159
    private function getLocationFieldSet()
160
    {
161
        // Field set
162
        $fieldSet = new Web\FieldSetElement('location', 'Location');
163
164
        // Select box with no multi selection and with no option groups
165
        $select1 = new Web\SelectElement('country', 'I live in:');
166
        $select1->setOptions(
167
            [
168
                ['label' => 'Hungary', 'value' => 'hu'],
169
                ['label' => 'Germany', 'value' => 'de', 'checked' => true],
170
                ['label' => 'Austria', 'value' => 'at'],
171
            ]
172
        );
173
174
        // Select box with no multi selection and WITH option groups
175
        $select2 = new Web\SelectElement('dream', 'I\'d like live in:');
176
        $select2->setOptions(
177
            [
178
                ['label' => 'Anywhere', 'value' => '*'],
179
                ['label' => 'Hungary', 'value' => 'hu', 'group' => 'Europe'],
180
                ['label' => 'USA', 'value' => 'us', 'group' => 'America'],
181
                ['label' => 'Germany', 'value' => 'de', 'group' => 'Europe'],
182
                ['label' => 'Austria', 'value' => 'at', 'group' => 'Europe'],
183
                ['label' => 'Canada', 'value' => 'ca', 'group' => 'America'],
184
                ['label' => 'Switzerland', 'value' => 'ch', 'group' => 'Europe', 'checked' => true],
185
                ['label' => 'France', 'value' => 'fr', 'group' => 'Europe'],
186
                ['label' => 'Spain', 'value' => 'es', 'group' => 'Europe'],
187
                ['label' => 'Japan', 'value' => 'jp', 'group' => 'Asia'],
188
            ]
189
        )
190
            ->setErrors(
191
                [
192
                    'This has some errors.',
193
                    'And also has a major problem.'
194
                ]
195
            );
196
197
        // Select box WITH multi selection.
198
        $select3 = new Web\SelectElement('past', 'I\'ve already lived in:');
199
        $select3->setOptions(
200
            [
201
                ['label' => 'Hungary', 'value' => 'hu', 'group' => 'Europe', 'checked' => true],
202
                ['label' => 'USA', 'value' => 'us', 'group' => 'America'],
203
                ['label' => 'Germany', 'value' => 'de', 'group' => 'Europe', 'checked' => true],
204
                ['label' => 'Austria', 'value' => 'at', 'group' => 'Europe'],
205
                ['label' => 'Canada', 'value' => 'ca', 'group' => 'America'],
206
                ['label' => 'Switzerland', 'value' => 'ch', 'group' => 'Europe'],
207
                ['label' => 'France', 'value' => 'fr', 'group' => 'Europe'],
208
                ['label' => 'Spain', 'value' => 'es', 'group' => 'Europe'],
209
                ['label' => 'Japan', 'value' => 'jp', 'group' => 'Asia'],
210
            ]
211
        )
212
            ->setAttributes(
213
                [
214
                    'multiple' => true,
215
                    'size' => 10
216
                ]
217
            );
218
219
        // Assign elements to the field set
220
        $fieldSet->setNodes(
221
            [
222
                $select1,
223
                $select2,
224
                $select3
225
            ]
226
        );
227
228
        return $fieldSet;
229
    }
230
}
231