Completed
Push — dev ( a486ee...7113e4 )
by James Ekow Abaka
03:55
created

Field::setRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Abstract form fields
4
 * 
5
 * Ntentan Framework
6
 * Copyright (c) 2008-2013 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 field class. This class represents a form field element.
37
 * Subclasses of this class are to be used to capture information from
38
 * the user of the application.
39
 * \ingroup Form_API
40
 */
41
class Field extends Element
42
{
43
    /**
44
     * A flag for setting the required state of the form. If this value
45
     * is set as true then the form would not be validated if there is
46
     * no value entered into this field.
47
     */
48
    public $required = false;
49
50
    /**
51
     * The value of the form field.
52
     */
53
    protected $value;
54
55
    /**
56
     * The constructor for the field element.
57
     */
58 9
    public function __construct($name="", $value="")
59
    {
60 9
        $this->name = $name;
61 9
        $this->value = $value;
62 9
    }
63
64
    /**
65
     * Sets the value of the field.
66
     *
67
     * @param $value The value of the field.
68
     * @return Field
69
     */
70 2
    public function setValue($value)
71
    {
72 2
        $this->value = $value;
73 2
        return $this;
74
    }
75
76
    /**
77
     * Get the value of the field.
78
     *
79
     * @return unknown
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...
80
     */
81 9
    public function getValue()
82
    {
83 9
        return $this->value;
84
    }
85
86
    /**
87
     * Sets the required status of the field.
88
     *
89
     * @param The required status of the field.
90
     * @return Field
91
     */
92 1
    public function setRequired($required)
93
    {
94 1
        $this->required = $required;
95 1
        return $this;
96
    }
97
98
    /**
99
     * Returns the required status of the field.
100
     *
101
     * @return bool
102
     */
103 9
    public function getRequired()
104
    {
105 9
        return $this->required;
106
    }
107
108
    /**
109
     * @param $data
110
     */
111 3
    public function setData($data)
112
    {
113 3
        if(array_search($this->getName(),array_keys($data))!==false)
114
        {
115 1
            $this->setValue($data[$this->getName()]);
116
        }
117 3
    }
118
119 9
    public function getCSSClasses()
120
    {
121 9
        $classes = parent::getCSSClasses();
122 9
        if($this->error) $classes.="error ";
123 9
        if($this->getRequired()) $classes .="required ";
124 9
        return trim($classes);
125
    }
126
127
    /**
128
     * @return string
129
     */
130 9
    public function render()
131
    {
132 9
        $this->setAttribute("class", "{$this->getAttribute('type')} {$this->getCSSClasses()}");
133 9
        $this->setAttribute("name", $this->getName());
134 9
        return $this->templateRenderer->render("input_element.tpl.php", array('element' => $this));
135
    }
136
}
137