1 | <?php |
||
8 | abstract class StringField extends DBField { |
||
9 | |||
10 | /** |
||
11 | * @var boolean |
||
12 | */ |
||
13 | protected $nullifyEmpty = true; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private static $casting = array( |
||
19 | "LimitCharacters" => "Text", |
||
20 | "LimitCharactersToClosestWord" => "Text", |
||
21 | 'LimitWordCount' => 'Text', |
||
22 | 'LimitWordCountXML' => 'HTMLText', |
||
23 | "LowerCase" => "Text", |
||
24 | "UpperCase" => "Text", |
||
25 | 'NoHTML' => 'Text', |
||
26 | ); |
||
27 | |||
28 | /** |
||
29 | * Construct a string type field with a set of optional parameters. |
||
30 | * |
||
31 | * @param $name string The name of the field |
||
32 | * @param $options array An array of options e.g. array('nullifyEmpty'=>false). See |
||
33 | * {@link StringField::setOptions()} for information on the available options |
||
34 | */ |
||
35 | public function __construct($name = null, $options = array()) { |
||
44 | |||
45 | /** |
||
46 | * Update the optional parameters for this field. |
||
47 | * @param $options array of options |
||
48 | * The options allowed are: |
||
49 | * <ul><li>"nullifyEmpty" |
||
50 | * This is a boolean flag. |
||
51 | * True (the default) means that empty strings are automatically converted to nulls to be stored in |
||
52 | * the database. Set it to false to ensure that nulls and empty strings are kept intact in the database. |
||
53 | * </li></ul> |
||
54 | * @return unknown_type |
||
55 | */ |
||
56 | public function setOptions(array $options = array()) { |
||
61 | |||
62 | /** |
||
63 | * Set whether this field stores empty strings rather than converting |
||
64 | * them to null. |
||
65 | * |
||
66 | * @param $value boolean True if empty strings are to be converted to null |
||
67 | */ |
||
68 | public function setNullifyEmpty($value) { |
||
71 | |||
72 | /** |
||
73 | * Get whether this field stores empty strings rather than converting |
||
74 | * them to null |
||
75 | * |
||
76 | * @return boolean True if empty strings are to be converted to null |
||
77 | */ |
||
78 | public function getNullifyEmpty() { |
||
81 | |||
82 | /** |
||
83 | * (non-PHPdoc) |
||
84 | * @see core/model/fieldtypes/DBField#exists() |
||
85 | */ |
||
86 | public function exists() { |
||
87 | $value = $this->RAW(); |
||
88 | return $this->isPopulated($value); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * (non-PHPdoc) |
||
93 | * @see core/model/fieldtypes/DBField#prepValueForDB($value) |
||
94 | */ |
||
95 | public function prepValueForDB($value) { |
||
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | */ |
||
106 | public function forTemplate() { |
||
109 | |||
110 | /** |
||
111 | * Limit this field's content by a number of characters. |
||
112 | * This makes use of strip_tags() to avoid malforming the |
||
113 | * HTML tags in the string of text. |
||
114 | * |
||
115 | * @param int $limit Number of characters to limit by |
||
116 | * @param string $add Ellipsis to add to the end of truncated string |
||
117 | * @return string |
||
118 | */ |
||
119 | public function LimitCharacters($limit = 20, $add = '...') { |
||
132 | |||
133 | /** |
||
134 | * Limit this field's content by a number of characters and truncate |
||
135 | * the field to the closest complete word. All HTML tags are stripped |
||
136 | * from the field. |
||
137 | * |
||
138 | * @param int $limit Number of characters to limit by |
||
139 | * @param string $add Ellipsis to add to the end of truncated string |
||
140 | * @return string |
||
141 | */ |
||
142 | public function LimitCharactersToClosestWord($limit = 20, $add = '...') { |
||
163 | |||
164 | /** |
||
165 | * Limit this field's content by a number of words. |
||
166 | * |
||
167 | * CAUTION: This is not XML safe. Please use |
||
168 | * {@link LimitWordCountXML()} instead. |
||
169 | * |
||
170 | * @param int $numWords Number of words to limit by. |
||
171 | * @param string $add Ellipsis to add to the end of truncated string. |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | public function LimitWordCount($numWords = 26, $add = '...') { |
||
188 | |||
189 | /** |
||
190 | * Limit the number of words of the current field's |
||
191 | * content. This is XML safe, so characters like & |
||
192 | * are converted to & |
||
193 | * |
||
194 | * @param int $numWords Number of words to limit by. |
||
195 | * @param string $add Ellipsis to add to the end of truncated string. |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public function LimitWordCountXML($numWords = 26, $add = '...') { |
||
204 | |||
205 | /** |
||
206 | * Converts the current value for this StringField to lowercase. |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | public function LowerCase() { |
||
213 | |||
214 | /** |
||
215 | * Converts the current value for this StringField to uppercase. |
||
216 | * @return string |
||
217 | */ |
||
218 | public function UpperCase() { |
||
221 | |||
222 | /** |
||
223 | * Return the value of the field stripped of html tags. |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | public function NoHTML() { |
||
230 | |||
231 | /** |
||
232 | * Returns true if the value meets all the criteria of not being empty, as defined by |
||
233 | * the class |
||
234 | * @param $value |
||
235 | * @return bool |
||
236 | */ |
||
237 | protected function isPopulated($value) { |
||
242 | } |
||
243 |