Completed
Push — master ( 24655e...ed68b6 )
by Derek Stephen
01:53
created

AbstractForm::createChildElement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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 DOMDocument;
13
use DOMElement;
14
15
abstract class AbstractForm implements FormInterface
16
{
17
    const ENC_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data';
18
    const METHOD_POST = 'post';
19
    const METHOD_GET = 'get';
20
21
    /** @var FieldCollection $fieldCollection */
22
    private $fieldCollection;
23
24
    /** @var DOMDocument $dom */
25
    private $dom;
26
27
    /** @var DomElement $form */
28
    private $form;
29
30 11
    public function __construct($name)
31
    {
32 11
        $this->fieldCollection = new FieldCollection();
33 11
        $this->dom = new DOMDocument();
34 11
        $form = $this->dom->createElement('form');
35 11
        $form->setAttribute('name', $name);
36 11
        $this->form = $form;
0 ignored issues
show
Documentation Bug introduced by
It seems like $form of type object<DOMElement> is incompatible with the declared type object<Del\Form\DomElement> of property $form.

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...
37 11
        $this->init();
38 11
    }
39
40
    abstract public function init();
41
42
    /**
43
     * @return bool
44
     */
45
    public function isValid()
46
    {
47
        return false;
48
    }
49
50
    /**
51
     * @return array
52
     */
53 2
    public function getValues()
54
    {
55 2
        $values = [];
56
        /** @var FieldInterface $field */
57 2
        foreach ($this->fieldCollection as $field) {
58 2
            $values[$field->getName()] = $field->getValue();
59 2
        }
60 2
        return $values;
61
    }
62
63
    /**
64
     * @param array $data
65
     * @return $this
66
     */
67 1
    public function populate(array $data)
68
    {
69
        /** @var FieldInterface $field */
70 1
        foreach ($this->fieldCollection as $field) {
71 1
            $name = $field->getName();
72 1
            if (isset($data[$name])) {
73 1
                $field->setValue($data[$name]);
74 1
            }
75 1
        }
76 1
        return $this;
77
    }
78
79
    /**
80
     * @param string $name
81
     * @return FieldInterface|null
82
     */
83 1
    public function getField($name)
84
    {
85 1
        return $this->fieldCollection->findByName($name);
86
    }
87
88
    /**
89
     * @return FieldCollection
90
     */
91 2
    public function getFields()
92
    {
93 2
        return $this->fieldCollection;
94
    }
95
96
    /**
97
     * @param FieldInterface $field
98
     * @return $this
99
     */
100 4
    public function addField(FieldInterface $field)
101
    {
102 4
        $this->fieldCollection->append($field);
103 4
        return $this;
104
    }
105
106
    /**
107
     * @return string
108
     */
109 2
    public function render()
110
    {
111 2
        $method = $this->form->getAttribute('method') ?: self::METHOD_POST;
112 2
        $id = $this->form->getAttribute('id') ?: $this->form->getAttribute('name');
113 2
        $action = $this->form->getAttribute('action') ?: $this->form->getAttribute('action');
114
115 2
        $this->form->setAttribute('id', $id);
116 2
        $this->form->setAttribute('method', $method);
117 2
        $this->form->setAttribute('action', $action);
118
119 2
        $this->fieldCollection->rewind();
120 2
        while ($this->fieldCollection->valid()) {
121
            /** @var FieldInterface $current */
122 1
            $current = $this->fieldCollection->current();
123 1
            $child = $this->createChildElement($current);
124 1
            $this->form->appendChild($child);
125 1
            $this->fieldCollection->next();
126 1
        }
127 2
        $this->fieldCollection->rewind();
128
129 2
        $this->dom->appendChild($this->form);
130
131 2
        return $this->dom->saveHTML();
132
    }
133
134
    /**
135
     * @param FieldInterface $field
136
     * @return DOMElement
137
     */
138 1
    private function createChildElement(FieldInterface $field)
139
    {
140 1
        $child = $this->dom->createElement($field->getTag());
141
142 1
        $child->setAttribute('type', $field->getTagType());
143 1
        $child->setAttribute('name', $field->getName());
144 1
        $child->setAttribute('id', $field->getId());
145 1
        $child->setAttribute('value', $field->getValue());
146 1
        $child->setAttribute('class', $field->getClass());
147
148 1
        return $child;
149
    }
150
151
    /**
152
     * @param $url
153
     * @return $this
154
     */
155 2
    public function setAction($url)
156
    {
157 2
        $this->form->setAttribute('action', $url);
158 2
        return $this;
159
    }
160
161
    /**
162
     * @return string
163
     */
164 2
    public function getAction()
165
    {
166 2
        return $this->form->getAttribute('action');
167
    }
168
169
    /**
170
     * @return string
171
     */
172 2
    public function getId()
173
    {
174 2
        return $this->form->getAttribute('id');
175
    }
176
177
    /**
178
     * @param string $id
179
     * @return $this
180
     */
181 2
    public function setId($id)
182
    {
183 2
        $this->form->setAttribute('id', $id);
184 2
        return $this;
185
    }
186
187
    /**
188
     * @param $encType
189
     * @return $this
190
     */
191 2
    public function setEncType($encType)
192
    {
193 2
        $this->form->setAttribute('enctype', $encType);
194 2
        return $this;
195
    }
196
197
    /**
198
     * @return string
199
     */
200 1
    public function getEncType()
201
    {
202 1
        return $this->form->getAttribute('enctype');
203
    }
204
205
    /**
206
     * @param string $method
207
     * @return FormInterface
208
     */
209 2
    public function setMethod($method)
210
    {
211 2
        $this->form->setAttribute('method', $method);
212 2
        return $this;
213
    }
214
215
    /**
216
     * @return string
217
     */
218 1
    public function getMethod()
219
    {
220 1
        return $this->form->getAttribute('method');
221
    }
222
223
    /**
224
     * @param $class
225
     * @return FormInterface
226
     */
227 2
    public function setClass($class)
228
    {
229 2
        $this->form->setAttribute('class', $class);
230 2
        return $this;
231
    }
232
233
    /**
234
     * @return string
235
     */
236 2
    public function getClass()
237
    {
238 2
        return $this->form->getAttribute('class');
239
    }
240
}