Passed
Pull Request — master (#6)
by James Ekow Abaka
02:52
created

Element::setLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Abstract form elements
4
 * 
5
 * Ntentan Framework
6
 * Copyright (c) 2008-2012 James Ekow Abaka Ainooson
7
 * 
8
 * Permission is hereby granted, free of charge, to any person obtaining
9
 * a copy of this software and associated documentation files (the
10
 * "Software"), to deal in the Software without restriction, including
11
 * without limitation the rights to use, copy, modify, merge, publish,
12
 * distribute, sublicense, and/or sell copies of the Software, and to
13
 * permit persons to whom the Software is furnished to do so, subject to
14
 * the following conditions:
15
 * 
16
 * The above copyright notice and this permission notice shall be
17
 * included in all copies or substantial portions of the Software.
18
 * 
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
26
 * 
27
 * @author James Ainooson <[email protected]>
28
 * @copyright Copyright 2010 James Ekow Abaka Ainooson
29
 * @license MIT
30
 */
31
32
namespace ntentan\honam\engines\php\helpers\form;
33
34
35
/**
36
 * The form element class. An element can be anything from the form
37
 * itself to the objects that are put in the form. Provides an
38
 * abstract render class that is used to output the HTML associated
39
 * with this form element. All visible elements of the form must be
40
 * subclasses of the element class.
41
 */
42
abstract class Element
43
{
44
    const SCOPE_ELEMENT = "";
45
    const SCOPE_WRAPPER = "_wrapper";
46
47
    /**
48
     * The id of the form useful for CSS styling and DOM access.
49
     * 
50
     * @var string
51
     */
52
    protected $id;
53
54
    /**
55
     * The label of the form element.
56
     * 
57
     * @var string
58
     */
59
    protected $label;
60
61
    /**
62
     * The description of the form element.
63
     * 
64
     * @var string
65
     */
66
    protected $description;
67
68
    /**
69
     * An array of all the CSS classes associated with this element.
70
     *
71
     * @var array 
72
     */
73
    protected $classes = array();
74
    
75
    /**
76
     * An array of all HTML attributes. These attributes are stored as
77
     * objects of the Attribute class. Attributes in this array are applied
78
     * directly to the form element.
79
     *
80
     * @var array 
81
     */    
82
    protected $attributes = array();
83
    
84
    /**
85
     * An array of all HTML attributes. These attributes are stored as
86
     * objects of the Attribute class. Attributes in this array are applied
87
     * directly to the wrapper which wraps the form element.
88
     *
89
     * @var array 
90
     */    
91
    protected $wrapperAttributes = array();
92
    
93
    /**
94
     * An array of all error messages associated with this element.
95
     * Error messages are setup during validation, when any element
96
     * fails its validation test.
97
     *
98
     * @var array
99
     */
100
    protected $errors = array();
101
102
    /**
103
     * A boolean value which is set to true whenever there is an error 
104
     * assiciated with the field element in one way or the other.
105
     */
106
    protected $error;
107
108
    /**
109
     * The parent element which contains this element.
110
     * @var Element
111
     */
112
    protected $parent = null;
113
114
    protected $name;
115
116
    protected $renderLabel = true;
117
118
    protected $templateRenderer;
119
120 9
    public function __construct($label="", $description="", $id="")
121
    {
122 9
        $this->setLabel($label);
123 9
        $this->setDescription($description);
124 9
        $this->setId($id);
125 9
    }
126
    
127 10
    public function setId($id)
128
    {
129 10
        $this->id = str_replace(".","_",$id);
130 10
        $this->setAttribute('id', $this->id);
131 10
        return $this;        
132
    }
133
    
134 9
    public function getId()
135
    {
136 9
        return $this->id;
137
    }
138
139
    /**
140
     * Public accessor for setting the name property of the field.
141
     *
142
     * @param  string $name
143
     * @return Element
144
     */
145 1
    public function setName($name)
146
    {
147 1
        $this->name = $name;
148 1
        return $this;
149
    }
150
151
    /**
152
     * Public accessor for getting the name property of the field.
153
     *
154
     * @return string
155
     */
156 9
    public function getName()
157
    {
158 9
        return $this->name;
159
    }
160
161
    //! Sets the label which is attached to this element.
162 9
    public function setLabel($label)
163
    {
164 9
        $this->label = $label;
165 9
        return $this;
166
    }
167
168
    //! Gets the label which is attached to this element.
169 9
    public function getLabel()
170
    {
171 9
        return $this->label;
172
    }
173
174
    /**
175
     * Gets the description which is attached to this element. The description
176
     * is normally displayed under the element when rendering HTML.
177
     *
178
     * @return string
179
     */
180 9
    public function getDescription()
181
    {
182 9
        return $this->description;
183
    }
184
185
    /**
186
     * Sets the description which is attached to this element. The description
187
     * is normally displayed under the element when rendering HTML.
188
     *
189
     * @return Element
190
     */
191 9
    public function setDescription($description)
192
    {
193 9
        $this->description = $description;
194 9
        return $this;
195
    }
196
197
    /**
198
     * Returns all the arrays associated with this document.
199
     *
200
     * @return array 
201
     */
202 1
    public function getErrors()
203
    {
204 1
        return $this->errors;
205
    }
206
207
    // Returns the error flag for this element.
208 9
    public function hasError()
209
    {
210 9
        return $this->error;
211
    }
212
213
    /**
214
     * Renders the form element by outputing the HTML associated with
215
     * the element. This method is abstract and it is implemented by
216
     * all the other classes which inherit the Element class.
217
     *
218
     * @return string
219
     */
220
    abstract public function render();
221
222 9
    public function __toString()
223
    {
224 9
        return $this->templateRenderer->render("inline_layout_element.tpl.php", array('element' => $this));
225
    }
226
227
    //! Returns an array of all the CSS classes associated with this
228
    //! element.
229 9
    public function getCSSClasses()
230
    {
231 9
        return implode(" ", $this->classes);
232
    }
233
234
    //! Adds a css class to this element.
235 1
    public function addCSSClass($class)
236
    {
237 1
        $this->classes[] = $class;
238 1
        return $this;
239
    }
240
241
    //! Adds an attribute to the list of attributes of this element.
242
    //! This method internally creates a new Attribute object and appends
243
    //! it to the list of attributes.
244
    //! \see Attribute
245 11
    public function setAttribute($attribute,$value,$scope = Element::SCOPE_ELEMENT)
246
    {
247
        // Force the setting of the attribute.
248 11
        if($scope == Element::SCOPE_ELEMENT)
249
        {
250 11
            $this->attributes[$attribute] = $value;
251
        }
252
        else if($scope == Element::SCOPE_WRAPPER)
253
        {
254
            $this->wrapperAttributes[$attribute] = $value;
255
        }
256 11
        return $this;
257
    }
258
259
    /**
260
     * Returns an HTML representation of all the attributes. This method is 
261
     * normally called when rendering the HTML for the element.
262
     */
263 11
    public function getAttributes($scope=Element::SCOPE_ELEMENT)
264
    {
265 11
        $attributes = array();
266 11
        switch($scope)
267
        {
268 11
            case Element::SCOPE_ELEMENT: $attributes = $this->attributes; break;
269 9
            case Element::SCOPE_WRAPPER: $attributes = $this->wrapperAttributes; break;
270
        }
271 11
        return $attributes;
272
    }
273
    
274 9
    public function getAttribute($attribute, $scope = Element::SCOPE_ELEMENT)
275
    {
276 9
        $attributes = $this->getAttributes($scope);
277 9
        return @$attributes[$attribute];
278
    }
279
    
280 4
    public function setErrors($errors)
281
    {
282 4
        $this->errors = $errors;
283 4
        $this->error = true;
284 4
    }
285
    
286 9
    public function getRenderLabel()
287
    {
288 9
        return $this->renderLabel;
289
    }
290
    
291 1
    public function setRenderLabel($renderLabel)
292
    {
293 1
        $this->renderLabel = $renderLabel;
294 1
    }
295
296 11
    public function setTemplateRenderer($templateRenderer)
297
    {
298 11
        $this->templateRenderer = $templateRenderer;
299 11
    }
300
}
301
302