Passed
Push — master ( e78c48...210134 )
by Damian
14:57 queued 06:02
created

TextareaField::setMaxLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
/**
6
 * TextareaField creates a multi-line text field,
7
 * allowing more data to be entered than a standard
8
 * text field. It creates the <textarea> tag in the
9
 * form HTML.
10
 *
11
 * <code>
12
 * new TextareaField(
13
 *    $name = "description",
14
 *    $title = "Description",
15
 *    $value = "This is the default description"
16
 * );
17
 * </code>
18
 */
19
class TextareaField extends FormField
20
{
21
22
    /**
23
     * Value should be XML
24
     *
25
     * @var array
26
     */
27
    private static $casting = array(
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
28
        'Value' => 'Text',
29
        'ValueEntities' => 'HTMLFragment(array(\'shortcodes\' => false))',
30
    );
31
32
    protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TEXT;
33
34
    /**
35
     * Visible number of text lines.
36
     *
37
     * @var int
38
     */
39
    protected $rows = 5;
40
41
    /**
42
     * Visible number of text columns.
43
     *
44
     * @var int
45
     */
46
    protected $cols = 20;
47
48
    /**
49
     * @var int
50
     */
51
    protected $maxLength;
52
53
    /**
54
     * Set textarea specific schema data
55
     */
56
    public function getSchemaDataDefaults()
57
    {
58
        $data = parent::getSchemaDataDefaults();
59
        $data['data']['rows'] = $this->getRows();
60
        $data['data']['columns'] = $this->getColumns();
61
        $data['data']['maxlength'] =  $this->getMaxLength();
62
        return $data;
63
    }
64
65
    /**
66
     * Set the number of rows in the textarea
67
     *
68
     * @param int $rows
69
     *
70
     * @return $this
71
     */
72
    public function setRows($rows)
73
    {
74
        $this->rows = $rows;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Gets number of rows
81
     *
82
     * @return int
83
     */
84
    public function getRows()
85
    {
86
        return $this->rows;
87
    }
88
89
    /**
90
     * Set the number of columns in the textarea
91
     *
92
     * @param int $cols
93
     *
94
     * @return $this
95
     */
96
    public function setColumns($cols)
97
    {
98
        $this->cols = $cols;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Gets the number of columns in this textarea
105
     *
106
     * @return int
107
     */
108
    public function getColumns()
109
    {
110
        return $this->cols;
111
    }
112
113
    /**
114
     * @param int $maxLength
115
     * @return $this
116
     */
117
    public function setMaxLength($maxLength)
118
    {
119
        $this->maxLength = $maxLength;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return null|int
126
     */
127
    public function getMaxLength()
128
    {
129
        return $this->maxLength;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getAttributes()
136
    {
137
        $attributes = array_merge(
138
            parent::getAttributes(),
139
            array(
140
                'rows' => $this->getRows(),
141
                'cols' => $this->getColumns(),
142
                'value' => null,
143
                'type' => null,
144
            )
145
        );
146
147
        $maxLength = $this->getMaxLength();
148
        if ($maxLength) {
149
            $attributes['maxlength'] = $maxLength;
150
        }
151
152
        return $attributes;
153
    }
154
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function Type()
160
    {
161
        $parent = parent::Type();
162
163
        if ($this->readonly) {
164
            return $parent . ' readonly';
165
        }
166
167
        return $parent;
168
    }
169
170
    /**
171
     * Return value with all values encoded in html entities
172
     *
173
     * @return string Raw HTML
174
     */
175
    public function ValueEntities()
176
    {
177
        return htmlentities($this->Value(), ENT_COMPAT, 'UTF-8');
178
    }
179
}
180