Completed
Push — develop ( cc0ca6...c312f0 )
by jake
01:40
created

Field::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
    /**
54
     * Create a new form field
55
     *
56
     * @param string $name name of field
57
     *
58
     * @access public
59
     */
60 15
    public function __construct($name)
61
    {
62 15
        $this->name = $name;
63 15
    }
64
65
    /**
66
     * Set label
67
     *
68
     * @param string $label Label for field
69
     *
70
     * @return $this
71
     *
72
     * @access public
73
     */
74 2
    public function label($label)
75
    {
76 2
        $this->label = $label;
77 2
        return $this;
78
    }
79
80
    /**
81
     * Set error mesages
82
     *
83
     * @param array $errors array of error messages
84
     *
85
     * @return $this
86
     *
87
     * @access public
88
     */
89 2
    public function errors(array $errors)
90
    {
91 2
        $this->errors = $errors;
92 2
        return $this;
93
    }
94
95
    /**
96
     * Set type
97
     *
98
     * @param string $type field type
99
     *
100
     * @return $this
101
     *
102
     * @access public
103
     */
104 2
    public function type($type)
105
    {
106 2
        $this->type = $type;
107 2
        return $this;
108
    }
109
110
    /**
111
     * Fill value
112
     *
113
     * @param mixed $value value of field
114
     *
115
     * @return $this
116
     *
117
     * @access public
118
     */
119 3
    public function fill($value)
120
    {
121 3
        $this->value = $value;
122 3
        return $this;
123
    }
124
125
    /**
126
     * Set attributes
127
     *
128
     * @param array $attribs attributes for field
129
     *
130
     * @return $this
131
     *
132
     * @access public
133
     */
134 3
    public function attribs(array $attribs)
135
    {
136 3
        if (isset($attribs['id'])) {
137 1
            $this->id = $attribs['id'];
138 1
        }
139
140 3
        $this->attribs = $attribs;
141 3
        return $this;
142
    }
143
144
    /**
145
     * Set options
146
     *
147
     * @param array $options options for field
148
     *
149
     * @return $this
150
     *
151
     * @access public
152
     */
153 2
    public function options(array $options)
154
    {
155 2
        $this->options = $options;
156 2
        return $this;
157
    }
158
159
    /**
160
     * Get field spec as array
161
     *
162
     * @return array
163
     *
164
     * @access public
165
     */
166 1
    public function getSpec()
167
    {
168
        return [
169 1
            'type' => $this->type,
170 1
            'name' => $this->name,
171 1
            'value' => $this->value,
172 1
            'attribs' => $this->attribs,
173 1
            'options' => $this->options
174 1
        ];
175
    }
176
177
    /**
178
     * Magic getter
179
     *
180
     * @param string $name name of property
181
     *
182
     * @return mixed
183
     *
184
     * @access public
185
     */
186 11
    public function __get($name)
187
    {
188 11
        $method = 'get' . ucfirst($name);
189 11
        if (method_exists($this, $method)) {
190 1
            return $this->$method();
191
        }
192 10
        return $this->$name;
193
    }
194
195
    /**
196
     * JsonSerialize
197
     *
198
     * @return mixed
199
     *
200
     * @access public
201
     */
202
    public function jsonSerialize()
203
    {
204
        return $this->getArrayCopy();
205
    }
206
207
    /**
208
     * GetArrayCopy
209
     *
210
     * @return mixed
211
     *
212
     * @access public
213
     */
214
    public function getArrayCopy()
215
    {
216
        return [
217
            'name'    => $this->name,
218
            'id'      => $this->id,
219
            'label'   => $this->label,
220
            'errors'  => $this->errors,
221
            'type'    => $this->type,
222
            'value'   => $this->value,
223
            'attribs' => $this->attribs,
224
            'options' => $this->options,
225
        ];
226
    }
227
228
}
229