Element::getId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ntentan\honam\engines\php\helpers\form;
4
5
use ntentan\honam\engines\php\Variable;
6
use ntentan\honam\TemplateRenderer;
7
8
9
/**
10
 * The form element class. 
11
 * An element can be anything from the form itself to the elements on the form. This class provides an abstract render 
12
 * method that elements use to output their HTML code. Any visible element of the form must have this class as a parent.
13
 */
14
abstract class Element
15
{
16
    use ElementFactory;
17
18
    /**
19
     * The id of the form useful for CSS styling and DOM access.
20
     * 
21
     * @var string
22
     */
23
    protected $id = "";
24
25
    /**
26
     * The label of the form element.
27
     * 
28
     * @var string
29
     */
30
    protected $label = "";
31
32
    /**
33
     * The description of the form element.
34
     * 
35
     * @var string
36
     */
37
    protected $description = "";
38
39
    /**
40
     * An array of all the CSS classes associated with this element.
41
     *
42
     * @var array 
43
     */
44
    protected $classes = [];
45
46
    /**
47
     * An array of all HTML attributes. These attributes are stored as
48
     * objects of the Attribute class. Attributes in this array are applied
49
     * directly to the form element.
50
     *
51
     * @var array 
52
     */
53
    protected $attributes = [];
54
55
    /**
56
     * An array of all HTML attributes. These attributes are stored as
57
     * objects of the Attribute class. Attributes in this array are applied
58
     * directly to the wrapper which wraps the form element.
59
     *
60
     * @var array 
61
     */
62
    //protected $wrapperAttributes = [];
63
64
    /**
65
     * An array of all error messages associated with this element.
66
     * Error messages are setup during validation, when any element
67
     * fails its validation test.
68
     *
69
     * @var array
70
     */
71
    protected $errors = array();
72
73
    /**
74
     * A boolean value which is set to true whenever there is an error 
75
     * assiciated with the field element in one way or the other.
76
     * @var boolean
77
     */
78
    protected $error = false;
79
80
    protected $name;
81
82
    protected $hasLabel = true;
83
84
    protected $templateRenderer;
85
86
    public function __construct(string $label = "")
87
    {
88
        $this->setLabel($label);
89
    }
90
91
    public function setId(string $id): Element
92
    {
93
        $this->id = $id;
94
        $this->setAttribute('id', $this->id);
95
        return $this;
96
    }
97
98
    public function getId(): string
99
    {
100
        return $this->id;
101
    }
102
103
    /**
104
     * Public accessor for setting the name property of the field.
105
     *
106
     * @param  string $name
107
     * @return Element
108
     */
109
    public function setName($name): Element
110
    {
111
        $this->name = $name;
112
        return $this;
113
    }
114
115
    /**
116
     * Public accessor for getting the name property of the field.
117
     *
118
     * @return string
119
     */
120
    public function getName(): string
121
    {
122
        return $this->name;
123
    }
124
125
    /**
126
     * Set a label for the form element.
127
     */
128
    public function setLabel(string $label): Element
129
    {
130
        $this->label = $label;
131
        return $this;
132
    }
133
134
    /**
135
     * Get the label for the form element.
136
     */
137
    public function getLabel(): string
138
    {
139
        return $this->label;
140
    }
141
142
    /**
143
     * Gets the description which is attached to this element. The description is normally displayed under the element 
144
     * when rendering HTML.
145
     *
146
     * @return string
147
     */
148
    public function getDescription(): string
149
    {
150
        return $this->description;
151
    }
152
153
    /**
154
     * Sets the description which is attached to this element. The description is normally displayed under the element 
155
     * when rendering HTML.
156
     *
157
     * @return Element
158
     */
159
    public function setDescription(string $description): Element
160
    {
161
        $this->description = $description;
162
        return $this;
163
    }
164
165
    /**
166
     * Returns all the arrays associated with this document.
167
     *
168
     * @return array 
169
     */
170
    public function getErrors(): array|Variable
171
    {
172
        return $this->errors;
173
    }
174
175
    public function hasError(): bool
176
    {
177
        return $this->error;
178
    }
179
180
    /**
181
     * Renders the form element by outputing the HTML associated with the element. This method is abstract and it is 
182
     * implemented by all the other classes which inherit the Element class.
183
     *
184
     * @return string
185
     */
186
    abstract public function render();
187
188
    public function __toString()
189
    {
190
        return $this->templateRenderer->render("forms_layout_element.tpl.php", array('element' => $this));
191
    }
192
193
    /**
194
     * Adds an attribute to the element or its surrounding scope.
195
     */
196
    public function setAttribute(string $attribute, string $value) : Element
197
    {
198
        $this->attributes[$attribute] = $value;
199
        return $this;
200
    }
201
202
    /**
203
     * Returns an HTML representation of all the attributes. This method is normally called when rendering the HTML for 
204
     * the element.
205
     */
206
    public function getAttributes() : array
207
    {
208
        return $this->attributes;
209
    }
210
211
    public function getAttribute(string $attribute) : ?string
212
    {
213
        return $this->attributes[$attribute] ?? null;
214
    }
215
216
    public function setErrors(array|Variable $errors) : Element
217
    {
218
        $this->errors = $errors;
219
        $this->error = true;
220
        return $this;
221
    }
222
223
    public function setTemplateRenderer(TemplateRenderer $templateRenderer)
224
    {
225
        $this->templateRenderer = $templateRenderer;
226
    }
227
228
    public function getHasLabel(): bool
229
    {
230
        return $this->hasLabel;
231
    }
232
}
233