Completed
Branch dev-master (5a9550)
by Derek Stephen
02:00
created

AbstractForm::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 19
    public function __construct($name)
45
    {
46 19
        $this->fieldCollection = new FieldCollection();
47 19
        $this->formRenderer = new FormRenderer($name);
48 19
        $this->attributes = [
49 19
            'method' => self::METHOD_POST,
50
        ];
51 19
        $this->displayErrors = false;
52 19
        $this->init();
53 19
    }
54
55
    abstract public function init();
56
57
    /**
58
     * @return bool
59
     */
60 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...
61
    {
62 1
        $this->errorMessages = [];
63 1
        $this->fieldCollection->rewind();
64 1
        while ($this->fieldCollection->valid()) {
65 1
            $this->checkForErrors($this->fieldCollection->current());
66 1
            $this->fieldCollection->next();
67
        }
68 1
        $this->fieldCollection->rewind();
69 1
        $count = count($this->errorMessages);
70 1
        return $count == 0;
71
    }
72
73
    /**
74
     * @param FieldInterface $field
75
     */
76 1
    private function checkForErrors(FieldInterface $field)
77
    {
78 1
        if (!$field->isValid()) {
79 1
            $this->errorMessages[$field->getName()] = $field->getMessages();
80
        }
81 1
    }
82
83
    /**
84
     * @return array
85
     */
86 3
    public function getValues()
87
    {
88 3
        $values = [];
89 3
        $this->fieldCollection->rewind();
90 3
        while ($this->fieldCollection->valid()) {
91 3
            $field = $this->fieldCollection->current();
92 3
            $values[$field->getName()] = $field->getValue();
93 3
            $this->fieldCollection->next();
94
        }
95 3
        $this->fieldCollection->rewind();
96 3
        return $values;
97
    }
98
99
    /**
100
     * @param array $data
101
     * @return $this
102
     */
103 3
    public function populate(array $data)
104
    {
105 3
        $this->fieldCollection->rewind();
106 3
        while ($this->fieldCollection->valid()) {
107 3
            $field = $this->fieldCollection->current();
108 3
            $name = $field->getName();
109 3
            if (isset($data[$name])) {
110 1
                $field->setValue($data[$name]);
111
            }
112 3
            $this->fieldCollection->next();
113
        }
114 3
        $this->fieldCollection->rewind();
115 3
        $this->displayErrors = true;
116 3
        return $this;
117
    }
118
119
    /**
120
     * @param string $name
121
     * @return FieldInterface|null
122
     */
123 2
    public function getField($name)
124
    {
125 2
        return $this->fieldCollection->findByName($name);
126
    }
127
128
    /**
129
     * @return FieldCollection
130
     */
131 8
    public function getFields()
132
    {
133 8
        return $this->fieldCollection;
134
    }
135
136
    /**
137
     * @param FieldInterface $field
138
     * @return $this
139
     */
140 11
    public function addField(FieldInterface $field)
141
    {
142 11
        $this->fieldCollection->append($field);
143 11
        return $this;
144
    }
145
146
    /**
147
     * @return string
148
     */
149 6
    public function render()
150
    {
151 6
        return $this->formRenderer->render($this, $this->isDisplayErrors());
152
    }
153
154
    /**
155
     * @param $url
156
     * @return $this
157
     */
158 2
    public function setAction($url)
159
    {
160 2
        $this->setAttribute('action', $url);
161 2
        return $this;
162
    }
163
164
    /**
165
     * @return string
166
     */
167 2
    public function getAction()
168
    {
169 2
        return $this->getAttribute('action');
170
    }
171
172
    /**
173
     * @return string
174
     */
175 7
    public function getId()
176
    {
177 7
        return $this->getAttribute('id');
178
    }
179
180
    /**
181
     * @param string $id
182
     * @return $this
183
     */
184 2
    public function setId($id)
185
    {
186 2
        $this->setAttribute('id', $id);
187 2
        return $this;
188
    }
189
190
    /**
191
     * @param $encType
192
     * @return $this
193
     */
194 2
    public function setEncType($encType)
195
    {
196 2
        $this->setAttribute('enctype', $encType);
197 2
        return $this;
198
    }
199
200
    /**
201
     * @return string
202
     */
203 1
    public function getEncType()
204
    {
205 1
        return $this->getAttribute('enctype');
206
    }
207
208
    /**
209
     * @param string $method
210
     * @return FormInterface
211
     */
212 2
    public function setMethod($method)
213
    {
214 2
        $this->setAttribute('method', $method);
215 2
        return $this;
216
    }
217
218
    /**
219
     * @return string
220
     */
221 7
    public function getMethod()
222
    {
223 7
        return $this->getAttribute('method');
224
    }
225
226
    /**
227
     * @param $class
228
     * @return FormInterface
229
     */
230 2
    public function setClass($class)
231
    {
232 2
        $this->setAttribute('class', $class);
233 2
        return $this;
234
    }
235
236
    /**
237
     * @return string
238
     */
239 2
    public function getClass()
240
    {
241 2
        return $this->getAttribute('class');
242
    }
243
244
    /**
245
     * @return boolean
246
     */
247 7
    public function isDisplayErrors()
248
    {
249 7
        return $this->displayErrors;
250
    }
251
252
    /**
253
     * @param boolean $displayErrors
254
     * @return AbstractForm
255
     */
256 1
    public function setDisplayErrors($displayErrors)
257
    {
258 1
        $this->displayErrors = $displayErrors;
259 1
        return $this;
260
    }
261
262
    /**
263
     * @param FieldRendererInterface $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...
264
     * @return AbstractForm
265
     */
266
    public function setFormRenderer(FieldRendererInterface $renderer)
267
    {
268
        $this->formRenderer = $renderer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $renderer of type object<Del\Form\Renderer...FieldRendererInterface> is incompatible with the declared type object<Del\Form\Renderer\FormRendererInterface> of property $formRenderer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
269
        return $this;
270
    }
271
}