Completed
Pull Request — master (#1)
by Derek Stephen
02:21
created

AbstractForm::setAttribute()   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 2
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\FormRenderer;
13
use Del\Form\Traits\HasAttributesTrait;
14
15
abstract class AbstractForm implements FormInterface
16
{
17
    const ENC_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data';
18
    const ENC_TYPE_URL_ENCODED = 'application/x-www-form-urlencoded';
19
    const ENC_TYPE_TEXT_PLAIN = 'text/plain';
20
21
    const METHOD_POST = 'post';
22
    const METHOD_GET = 'get';
23
24
    /** @var FieldCollection $fieldCollection */
25
    private $fieldCollection;
26
27
    /** @var FormRenderer  */
28
    private $formRenderer;
29
30
    /** @var array $errorMessages */
31
    private $errorMessages;
32
33
    /** @var bool $displayErrors */
34
    private $displayErrors;
35
36
    use HasAttributesTrait;
37
38
    /**
39
     * AbstractForm constructor.
40
     * @param $name
41
     */
42 19
    public function __construct($name)
43
    {
44 19
        $this->fieldCollection = new FieldCollection();
45 19
        $this->formRenderer = new FormRenderer($name);
46 19
        $this->attributes = [
47 19
            'method' => self::METHOD_POST,
48
        ];
49 19
        $this->displayErrors = false;
50 19
        $this->init();
51 19
    }
52
53
    abstract public function init();
54
55
    /**
56
     * @return bool
57
     */
58 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...
59
    {
60 1
        $this->errorMessages = [];
61 1
        $this->fieldCollection->rewind();
62 1
        while ($this->fieldCollection->valid()) {
63 1
            $this->checkForErrors($this->fieldCollection->current());
64 1
            $this->fieldCollection->next();
65
        }
66 1
        $this->fieldCollection->rewind();
67 1
        $count = count($this->errorMessages);
68 1
        return $count == 0;
69
    }
70
71
    /**
72
     * @param FieldInterface $field
73
     */
74 1
    private function checkForErrors(FieldInterface $field)
75
    {
76 1
        if (!$field->isValid()) {
77 1
            $this->errorMessages[$field->getName()] = $field->getMessages();
78
        }
79 1
    }
80
81
    /**
82
     * @return array
83
     */
84 3
    public function getValues()
85
    {
86 3
        $values = [];
87 3
        $this->fieldCollection->rewind();
88 3
        while ($this->fieldCollection->valid()) {
89 3
            $field = $this->fieldCollection->current();
90 3
            $values[$field->getName()] = $field->getValue();
91 3
            $this->fieldCollection->next();
92
        }
93 3
        $this->fieldCollection->rewind();
94 3
        return $values;
95
    }
96
97
    /**
98
     * @param array $data
99
     * @return $this
100
     */
101 3
    public function populate(array $data)
102
    {
103 3
        $this->fieldCollection->rewind();
104 3
        while ($this->fieldCollection->valid()) {
105 3
            $field = $this->fieldCollection->current();
106 3
            $name = $field->getName();
107 3
            if (isset($data[$name])) {
108 1
                $field->setValue($data[$name]);
109
            }
110 3
            $this->fieldCollection->next();
111
        }
112 3
        $this->fieldCollection->rewind();
113 3
        $this->displayErrors = true;
114 3
        return $this;
115
    }
116
117
    /**
118
     * @param string $name
119
     * @return FieldInterface|null
120
     */
121 2
    public function getField($name)
122
    {
123 2
        return $this->fieldCollection->findByName($name);
124
    }
125
126
    /**
127
     * @return FieldCollection
128
     */
129 8
    public function getFields()
130
    {
131 8
        return $this->fieldCollection;
132
    }
133
134
    /**
135
     * @param FieldInterface $field
136
     * @return $this
137
     */
138 11
    public function addField(FieldInterface $field)
139
    {
140 11
        $this->fieldCollection->append($field);
141 11
        return $this;
142
    }
143
144
    /**
145
     * @return string
146
     */
147 6
    public function render()
148
    {
149 6
        return $this->formRenderer->render($this, $this->isDisplayErrors());
150
    }
151
152
    /**
153
     * @param $url
154
     * @return $this
155
     */
156 2
    public function setAction($url)
157
    {
158 2
        $this->setAttribute('action', $url);
159 2
        return $this;
160
    }
161
162
    /**
163
     * @return string
164
     */
165 7
    public function getAction()
166
    {
167 7
        return $this->getAttribute('action');
168
    }
169
170
    /**
171
     * @return string
172
     */
173 7
    public function getId()
174
    {
175 7
        return $this->getAttribute('id');
176
    }
177
178
    /**
179
     * @param string $id
180
     * @return $this
181
     */
182 2
    public function setId($id)
183
    {
184 2
        $this->setAttribute('id', $id);
185 2
        return $this;
186
    }
187
188
    /**
189
     * @param $encType
190
     * @return $this
191
     */
192 2
    public function setEncType($encType)
193
    {
194 2
        $this->setAttribute('enctype', $encType);
195 2
        return $this;
196
    }
197
198
    /**
199
     * @return string
200
     */
201 7
    public function getEncType()
202
    {
203 7
        return $this->getAttribute('enctype');
204
    }
205
206
    /**
207
     * @param string $method
208
     * @return FormInterface
209
     */
210 2
    public function setMethod($method)
211
    {
212 2
        $this->setAttribute('method', $method);
213 2
        return $this;
214
    }
215
216
    /**
217
     * @return string
218
     */
219 7
    public function getMethod()
220
    {
221 7
        return $this->getAttribute('method');
222
    }
223
224
    /**
225
     * @param $class
226
     * @return FormInterface
227
     */
228 2
    public function setClass($class)
229
    {
230 2
        $this->setAttribute('class', $class);
231 2
        return $this;
232
    }
233
234
    /**
235
     * @return string
236
     */
237 7
    public function getClass()
238
    {
239 7
        return $this->getAttribute('class');
240
    }
241
242
    /**
243
     * @return boolean
244
     */
245 7
    public function isDisplayErrors()
246
    {
247 7
        return $this->displayErrors;
248
    }
249
250
    /**
251
     * @param boolean $displayErrors
252
     * @return AbstractForm
253
     */
254 1
    public function setDisplayErrors($displayErrors)
255
    {
256 1
        $this->displayErrors = $displayErrors;
257 1
        return $this;
258
    }
259
}