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

Field   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 98
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setValue() 0 5 1
A getValue() 0 4 1
A setRequired() 0 5 1
A getRequired() 0 4 1
A setData() 0 7 2
A getType() 0 4 1
A getCSSClasses() 0 7 3
A render() 0 6 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
 * Sublcasses 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
    public function __construct($name="", $value="")
59
    {
60
        $this->name = $name;
61
        $this->value = $value;
62
    }
63
64
    /**
65
     * Sets the value of the field.
66
     *
67
     * @param $value The value of the field.
68
     * @return Field
69
     */
70
    public function setValue($value)
71
    {
72
        $this->value = $value;
73
        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
    public function getValue()
82
    {
83
        return $this->value;
84
    }
85
86
    /**
87
     * Sets the required status of the field.
88
     *
89
     * @param The required status of the field.
90
     */
91
    public function setRequired($required)
92
    {
93
        $this->required = $required;
94
        return $this;
95
    }
96
97
    /**
98
     * Returns the required status of the field.
99
     *
100
     * @return The required status of the field.
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

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...
101
     */
102
    public function getRequired()
103
    {
104
        return $this->required;
105
    }
106
107
    //! Sets the data that is stored in this field.
108
    //! \param $data An array of fields. This method just looks through for
109
    //!              a field that matches it and then applies its value to
110
    //!              itself.
111
    public function setData($data)
112
    {
113
        if(array_search($this->getName(false),array_keys($data))!==false)
0 ignored issues
show
Unused Code introduced by
The call to Field::getName() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
114
        {
115
            $this->setValue($data[$this->getName(false)]);
0 ignored issues
show
Unused Code introduced by
The call to Field::getName() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116
        }
117
    }
118
119
    public function getType()
120
    {
121
        return __CLASS__;
122
    }
123
124
    public function getCSSClasses()
125
    {
126
        $classes = parent::getCSSClasses();
127
        if($this->error) $classes.="error ";
128
        if($this->getRequired()) $classes .="required ";
129
        return trim($classes);
130
    }
131
    
132
    public function render()
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...
133
    {
134
        $this->setAttribute("class", "{$this->getAttribute('type')} {$this->getCSSClasses()}");
135
        $this->setAttribute("name", $this->getName());
136
        return TemplateEngine::render("input_element.tpl.php", array('element' => $this));
137
    }
138
}
139