Completed
Push — master ( d913d0...312f5c )
by Derek Stephen
07:27
created

src/AbstractForm.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Renderer\FormRendererInterface;
14
use Del\Form\Traits\HasAttributesTrait;
15
16
abstract class AbstractForm implements FormInterface
17
{
18
    const ENC_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data';
19
    const ENC_TYPE_URL_ENCODED = 'application/x-www-form-urlencoded';
20
    const ENC_TYPE_TEXT_PLAIN = 'text/plain';
21
22
    const METHOD_POST = 'post';
23
    const METHOD_GET = 'get';
24
25
    /** @var FieldCollection $fieldCollection */
26
    private $fieldCollection;
27
28
    /** @var FormRendererInterface  */
29
    private $formRenderer;
30
31
    /** @var array $errorMessages */
32
    private $errorMessages;
33
34
    /** @var bool $displayErrors */
35
    private $displayErrors;
36
37
    use HasAttributesTrait;
38
39
    /**
40
     * AbstractForm constructor.
41
     * @param $name
42
     */
43 40
    public function __construct($name)
44
    {
45 40
        $this->fieldCollection = new FieldCollection();
46 40
        $this->formRenderer = new FormRenderer();
47 40
        $this->attributes = [
48 40
            'name' => $name,
49 40
            'method' => self::METHOD_POST,
50
        ];
51 40
        $this->displayErrors = false;
52 40
        $this->init();
53 40
    }
54
55
    abstract public function init();
56
57
    /**
58
     * @return bool
59
     */
60 4 View Code Duplication
    public function isValid()
0 ignored issues
show
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 4
        $this->errorMessages = [];
63 4
        $this->fieldCollection->rewind();
64 4
        while ($this->fieldCollection->valid()) {
65 4
            $this->checkForErrors($this->fieldCollection->current());
66 4
            $this->fieldCollection->next();
67
        }
68 4
        $this->fieldCollection->rewind();
69 4
        $count = count($this->errorMessages);
70 4
        return $count == 0;
71
    }
72
73
    /**
74
     * @param FieldInterface $field
75
     */
76 4
    private function checkForErrors(FieldInterface $field)
77
    {
78 4
        if (!$field->isValid()) {
79 4
            $this->errorMessages[$field->getName()] = $field->getMessages();
80
        }
81 4
    }
82
83
    /**
84
     * @return array
85
     */
86 4
    public function getValues()
87
    {
88 4
        $values = [];
89 4
        $this->fieldCollection->rewind();
90 4
        while ($this->fieldCollection->valid()) {
91 4
            $field = $this->fieldCollection->current();
92 4
            $values[$field->getName()] = $field->getValue();
93 4
            $this->fieldCollection->next();
94
        }
95 4
        $this->fieldCollection->rewind();
96 4
        return $values;
97
    }
98
99
    /**
100
     * @param array $data
101
     * @return $this
102
     */
103 5
    public function populate(array $data)
104
    {
105 5
        $this->fieldCollection->rewind();
106 5
        while ($this->fieldCollection->valid()) {
107 5
            $field = $this->fieldCollection->current();
108 5
            $name = $field->getName();
109 5
            if (isset($data[$name])) {
110 2
                $field->setValue($data[$name]);
111
            }
112 5
            $this->fieldCollection->next();
113
        }
114 5
        $this->fieldCollection->rewind();
115 5
        $this->displayErrors = true;
116 5
        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 25
    public function getFields()
132
    {
133 25
        return $this->fieldCollection;
134
    }
135
136
    /**
137
     * @param FieldInterface $field
138
     * @return $this
139
     */
140 31
    public function addField(FieldInterface $field)
141
    {
142 31
        $this->fieldCollection->append($field);
143 31
        return $this;
144
    }
145
146
    /**
147
     * @return string
148
     */
149 23
    public function render()
150
    {
151 23
        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 24
    public function getId()
176
    {
177 24
        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 24
    public function getMethod()
222
    {
223 24
        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 24
    public function isDisplayErrors()
248
    {
249 24
        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 FormRendererInterface $renderer
264
     * @return AbstractForm
265
     */
266 2
    public function setFormRenderer(FormRendererInterface $renderer)
267
    {
268 2
        $this->formRenderer = $renderer;
269 2
        return $this;
270
    }
271
}