Completed
Push — develop ( 25051e...29e407 )
by
unknown
09:42
created

UserInfoFieldset::init()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 114
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 114
rs 8.2857
c 2
b 0
f 0
cc 1
eloc 64
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license       MIT
8
 */
9
10
namespace Auth\Form;
11
12
use Core\Entity\Hydrator\EntityHydrator;
13
use Core\Form\EmptySummaryAwareInterface;
14
use Zend\Form\Fieldset;
15
use Core\Form\ViewPartialProviderInterface;
16
use Zend\InputFilter\InputFilterProviderInterface;
17
use Zend\Validator\StringLength;
18
use Zend\Validator\NotEmpty;
19
use Zend\Validator\EmailAddress;
20
use Zend\Validator\File;
21
22
class UserInfoFieldset extends Fieldset implements
23
    ViewPartialProviderInterface,
24
    EmptySummaryAwareInterface,
25
    InputFilterProviderInterface
26
{
27
28
    /**
29
     * View script for rendering
30
     * 
31
     * @var string
32
     */
33
    protected $viewPartial = 'form/auth/contact';
34
    /**
35
     * The empty summary notice.
36
     *
37
     * @var string
38
     */
39
    protected $emptySummaryNotice = /*@translate*/ 'Click here to enter contact informations.';
40
41
    /**
42
     * @param String $partial
43
     *
44
     * @return $this
45
     */
46
    public function setViewPartial($partial)
47
    {
48
        $this->viewPartial = $partial;
49
50
        return $this;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getViewPartial()
57
    {
58
        return $this->viewPartial;
59
    }
60
61
    /**
62
     * @return \Zend\Hydrator\HydratorInterface
63
     */
64
    public function getHydrator()
65
    {
66
        if (!$this->hydrator) {
67
            $hydrator = new EntityHydrator();
68
            $this->setHydrator($hydrator);
69
        }
70
71
        return $this->hydrator;
72
    }
73
74
    public function init()
75
    {
76
        $this->setName('info');
77
78
        $this->add(
79
            [
80
                'name'    => 'email',
81
                'options' => [
82
                    'label' => /* @translate */ 'Email'
83
                ],
84
                'attributes' => [
85
                    'required' => true, // marks the label as required.
86
                ]
87
            ]
88
        );
89
90
        $this->add(
91
            [
92
                'name'      => 'phone',
93
                'type'      => '\Core\Form\Element\Phone',
94
                'options'   => [
95
                    'label' => /* @translate */ 'Phone',
96
                ],
97
                'maxlength' => 20,
98
                'attributes' => [
99
                    'required' => true,
100
                ]
101
            ]
102
        );
103
104
        $this->add(
105
            [
106
                'name'    => 'postalCode',
107
                'options' => array(
108
                    'label' => /* @translate */ 'Postalcode'
109
                )
110
            ]
111
        );
112
113
        $this->add(
114
            [
115
                'name'    => 'city',
116
                'options' => [
117
                    'label' => /* @translate */ 'City'
118
                ]
119
            ]
120
        );
121
122
        $this->add(
123
            array(
124
                'name'       => 'gender',
125
                'type'       => 'Zend\Form\Element\Select',
126
                'options'    => [
127
                    'label'         => /*@translate */ 'Salutation',
128
                    'value_options' => [
129
                        ''       => '',
130
                        'male'   => /*@translate */ 'Mr.',
131
                        'female' => /*@translate */ 'Mrs.',
132
                    ]
133
                ],
134
                'attributes' => [
135
                    'data-placeholder' => /*@translate*/ 'please select',
136
                    'data-allowclear' => 'false',
137
                    'data-searchbox' => -1,  // hide the search box
138
                    'required' => true, // mark label as required
139
                ],
140
            )
141
        );
142
143
        $this->add(
144
            array(
145
                'name'       => 'firstName',
146
                'required'   => true,
147
                'options'    => [
148
                    'label'     => /*@translate*/ 'First name',
149
                    'maxlength' => 50,
150
                ],
151
                'attributes' => [
152
                    'required' => true,
153
                ]
154
            )
155
        );
156
157
        $this->add(
158
            array(
159
                'name'     => 'lastName',
160
                'options'  => array(
161
                    'label'     => /*@translate*/ 'Last name',
162
                    'maxlength' => 50,
163
                ),
164
                'attributes' => [
165
                    'required' => true,
166
                ]
167
            )
168
        );
169
170
        $this->add(
171
            [
172
                'name'    => 'street',
173
                'options' => [
174
                    'label' => /*@translate*/ 'street'
175
                ]
176
            ]
177
        );
178
179
        $this->add(
180
            [
181
                'name'    => 'houseNumber',
182
                'options' => [
183
                    'label' => /*@translate*/ 'house number'
184
                ]
185
            ]
186
        );
187
    }
188
189
    /**
190
     * (non-PHPdoc)
191
     *
192
     * @see \Zend\InputFilter\InputFilterProviderInterface::getInputFilterSpecification()
193
     */
194
    public function getInputFilterSpecification()
195
    {
196
        return array(
197
            'firstName' => array(
198
                'required'   => true,
199
                'filters'    => array(
200
                    array('name' => '\Zend\Filter\StringTrim'),
201
                ),
202
                'validators' => array(
203
                    new NotEmpty(),
204
                    new StringLength(array('max' => 50))
205
                ),
206
            ),
207
            'lastName'  => array(
208
                'required'   => true,
209
                'filters'    => array(
210
                    array('name' => 'Zend\Filter\StringTrim'),
211
                ),
212
                'validators' => array(
213
                    new NotEmpty(),
214
                    new StringLength(array('max' => 50))
215
                ),
216
            ),
217
            'email'     => array(
218
                'required'   => true,
219
                'filters'    => array(
220
                    array('name' => 'Zend\Filter\StringTrim'),
221
                ),
222
                'validators' => array(
223
                    new NotEmpty(),
224
                    new StringLength(array('max' => 100)),
225
                    new EmailAddress()
226
                )
227
            ),
228
            'image'     => array(
229
                'required'   => false,
230
                'filters'    => array(),
231
                'validators' => array(
232
                    new File\Exists(),
233
                    new File\Extension(array('extension' => array('jpg', 'png', 'jpeg', 'gif'))),
234
                ),
235
            ),
236
        );
237
    }
238
239
    /**
240
     * If all elements have empty values, the form will be displayed collapsed with the EmptySummaryNotice
241
     *
242
     * @return bool
243
     */
244
    public function isSummaryEmpty()
245
    {
246
        foreach ($this as $element) { /* @var $element \Zend\Form\ElementInterface */
247
            if ('' != $element->getValue()) {
248
                return false;
249
            }
250
        }
251
        return true;
252
    }
253
254
    /**
255
     * @param $message
256
     *
257
     * @return $this
258
     */
259
    public function setEmptySummaryNotice($message)
260
    {
261
        $this->emptySummaryNotice = $message;
262
        return $this;
263
    }
264
265
    /**
266
     * @return string
267
     */
268
    public function getEmptySummaryNotice()
269
    {
270
        return $this->emptySummaryNotice;
271
    }
272
273
}
274