Completed
Push — dev ( c5a23f...f84598 )
by James Ekow Abaka
09:41
created

Element::getAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
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
use ntentan\honam\helpers\FormHelper;
35
use ntentan\honam\TemplateEngine;
36
37
/**
38
 * The form element class. An element can be anything from the form
39
 * itself to the objects that are put in the form. Provides an
40
 * abstract render class that is used to output the HTML associated
41
 * with this form element. All visible elements of the form must be
42
 * subclasses of the element class.
43
 */
44
abstract class Element
45
{
46
    const SCOPE_ELEMENT = "";
47
    const SCOPE_WRAPPER = "_wrapper";
48
49
    /**
50
     * The id of the form useful for CSS styling and DOM access.
51
     * 
52
     * @var string
53
     */
54
    protected $id;
55
56
    /**
57
     * The label of the form element.
58
     * 
59
     * @var string
60
     */
61
    protected $label;
62
63
    /**
64
     * The description of the form element.
65
     * 
66
     * @var string
67
     */
68
    protected $description;
69
70
    /**
71
     * An array of all the CSS classes associated with this element.
72
     *
73
     * @var array 
74
     */
75
    protected $classes = array();
76
    
77
    /**
78
     * An array of all HTML attributes. These attributes are stored as
79
     * objects of the Attribute class. Attributes in this array are applied
80
     * directly to the form element.
81
     *
82
     * @var array 
83
     */    
84
    protected $attributes = array();
85
    
86
    /**
87
     * An array of all HTML attributes. These attributes are stored as
88
     * objects of the Attribute class. Attributes in this array are applied
89
     * directly to the wrapper which wraps the form element.
90
     *
91
     * @var array 
92
     */    
93
    protected $wrapperAttributes = array();
94
    
95
    /**
96
     * An array of all error messages associated with this element.
97
     * Error messages are setup during validation, when any element
98
     * fails its validation test.
99
     *
100
     * @var array
101
     */
102
    protected $errors = array();
103
104
    /**
105
     * A boolean value which is set to true whenever there is an error 
106
     * assiciated with the field element in one way or the other.
107
     */
108
    protected $error;
109
110
    /**
111
     * The parent element which contains this element.
112
     * @var Element
113
     */
114
    protected $parent = null;
115
116
    protected $name;
117
118
    protected $renderLabel = true;
119
120
    protected $templateRenderer;
121
122
    public function __construct($label="", $description="", $id="")
123
    {
124
        $this->setLabel($label);
125
        $this->setDescription($description);
126
        $this->setId($id);
127
    }
128
    
129
    public function setId($id)
130
    {
131
        $this->id = str_replace(".","_",$id);
132
        $this->setAttribute('id', $this->id);
133
        return $this;        
134
    }
135
    
136
    public function getId()
137
    {
138
        return $this->id;
139
    }
140
141
    /**
142
     * Public accessor for setting the name property of the field.
143
     *
144
     * @param  $name The name to assign to the form element.
145
     * @return Element
146
     */
147
    public function setName($name)
148
    {
149
        $this->name = $name;
150
        return $this;
151
    }
152
153
    /**
154
     * Public accessor for getting the name property of the field.
155
     *
156
     * @return The name of the form field.
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
157
     */
158
    public function getName()
159
    {
160
        return $this->name;
161
    }
162
163
    //! Sets the label which is attached to this element.
164
    public function setLabel($label)
165
    {
166
        $this->label = $label;
167
        return $this;
168
    }
169
170
    //! Gets the label which is attached to this element.
171
    public function getLabel()
172
    {
173
        return $this->label;
174
    }
175
176
    /**
177
     * Gets the description which is attached to this element. The description
178
     * is normally displayed under the element when rendering HTML.
179
     *
180
     * @return string
181
     */
182
    public function getDescription()
183
    {
184
        return $this->description;
185
    }
186
187
    /**
188
     * Sets the description which is attached to this element. The description
189
     * is normally displayed under the element when rendering HTML.
190
     *
191
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be Element?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
192
     */
193
    public function setDescription($description)
194
    {
195
        $this->description = $description;
196
        return $this;
197
    }
198
199
    /**
200
     * Returns all the arrays associated with this document.
201
     *
202
     * @return array 
203
     */
204
    public function getErrors()
205
    {
206
        return $this->errors;
207
    }
208
209
    // Returns the error flag for this element.
210
    public function hasError()
211
    {
212
        return $this->error;
213
    }
214
215
    public function getType()
216
    {
217
        return __CLASS__;
218
    }
219
220
    /**
221
     * Renders the form element by outputing the HTML associated with
222
     * the element. This method is abstract and it is implemented by
223
     * all the other classes which inherit the Element class.
224
     */
225
    abstract public function render();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
226
227
    public function __toString()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
228
    {
229
        return $this->templateRenderer->render("inline_layout_element.tpl.php", array('element' => $this));
230
    }
231
232
    //! Returns an array of all the CSS classes associated with this
233
    //! element.
234
    public function getCSSClasses()
235
    {
236
        return implode(" ", $this->classes);
237
    }
238
239
    //! Adds a css class to this element.
240
    public function addCSSClass($class)
241
    {
242
        $this->classes[] = $class;
243
        return $this;
244
    }
245
246
    //! Adds an attribute to the list of attributes of this element.
247
    //! This method internally creates a new Attribute object and appends
248
    //! it to the list of attributes.
249
    //! \see Attribute
250
    public function setAttribute($attribute,$value,$scope = Element::SCOPE_ELEMENT)
251
    {
252
        // Force the setting of the attribute.
253
        if($scope == Element::SCOPE_ELEMENT)
254
        {
255
            $this->attributes[$attribute] = $value;
256
        }
257
        else if($scope == Element::SCOPE_WRAPPER)
258
        {
259
            $this->wrapperAttributes[$attribute] = $value;
260
        }
261
        return $this;
262
    }
263
264
    /**
265
     * Returns an HTML representation of all the attributes. This method is 
266
     * normally called when rendering the HTML for the element.
267
     */
268
    public function getAttributes($scope=Element::SCOPE_ELEMENT)
269
    {
270
        $attributes = array();
271
        switch($scope)
272
        {
273
            case Element::SCOPE_ELEMENT: $attributes = $this->attributes; break;
274
            case Element::SCOPE_WRAPPER: $attributes = $this->wrapperAttributes; break;
275
        }
276
        return $attributes;
277
    }
278
    
279
    public function getAttribute($attribute, $scope = Element::SCOPE_ELEMENT)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
280
    {
281
        $attributes = $this->getAttributes($scope);
282
        return @$attributes[$attribute];
283
    }
284
    
285
    public function setErrors($errors)
286
    {
287
        $this->errors = $errors;
288
        $this->error = true;
289
    }
290
    
291
    public function getRenderLabel()
292
    {
293
        return $this->renderLabel;
294
    }
295
    
296
    public function setRenderLabel($renderLabel)
297
    {
298
        $this->renderLabel = $renderLabel;
299
    }
300
301
    public function setTemplateRenderer($templateRenderer)
302
    {
303
        $this->templateRenderer = $templateRenderer;
304
    }
305
}
306
307