Passed
Branch main (e68017)
by James Ekow Abaka
10:11
created

Element::getCSSClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ntentan\honam\engines\php\helpers\form;
4
5
use ntentan\honam\TemplateRenderer;
6
7
8
/**
9
 * The form element class. 
10
 * An element can be anything from the form itself to the elements on the form. This class provides an abstract render 
11
 * method that elements use to output their HTML code. Any visible element of the form must have this class as a parent.
12
 */
13
abstract class Element
14
{
15
    use ElementFactory;
16
17
    /**
18
     * The id of the form useful for CSS styling and DOM access.
19
     * 
20
     * @var string
21
     */
22
    protected $id = "";
23
24
    /**
25
     * The label of the form element.
26
     * 
27
     * @var string
28
     */
29
    protected $label;
30
31
    /**
32
     * The description of the form element.
33
     * 
34
     * @var string
35
     */
36
    protected $description;
37
38
    /**
39
     * An array of all the CSS classes associated with this element.
40
     *
41
     * @var array 
42
     */
43
    protected $classes = [];
44
45
    /**
46
     * An array of all HTML attributes. These attributes are stored as
47
     * objects of the Attribute class. Attributes in this array are applied
48
     * directly to the form element.
49
     *
50
     * @var array 
51
     */
52
    protected $attributes = [];
53
54
    /**
55
     * An array of all HTML attributes. These attributes are stored as
56
     * objects of the Attribute class. Attributes in this array are applied
57
     * directly to the wrapper which wraps the form element.
58
     *
59
     * @var array 
60
     */
61
    //protected $wrapperAttributes = [];
62
63
    /**
64
     * An array of all error messages associated with this element.
65
     * Error messages are setup during validation, when any element
66
     * fails its validation test.
67
     *
68
     * @var array
69
     */
70
    protected $errors = array();
71
72
    /**
73
     * A boolean value which is set to true whenever there is an error 
74
     * assiciated with the field element in one way or the other.
75
     * @var boolean
76
     */
77
    protected $error = false;
78
79
    /**
80
     * The parent element which contains this element.
81
     * @var Element
82
     */
83
    //protected $parent = null;
84
85
    protected $name;
86
87
    protected $renderLabel = true;
88
89
    protected $templateRenderer;
90
91
    public function __construct(string $label = "")
92
    {
93
        $this->setLabel($label);
94
    }
95
96
    public function setId(string $id): Element
97
    {
98
        $this->id = $id;
99
        $this->setAttribute('id', $this->id);
100
        return $this;
101
    }
102
103
    public function getId(): string
104
    {
105
        return $this->id;
106
    }
107
108
    /**
109
     * Public accessor for setting the name property of the field.
110
     *
111
     * @param  string $name
112
     * @return Element
113
     */
114
    public function setName($name): Element
115
    {
116
        $this->name = $name;
0 ignored issues
show
Documentation Bug introduced by
It seems like $name of type string is incompatible with the declared type ntentan\honam\engines\php\helpers\form\Element of property $name.

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...
117
        return $this;
118
    }
119
120
    /**
121
     * Public accessor for getting the name property of the field.
122
     *
123
     * @return string
124
     */
125
    public function getName(): string
126
    {
127
        return $this->name;
128
    }
129
130
    /**
131
     * Set a label for the form element.
132
     */
133
    public function setLabel(string $label): Element
134
    {
135
        $this->label = $label;
136
        return $this;
137
    }
138
139
    /**
140
     * Get the label for the form element.
141
     */
142
    public function getLabel(): string
143
    {
144
        return $this->label;
145
    }
146
147
    /**
148
     * Gets the description which is attached to this element. The description
149
     * is normally displayed under the element when rendering HTML.
150
     *
151
     * @return string
152
     */
153
    public function getDescription(): string
154
    {
155
        return $this->description;
156
    }
157
158
    /**
159
     * Sets the description which is attached to this element. The description
160
     * is normally displayed under the element when rendering HTML.
161
     *
162
     * @return Element
163
     */
164
    public function setDescription($description): Element
165
    {
166
        $this->description = $description;
167
        return $this;
168
    }
169
170
    /**
171
     * Returns all the arrays associated with this document.
172
     *
173
     * @return array 
174
     */
175
    public function getErrors(): array
176
    {
177
        return $this->errors;
178
    }
179
180
    // Returns the error flag for this element.
181
    public function hasError(): bool
182
    {
183
        return $this->error;
184
    }
185
186
    /**
187
     * Renders the form element by outputing the HTML associated with
188
     * the element. This method is abstract and it is implemented by
189
     * all the other classes which inherit the Element class.
190
     *
191
     * @return string
192
     */
193
    abstract public function render();
194
195
    public function __toString()
196
    {
197
        return $this->templateRenderer->render("forms_layout_element.tpl.php", array('element' => $this));
198
    }
199
200
    /**
201
     * Adds an attribute to the element or its surrounding scope.
202
     */
203
    public function setAttribute(string $attribute, string $value) : Element
204
    {
205
        $this->attributes[$attribute] = $value;
206
        return $this;
207
    }
208
209
    /**
210
     * Returns an HTML representation of all the attributes. This method is 
211
     * normally called when rendering the HTML for the element.
212
     */
213
    public function getAttributes() : array
214
    {
215
        return $this->attributes;
216
    }
217
218
    public function getAttribute(string $attribute) : ?string
219
    {
220
        return $this->attributes[$attribute] ?? null;
221
    }
222
223
    public function setErrors(array $errors) : Element
224
    {
225
        $this->errors = $errors;
226
        $this->error = true;
227
        return $this;
228
    }
229
230
    public function setTemplateRenderer(TemplateRenderer $templateRenderer)
231
    {
232
        $this->templateRenderer = $templateRenderer;
233
    }
234
}
235