Completed
Push — develop ( 2974e1...16a458 )
by jake
02:50 queued 01:32
created

Field::help()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

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 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Jnjxp\Form
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (C) 2016 Jake Johns
8
 *
9
 * This software may be modified and distributed under the terms
10
 * of the MIT license.  See the LICENSE file for details.
11
 *
12
 * @category  Field
13
 * @package   Jnjxp\Field
14
 * @author    Jake Johns <[email protected]>
15
 * @copyright 2016 Jake Johns
16
 * @license   http://jnj.mit-license.org/2016 MIT License
17
 * @link      http://github.com/jnjxp/jnjxp.form
18
 */
19
20
namespace Jnjxp\Form;
21
22
use JsonSerializable;
23
24
/**
25
 * Field
26
 *
27
 * @category Field
28
 * @package  Jnjxp\Form
29
 * @author   Jake Johns <[email protected]>
30
 * @license  http://jnj.mit-license.org/ MIT License
31
 * @link     http://github.com/jnjxp/jnjxp.form
32
 *
33
 * @SuppressWarnings(PHPMD.ShortVariable)
34
 */
35
class Field implements JsonSerializable
36
{
37
    protected $name;
38
39
    protected $id;
40
41
    protected $label;
42
43
    protected $errors = [];
44
45
    protected $type = 'text';
46
47
    protected $value;
48
49
    protected $attribs = [];
50
51
    protected $options = [];
52
53
    protected $help;
54
55
    /**
56
     * Create a new form field
57
     *
58
     * @param string $name name of field
59
     *
60
     * @access public
61
     */
62 16
    public function __construct($name)
63
    {
64 16
        $this->name = $name;
65 16
    }
66
67
    /**
68
     * Set label
69
     *
70
     * @param string $label Label for field
71
     *
72
     * @return $this
73
     *
74
     * @access public
75
     */
76 2
    public function label($label)
77
    {
78 2
        $this->label = $label;
79 2
        return $this;
80
    }
81
82
    /**
83
     * Set error mesages
84
     *
85
     * @param array $errors array of error messages
86
     *
87
     * @return $this
88
     *
89
     * @access public
90
     */
91 2
    public function errors(array $errors)
92
    {
93 2
        $this->errors = $errors;
94 2
        return $this;
95
    }
96
97
    /**
98
     * Set type
99
     *
100
     * @param string $type field type
101
     *
102
     * @return $this
103
     *
104
     * @access public
105
     */
106 2
    public function type($type)
107
    {
108 2
        $this->type = $type;
109 2
        return $this;
110
    }
111
112
    /**
113
     * Fill value
114
     *
115
     * @param mixed $value value of field
116
     *
117
     * @return $this
118
     *
119
     * @access public
120
     */
121 3
    public function fill($value)
122
    {
123 3
        $this->value = $value;
124 3
        return $this;
125
    }
126
127
    /**
128
     * Set attributes
129
     *
130
     * @param array $attribs attributes for field
131
     *
132
     * @return $this
133
     *
134
     * @access public
135
     */
136 3
    public function attribs(array $attribs)
137
    {
138 3
        if (isset($attribs['id'])) {
139 1
            $this->id = $attribs['id'];
140
        }
141
142 3
        $this->attribs = $attribs;
143 3
        return $this;
144
    }
145
146
    /**
147
     * Set options
148
     *
149
     * @param array $options options for field
150
     *
151
     * @return $this
152
     *
153
     * @access public
154
     */
155 2
    public function options(array $options)
156
    {
157 2
        $this->options = $options;
158 2
        return $this;
159
    }
160
161
    /**
162
     * Set help text for field
163
     *
164
     * @param string $text help text for field
165
     *
166
     * @return $this
167
     *
168
     * @access public
169
     */
170 1
    public function help($text)
171
    {
172 1
        $this->help = $text;
173 1
        return $this;
174
    }
175
176
    /**
177
     * Get field spec as array
178
     *
179
     * @return array
180
     *
181
     * @access public
182
     */
183 1
    public function getSpec()
184
    {
185
        return [
186 1
            'type' => $this->type,
187 1
            'name' => $this->name,
188 1
            'value' => $this->value,
189 1
            'attribs' => $this->attribs,
190 1
            'options' => $this->options
191
        ];
192
    }
193
194
    /**
195
     * Magic getter
196
     *
197
     * @param string $name name of property
198
     *
199
     * @return mixed
200
     *
201
     * @access public
202
     */
203 12
    public function __get($name)
204
    {
205 12
        $method = 'get' . ucfirst($name);
206 12
        if (method_exists($this, $method)) {
207 1
            return $this->$method();
208
        }
209 11
        return $this->$name;
210
    }
211
212
    /**
213
     * JsonSerialize
214
     *
215
     * @return mixed
216
     *
217
     * @access public
218
     */
219
    public function jsonSerialize()
220
    {
221
        return $this->getArrayCopy();
222
    }
223
224
    /**
225
     * GetArrayCopy
226
     *
227
     * @return mixed
228
     *
229
     * @access public
230
     */
231
    public function getArrayCopy()
232
    {
233
        return [
234
            'name'    => $this->name,
235
            'id'      => $this->id,
236
            'label'   => $this->label,
237
            'errors'  => $this->errors,
238
            'type'    => $this->type,
239
            'value'   => $this->value,
240
            'attribs' => $this->attribs,
241
            'options' => $this->options,
242
        ];
243
    }
244
245
}
246