Completed
Push — master ( e36103...5c707d )
by James Ekow Abaka
02:10
created

Element::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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\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
    protected $formId;
50
51
    /**
52
     * The id of the form useful for CSS styling and DOM access.
53
     * 
54
     * @var string
55
     */
56
    protected $id;
57
58
    /**
59
     * The label of the form element.
60
     * 
61
     * @var string
62
     */
63
    protected $label;
64
65
    /**
66
     * The description of the form element.
67
     * 
68
     * @var string
69
     */
70
    protected $description;
71
72
    /**
73
     * An array of all the CSS classes associated with this element.
74
     *
75
     * @var array 
76
     */
77
    protected $classes = array();
78
    
79
    /**
80
     * An array of all HTML attributes. These attributes are stored as
81
     * objects of the Attribute class. Attributes in this array are applied
82
     * directly to the form element.
83
     *
84
     * @var array 
85
     */    
86
    protected $attributes = array();
87
    
88
    /**
89
     * An array of all HTML attributes. These attributes are stored as
90
     * objects of the Attribute class. Attributes in this array are applied
91
     * directly to the wrapper which wraps the form element.
92
     *
93
     * @var array 
94
     */    
95
    protected $wrapperAttributes = array();
96
    
97
    /**
98
     * An array of all error messages associated with this element.
99
     * Error messages are setup during validation, when any element
100
     * fails its validation test.
101
     *
102
     * @var array
103
     */
104
    protected $errors = array();
105
106
    /**
107
     * A boolean value which is set to true whenever there is an error 
108
     * assiciated with the field element in one way or the other.
109
     */
110
    protected $error;
111
112
    /**
113
     * The parent element which contains this element.
114
     * @var Element
115
     */
116
    protected $parent = null;
117
118
    /**
119
     * The name of the form field. This is what is to be outputed as
120
     * the HTML name attribute of the field. If name encryption is
121
     * enabled the outputed name to HTML is mangled by the encryption
122
     * algorithm. However internally the Field may still be referenced
123
     * bu the unmangled name.
124
     */
125
    public $name;
126
127
    protected $renderLabel = true;
128
129 14
    public function __construct($label="", $description="", $id="")
130
    {
131 14
        $this->setLabel($label);
132 14
        $this->setDescription($description);
133 14
        $this->setId($id);
134 14
    }
135
    
136 14
    public function setId($id)
137
    {
138 14
        $this->id = str_replace(".","_",$id);
139 14
        $this->setAttribute('id', $this->id);
140 14
        return $this;        
141
    }
142
    
143 9
    public function getId()
144
    {
145 9
        return $this->id;
146
    }
147
148
    /**
149
     * Public accessor for setting the name property of the field.
150
     *
151
     * @param  $name The name to assign to the form element.
152
     */
153 1
    public function setName($name)
154
    {
155 1
        $this->name = $name;
156 1
        return $this;
157
    }
158
159
    /**
160
     * Public accessor for getting the name property of the field.
161
     *
162
     * @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...
163
     */
164 9
    public function getName()
165
    {
166 9
        return $this->name;
167
    }
168
169
    //! Sets the label which is attached to this element.
170 14
    public function setLabel($label)
171
    {
172 14
        $this->label = $label;
173 14
        return $this;
174
    }
175
176
    //! Gets the label which is attached to this element.
177 9
    public function getLabel()
178
    {
179 9
        return $this->label;
180
    }
181
182
    /**
183
     * Gets the description which is attached to this element. The description
184
     * is normally displayed under the element when rendering HTML.
185
     *
186
     * @return string
187
     */
188 9
    public function getDescription()
189
    {
190 9
        return $this->description;
191
    }
192
193
    /**
194
     * Sets the description which is attached to this element. The description
195
     * is normally displayed under the element when rendering HTML.
196
     *
197
     * @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...
198
     */
199 14
    public function setDescription($description)
200
    {
201 14
        $this->description = $description;
202 14
        return $this;
203
    }
204
205
    /**
206
     * Returns all the arrays associated with this document.
207
     *
208
     * @return array 
209
     */
210 1
    public function getErrors()
211
    {
212 1
        return $this->errors;
213
    }
214
215
    // Returns the error flag for this element.
216 9
    public function hasError()
217
    {
218 9
        return $this->error;
219
    }
220
221 1
    public function getType()
222
    {
223 1
        return __CLASS__;
224
    }
225
226
    /**
227
     * Renders the form element by outputing the HTML associated with
228
     * the element. This method is abstract and it is implemented by
229
     * all the other classes which inherit the Element class.
230
     */
231
    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...
232
233 9
    public function __toString()
234
    {
235 9
        return TemplateEngine::render("inline_layout_element.tpl.php", array('element' => $this));
236
    }
237
238
    //! Returns an array of all the CSS classes associated with this
239
    //! element.
240 9
    public function getCSSClasses()
241
    {
242 9
        return implode(" ", $this->classes);
243
    }
244
245
    //! Adds a css class to this element.
246 1
    public function addCSSClass($class)
247
    {
248 1
        $this->classes[] = $class;
249 1
        return $this;
250
    }
251
252
    //! Adds an attribute to the list of attributes of this element.
253
    //! This method internally creates a new Attribute object and appends
254
    //! it to the list of attributes.
255
    //! \see Attribute
256 14
    public function setAttribute($attribute,$value,$scope = Element::SCOPE_ELEMENT)
257
    {
258
        // Force the setting of the attribute.
259 14
        if($scope == Element::SCOPE_ELEMENT)
260
        {
261 14
            $this->attributes[$attribute] = $value;
262
        }
263
        else if($scope == Element::SCOPE_WRAPPER)
264
        {
265
            $this->wrapperAttributes[$attribute] = $value;
266
        }
267 14
        return $this;
268
    }
269
270
    /**
271
     * Returns an HTML representation of all the attributes. This method is 
272
     * normally called when rendering the HTML for the element.
273
     */
274 12
    public function getAttributes($scope=Element::SCOPE_ELEMENT)
275
    {
276 12
        $attributes = array();
277
        switch($scope)
278
        {
279 12
            case Element::SCOPE_ELEMENT: $attributes = $this->attributes; break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
280 9
            case Element::SCOPE_WRAPPER: $attributes = $this->wrapperAttributes; break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
281
        }
282 12
        return $attributes;
283
    }
284
    
285 9
    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...
286
    {
287 9
        $attributes = $this->getAttributes($scope);
288 9
        return @$attributes[$attribute];
289
    }
290
    
291 4
    public function setErrors($errors)
292
    {
293 4
        $this->errors = $errors;
294 4
        $this->error = true;
295 4
    }
296
    
297 9
    public function getRenderLabel()
298
    {
299 9
        return $this->renderLabel;
300
    }
301
    
302 1
    public function setRenderLabel($renderLabel)
303
    {
304 1
        $this->renderLabel = $renderLabel;
305 1
    }
306
    
307 9
    public function isContainer()
308
    {
309 9
        return false;
310
    }    
311
}
312
313