/src/ORM/FieldType/DBString.php

https://gitlab.com/djpmedia/silverstripe-framework · PHP · 215 lines · 101 code · 22 blank · 92 comment · 10 complexity · b2eb56da3bade1acaa11884c5a31dc28 MD5 · raw file

  1. <?php
  2. namespace SilverStripe\ORM\FieldType;
  3. /**
  4. * An abstract base class for the string field types (i.e. Varchar and Text)
  5. */
  6. abstract class DBString extends DBField
  7. {
  8. /**
  9. * @var array
  10. */
  11. private static $casting = array(
  12. "LimitCharacters" => "Text",
  13. "LimitCharactersToClosestWord" => "Text",
  14. "LimitWordCount" => "Text",
  15. "LowerCase" => "Text",
  16. "UpperCase" => "Text",
  17. "Plain" => "Text",
  18. );
  19. /**
  20. * Set the default value for "nullify empty"
  21. *
  22. * {@inheritDoc}
  23. */
  24. public function __construct($name = null, $options = [])
  25. {
  26. $this->options['nullifyEmpty'] = true;
  27. parent::__construct($name, $options);
  28. }
  29. /**
  30. * Update the optional parameters for this field.
  31. *
  32. * @param array $options Array of options
  33. * The options allowed are:
  34. * <ul><li>"nullifyEmpty"
  35. * This is a boolean flag.
  36. * True (the default) means that empty strings are automatically converted to nulls to be stored in
  37. * the database. Set it to false to ensure that nulls and empty strings are kept intact in the database.
  38. * </li></ul>
  39. * @return $this
  40. */
  41. public function setOptions(array $options = [])
  42. {
  43. parent::setOptions($options);
  44. if (array_key_exists('nullifyEmpty', $options)) {
  45. $this->options['nullifyEmpty'] = (bool) $options['nullifyEmpty'];
  46. }
  47. if (array_key_exists('default', $options)) {
  48. $this->setDefaultValue($options['default']);
  49. }
  50. return $this;
  51. }
  52. /**
  53. * Set whether this field stores empty strings rather than converting
  54. * them to null.
  55. *
  56. * @param $value boolean True if empty strings are to be converted to null
  57. * @return $this
  58. */
  59. public function setNullifyEmpty($value)
  60. {
  61. $this->options['nullifyEmpty'] = (bool) $value;
  62. return $this;
  63. }
  64. /**
  65. * Get whether this field stores empty strings rather than converting
  66. * them to null
  67. *
  68. * @return boolean True if empty strings are to be converted to null
  69. */
  70. public function getNullifyEmpty()
  71. {
  72. return $this->options['nullifyEmpty'];
  73. }
  74. /**
  75. * (non-PHPdoc)
  76. * @see core/model/fieldtypes/DBField#exists()
  77. */
  78. public function exists()
  79. {
  80. $value = $this->RAW();
  81. // All truthy values and non-empty strings exist ('0' but not (int)0)
  82. return $value || (is_string($value) && strlen($value));
  83. }
  84. public function prepValueForDB($value)
  85. {
  86. // Cast non-empty value
  87. if (strlen($value)) {
  88. return (string)$value;
  89. }
  90. // Return "empty" value
  91. if ($this->options['nullifyEmpty'] || $value === null) {
  92. return null;
  93. }
  94. return '';
  95. }
  96. /**
  97. * @return string
  98. */
  99. public function forTemplate()
  100. {
  101. return nl2br(parent::forTemplate());
  102. }
  103. /**
  104. * Limit this field's content by a number of characters.
  105. * This makes use of strip_tags() to avoid malforming the
  106. * HTML tags in the string of text.
  107. *
  108. * @param int $limit Number of characters to limit by
  109. * @param string $add Ellipsis to add to the end of truncated string
  110. * @return string
  111. */
  112. public function LimitCharacters($limit = 20, $add = '...')
  113. {
  114. $value = $this->Plain();
  115. if (mb_strlen($value) <= $limit) {
  116. return $value;
  117. }
  118. return mb_substr($value, 0, $limit) . $add;
  119. }
  120. /**
  121. * Limit this field's content by a number of characters and truncate
  122. * the field to the closest complete word. All HTML tags are stripped
  123. * from the field.
  124. *
  125. * @param int $limit Number of characters to limit by
  126. * @param string $add Ellipsis to add to the end of truncated string
  127. * @return string Plain text value with limited characters
  128. */
  129. public function LimitCharactersToClosestWord($limit = 20, $add = '...')
  130. {
  131. // Safely convert to plain text
  132. $value = $this->Plain();
  133. // Determine if value exceeds limit before limiting characters
  134. if (mb_strlen($value) <= $limit) {
  135. return $value;
  136. }
  137. // Limit to character limit
  138. $value = mb_substr($value, 0, $limit);
  139. // If value exceeds limit, strip punctuation off the end to the last space and apply ellipsis
  140. $value = preg_replace(
  141. '/[^\w_]+$/',
  142. '',
  143. mb_substr($value, 0, mb_strrpos($value, " "))
  144. ) . $add;
  145. return $value;
  146. }
  147. /**
  148. * Limit this field's content by a number of words.
  149. *
  150. * @param int $numWords Number of words to limit by.
  151. * @param string $add Ellipsis to add to the end of truncated string.
  152. *
  153. * @return string
  154. */
  155. public function LimitWordCount($numWords = 26, $add = '...')
  156. {
  157. $value = $this->Plain();
  158. $words = explode(' ', $value);
  159. if (count($words) <= $numWords) {
  160. return $value;
  161. }
  162. // Limit
  163. $words = array_slice($words, 0, $numWords);
  164. return implode(' ', $words) . $add;
  165. }
  166. /**
  167. * Converts the current value for this StringField to lowercase.
  168. *
  169. * @return string Text with lowercase (HTML for some subclasses)
  170. */
  171. public function LowerCase()
  172. {
  173. return mb_strtolower($this->RAW());
  174. }
  175. /**
  176. * Converts the current value for this StringField to uppercase.
  177. *
  178. * @return string Text with uppercase (HTML for some subclasses)
  179. */
  180. public function UpperCase()
  181. {
  182. return mb_strtoupper($this->RAW());
  183. }
  184. /**
  185. * Plain text version of this string
  186. *
  187. * @return string Plain text
  188. */
  189. public function Plain()
  190. {
  191. return trim($this->RAW());
  192. }
  193. }