/
DBVarchar.php
135 lines (119 loc) · 3.43 KB
/
DBVarchar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace SilverStripe\ORM\FieldType;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\NullableField;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\Connect\MySQLDatabase;
use SilverStripe\ORM\DB;
/**
* Class Varchar represents a variable-length string of up to 255 characters, designed to store raw text
*
* @see DBHTMLText
* @see DBHTMLVarchar
* @see DBText
*/
class DBVarchar extends DBString
{
private static $casting = [
'Initial' => 'Text',
'URL' => 'Text',
];
/**
* Max size of this field
*
* @var int
*/
protected $size;
/**
* Construct a new short text field
*
* @param string $name The name of the field
* @param int $size The maximum size of the field, in terms of characters
* @param array $options Optional parameters, e.g. array("nullifyEmpty"=>false).
* See {@link StringField::setOptions()} for information on the available options
*/
public function __construct($name = null, $size = 255, $options = [])
{
$this->size = $size ? $size : 255;
parent::__construct($name, $options);
}
/**
* Allow the ability to access the size of the field programmatically. This
* can be useful if you want to have text fields with a length limit that
* is dictated by the DB field.
*
* TextField::create('Title')->setMaxLength(singleton('SiteTree')->dbObject('Title')->getSize())
*
* @return int The size of the field
*/
public function getSize()
{
return $this->size;
}
/**
* (non-PHPdoc)
* @see DBField::requireField()
*/
public function requireField()
{
$charset = Config::inst()->get(MySQLDatabase::class, 'charset');
$collation = Config::inst()->get(MySQLDatabase::class, 'collation');
$parts = [
'datatype' => 'varchar',
'precision' => $this->size,
'character set' => $charset,
'collate' => $collation,
'arrayValue' => $this->arrayValue
];
$values = [
'type' => 'varchar',
'parts' => $parts
];
DB::require_field($this->tableName, $this->name, $values);
}
/**
* Return the first letter of the string followed by a .
*
* @return string
*/
public function Initial()
{
if ($this->exists()) {
$value = $this->RAW();
return $value[0] . '.';
}
return null;
}
/**
* Ensure that the given value is an absolute URL.
*
* @return string
*/
public function URL()
{
$value = $this->RAW();
if (preg_match('#^[a-zA-Z]+://#', $value ?? '')) {
return $value;
}
return 'http://' . $value;
}
/**
* Return the value of the field in rich text format
* @return string
*/
public function RTF()
{
return str_replace("\n", '\par ', $this->RAW() ?? '');
}
public function scaffoldFormField($title = null, $params = null)
{
// Set field with appropriate size
$field = TextField::create($this->name, $title);
$field->setMaxLength($this->getSize());
// Allow the user to select if it's null instead of automatically assuming empty string is
if (!$this->getNullifyEmpty()) {
return NullableField::create($field);
}
return $field;
}
}