Completed
Branch dev-master (76dc67)
by Derek Stephen
01:55
created

AbstractForm::setFormRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 19/11/2016
5
 * Time: 12:13
6
 */
7
8
namespace Del\Form;
9
10
use Del\Form\Collection\FieldCollection;
11
use Del\Form\Field\FieldInterface;
12
use Del\Form\Renderer\Field\FieldRendererInterface;
13
use Del\Form\Renderer\FormRenderer;
14
use Del\Form\Renderer\FormRendererInterface;
15
use Del\Form\Traits\HasAttributesTrait;
16
17
abstract class AbstractForm implements FormInterface
18
{
19
    const ENC_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data';
20
    const ENC_TYPE_URL_ENCODED = 'application/x-www-form-urlencoded';
21
    const ENC_TYPE_TEXT_PLAIN = 'text/plain';
22
23
    const METHOD_POST = 'post';
24
    const METHOD_GET = 'get';
25
26
    /** @var FieldCollection $fieldCollection */
27
    private $fieldCollection;
28
29
    /** @var FormRendererInterface  */
30
    private $formRenderer;
31
32
    /** @var array $errorMessages */
33
    private $errorMessages;
34
35
    /** @var bool $displayErrors */
36
    private $displayErrors;
37
38
    use HasAttributesTrait;
39
40
    /**
41
     * AbstractForm constructor.
42
     * @param $name
43
     */
44 23
    public function __construct($name)
45
    {
46 23
        $this->fieldCollection = new FieldCollection();
47 23
        $this->formRenderer = new FormRenderer();
48 23
        $this->attributes = [
49 23
            'name' => $name,
50 23
            'method' => self::METHOD_POST,
51
        ];
52 23
        $this->displayErrors = false;
53 23
        $this->init();
54 23
    }
55
56
    abstract public function init();
57
58
    /**
59
     * @return bool
60
     */
61 1 View Code Duplication
    public function isValid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63 1
        $this->errorMessages = [];
64 1
        $this->fieldCollection->rewind();
65 1
        while ($this->fieldCollection->valid()) {
66 1
            $this->checkForErrors($this->fieldCollection->current());
67 1
            $this->fieldCollection->next();
68
        }
69 1
        $this->fieldCollection->rewind();
70 1
        $count = count($this->errorMessages);
71 1
        return $count == 0;
72
    }
73
74
    /**
75
     * @param FieldInterface $field
76
     */
77 1
    private function checkForErrors(FieldInterface $field)
78
    {
79 1
        if (!$field->isValid()) {
80 1
            $this->errorMessages[$field->getName()] = $field->getMessages();
81
        }
82 1
    }
83
84
    /**
85
     * @return array
86
     */
87 3
    public function getValues()
88
    {
89 3
        $values = [];
90 3
        $this->fieldCollection->rewind();
91 3
        while ($this->fieldCollection->valid()) {
92 3
            $field = $this->fieldCollection->current();
93 3
            $values[$field->getName()] = $field->getValue();
94 3
            $this->fieldCollection->next();
95
        }
96 3
        $this->fieldCollection->rewind();
97 3
        return $values;
98
    }
99
100
    /**
101
     * @param array $data
102
     * @return $this
103
     */
104 4
    public function populate(array $data)
105
    {
106 4
        $this->fieldCollection->rewind();
107 4
        while ($this->fieldCollection->valid()) {
108 4
            $field = $this->fieldCollection->current();
109 4
            $name = $field->getName();
110 4
            if (isset($data[$name])) {
111 1
                $field->setValue($data[$name]);
112
            }
113 4
            $this->fieldCollection->next();
114
        }
115 4
        $this->fieldCollection->rewind();
116 4
        $this->displayErrors = true;
117 4
        return $this;
118
    }
119
120
    /**
121
     * @param string $name
122
     * @return FieldInterface|null
123
     */
124 2
    public function getField($name)
125
    {
126 2
        return $this->fieldCollection->findByName($name);
127
    }
128
129
    /**
130
     * @return FieldCollection
131
     */
132 12
    public function getFields()
133
    {
134 12
        return $this->fieldCollection;
135
    }
136
137
    /**
138
     * @param FieldInterface $field
139
     * @return $this
140
     */
141 15
    public function addField(FieldInterface $field)
142
    {
143 15
        $this->fieldCollection->append($field);
144 15
        return $this;
145
    }
146
147
    /**
148
     * @return string
149
     */
150 10
    public function render()
151
    {
152 10
        return $this->formRenderer->render($this, $this->isDisplayErrors());
153
    }
154
155
    /**
156
     * @param $url
157
     * @return $this
158
     */
159 2
    public function setAction($url)
160
    {
161 2
        $this->setAttribute('action', $url);
162 2
        return $this;
163
    }
164
165
    /**
166
     * @return string
167
     */
168 2
    public function getAction()
169
    {
170 2
        return $this->getAttribute('action');
171
    }
172
173
    /**
174
     * @return string
175
     */
176 11
    public function getId()
177
    {
178 11
        return $this->getAttribute('id');
179
    }
180
181
    /**
182
     * @param string $id
183
     * @return $this
184
     */
185 2
    public function setId($id)
186
    {
187 2
        $this->setAttribute('id', $id);
188 2
        return $this;
189
    }
190
191
    /**
192
     * @param $encType
193
     * @return $this
194
     */
195 2
    public function setEncType($encType)
196
    {
197 2
        $this->setAttribute('enctype', $encType);
198 2
        return $this;
199
    }
200
201
    /**
202
     * @return string
203
     */
204 1
    public function getEncType()
205
    {
206 1
        return $this->getAttribute('enctype');
207
    }
208
209
    /**
210
     * @param string $method
211
     * @return FormInterface
212
     */
213 2
    public function setMethod($method)
214
    {
215 2
        $this->setAttribute('method', $method);
216 2
        return $this;
217
    }
218
219
    /**
220
     * @return string
221
     */
222 11
    public function getMethod()
223
    {
224 11
        return $this->getAttribute('method');
225
    }
226
227
    /**
228
     * @param $class
229
     * @return FormInterface
230
     */
231 2
    public function setClass($class)
232
    {
233 2
        $this->setAttribute('class', $class);
234 2
        return $this;
235
    }
236
237
    /**
238
     * @return string
239
     */
240 2
    public function getClass()
241
    {
242 2
        return $this->getAttribute('class');
243
    }
244
245
    /**
246
     * @return boolean
247
     */
248 11
    public function isDisplayErrors()
249
    {
250 11
        return $this->displayErrors;
251
    }
252
253
    /**
254
     * @param boolean $displayErrors
255
     * @return AbstractForm
256
     */
257 1
    public function setDisplayErrors($displayErrors)
258
    {
259 1
        $this->displayErrors = $displayErrors;
260 1
        return $this;
261
    }
262
263
    /**
264
     * @param FormRendererInterface $displayErrors
0 ignored issues
show
Bug introduced by
There is no parameter named $displayErrors. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
265
     * @return AbstractForm
266
     */
267 2
    public function setFormRenderer(FormRendererInterface $renderer)
268
    {
269 2
        $this->formRenderer = $renderer;
270 2
        return $this;
271
    }
272
}