Passed
Pull Request — 4.0 (#7676)
by
unknown
07:28
created

TextareaField::getMaxLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
60
        $data['data']['rows'] = $this->getRows();
61
        $data['data']['columns'] = $this->getColumns();
62
63
        $maxLength = $this->getMaxLength();
64
        if ($maxLength) {
65
            $data['maxLength'] = $maxLength;
66
        }
67
68
        return $data;
69
    }
70
71
    /**
72
     * Set the number of rows in the textarea
73
     *
74
     * @param int $rows
75
     *
76
     * @return $this
77
     */
78
    public function setRows($rows)
79
    {
80
        $this->rows = $rows;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Gets number of rows
87
     *
88
     * @return int
89
     */
90
    public function getRows()
91
    {
92
        return $this->rows;
93
    }
94
95
    /**
96
     * Set the number of columns in the textarea
97
     *
98
     * @param int $cols
99
     *
100
     * @return $this
101
     */
102
    public function setColumns($cols)
103
    {
104
        $this->cols = $cols;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Gets the number of columns in this textarea
111
     *
112
     * @return int
113
     */
114
    public function getColumns()
115
    {
116
        return $this->cols;
117
    }
118
119
    /**
120
     * @param int $maxLength
121
     * @return $this
122
     */
123
    public function setMaxLength($maxLength)
124
    {
125
        $this->maxLength = $maxLength;
126
127
        return $this;
128
    }
129
130
    /**
131
     * @return null|int
132
     */
133
    public function getMaxLength()
134
    {
135
        return $this->maxLength;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function getAttributes()
142
    {
143
        return array_merge(
144
            parent::getAttributes(),
145
            array(
146
                'rows' => $this->getRows(),
147
                'cols' => $this->getColumns(),
148
                'value' => null,
149
                'type' => null
150
            )
151
        );
152
    }
153
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function Type()
159
    {
160
        $parent = parent::Type();
161
162
        if ($this->readonly) {
163
            return $parent . ' readonly';
164
        }
165
166
        return $parent;
167
    }
168
169
    /**
170
     * Return value with all values encoded in html entities
171
     *
172
     * @return string Raw HTML
173
     */
174
    public function ValueEntities()
175
    {
176
        return htmlentities($this->Value(), ENT_COMPAT, 'UTF-8');
177
    }
178
}
179