Completed
Push — master ( a1a3b2...ba72b8 )
by Derek Stephen
07:25
created

AbstractForm::getId()   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
13
abstract class AbstractForm implements FormInterface
14
{
15
    const ENC_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data';
16
    const METHOD_POST = 'post';
17
    const METHOD_GET = 'get';
18
19
    /** @var FieldCollection $fieldCollection */
20
    private $fieldCollection;
21
22
    /** @var FormRenderer  */
23
    private $formRenderer;
24
25
    /**
26
     * @var array
27
     */
28
    private $errorMessages;
29
30
    /** @var array $attributes */
31
    private $attributes;
32
33
    /** @var bool $displayErrors */
34
    private $displayErrors;
35
36
    /**
37
     * AbstractForm constructor.
38
     * @param $name
39
     */
40 16
    public function __construct($name)
41
    {
42 16
        $this->fieldCollection = new FieldCollection();
43 16
        $this->formRenderer = new FormRenderer($name);
44 16
        $this->attributes = [
45 16
            'name' => null,
46 16
            'id' => null,
47 16
            'class' => null,
48 16
            'enc-type' => null,
49 16
            'method' => self::METHOD_POST,
50
        ];
51 16
        $this->displayErrors = false;
52 16
        $this->init();
53 16
    }
54
55
    abstract public function init();
56
57
    /**
58
     * @return bool
59
     */
60 1
    public function isValid()
61
    {
62 1
        $this->hasBeenPopulatedOrValidated = true;
0 ignored issues
show
Bug introduced by
The property hasBeenPopulatedOrValidated does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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 1
        }
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 1
        }
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 3
        }
96 3
        $this->fieldCollection->rewind();
97 3
        return $values;
98
    }
99
100
    /**
101
     * @param array $data
102
     * @return $this
103
     */
104 2
    public function populate(array $data)
105
    {
106 2
        $this->fieldCollection->rewind();
107 2
        while ($this->fieldCollection->valid()) {
108 2
            $field = $this->fieldCollection->current();
109 2
            $name = $field->getName();
110 2
            if (isset($data[$name])) {
111 1
                $field->setValue($data[$name]);
112 1
            }
113 2
            $this->fieldCollection->next();
114 2
        }
115 2
        $this->fieldCollection->rewind();
116 2
        $this->displayErrors = true;
117 2
        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 6
    public function getFields()
133
    {
134 6
        return $this->fieldCollection;
135
    }
136
137
    /**
138
     * @param FieldInterface $field
139
     * @return $this
140
     */
141 9
    public function addField(FieldInterface $field)
142
    {
143 9
        $this->fieldCollection->append($field);
144 9
        return $this;
145
    }
146
147
    /**
148
     * @return string
149
     */
150 4
    public function render()
151
    {
152 4
        return $this->formRenderer->render($this, $this->displayErrors);
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 5
    public function getAction()
169
    {
170 5
        return $this->getAttribute('action');
171
    }
172
173
    /**
174
     * @return string
175
     */
176 5
    public function getId()
177
    {
178 5
        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 5
    public function getEncType()
205
    {
206 5
        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 5
    public function getMethod()
223
    {
224 5
        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 5
    public function getClass()
241
    {
242 5
        return $this->getAttribute('class');
243
    }
244
245
    /**
246
     * @param $key
247
     * @return mixed|string
248
     */
249 9
    public function getAttribute($key)
250
    {
251 9
        return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
252
    }
253
254
    /**
255
     * @param $key
256
     * @param $value
257
     * @return $this
258
     */
259 6
    public function setAttribute($key, $value)
260
    {
261 6
        $this->attributes[$key] = $value;
262 6
        return $this;
263
    }
264
265
}