Permalink
Browse files

API: Move LimitWordCount() to Varchar

  • Loading branch information...
1 parent 65cb182 commit ddb017a6ff0f3b548114608de394b9f87cb13ccf @wilr wilr committed Apr 5, 2013
Showing with 85 additions and 56 deletions.
  1. +68 −9 model/fieldtypes/StringField.php
  2. +17 −47 model/fieldtypes/Text.php
@@ -1,22 +1,31 @@
<?php
/**
* An abstract base class for the string field types (i.e. Varchar and Text)
+ *
* @package framework
* @subpackage model
- * @author Pete Bacon Darwin
- *
*/
abstract class StringField extends DBField {
+
+ /**
+ * @var boolean
+ */
protected $nullifyEmpty = true;
+ /**
+ * @var array
+ */
private static $casting = array(
"LimitCharacters" => "Text",
+ 'LimitWordCount' => 'Text',
+ 'LimitWordCountXML' => 'HTMLText',
"LowerCase" => "Text",
"UpperCase" => "Text",
);
/**
- * Construct a string type field with a set of optional parameters
+ * Construct a string type field with a set of optional parameters.
+ *
* @param $name string The name of the field
* @param $options array An array of options e.g. array('nullifyEmpty'=>false). See
* {@link StringField::setOptions()} for information on the available options
@@ -27,6 +36,7 @@ public function __construct($name = null, $options = array()) {
if(is_array($options)){
$this->setOptions($options);
}
+
parent::__construct($name);
}
@@ -48,15 +58,20 @@ public function setOptions(array $options = array()) {
}
/**
- * Set whether this field stores empty strings rather than converting them to null
+ * Set whether this field stores empty strings rather than converting
+ * them to null.
+ *
* @param $value boolean True if empty strings are to be converted to null
*/
public function setNullifyEmpty($value) {
$this->nullifyEmpty = ($value ? true : false);
}
+
/**
- * Get whether this field stores empty strings rather than converting them to null
- * @return bool True if empty strings are to be converted to null
+ * Get whether this field stores empty strings rather than converting
+ * them to null
+ *
+ * @return boolean True if empty strings are to be converted to null
*/
public function getNullifyEmpty() {
return $this->nullifyEmpty;
@@ -93,6 +108,7 @@ public function prepValueForDB($value) {
*/
public function LimitCharacters($limit = 20, $add = '...') {
$value = trim($this->value);
+
if($this->stat('escape_type') == 'xml') {
$value = strip_tags($value);
$value = html_entity_decode($value, ENT_COMPAT, 'UTF-8');
@@ -102,23 +118,66 @@ public function LimitCharacters($limit = 20, $add = '...') {
} else {
$value = (mb_strlen($value) > $limit) ? mb_substr($value, 0, $limit) . $add : $value;
}
+
return $value;
}
+
/**
- * Converts the current value for this Enum DBField to lowercase.
+ * Limit this field's content by a number of words.
+ *
+ * CAUTION: This is not XML safe. Please use
+ * {@link LimitWordCountXML()} instead.
+ *
+ * @param int $numWords Number of words to limit by.
+ * @param string $add Ellipsis to add to the end of truncated string.
+ *
+ * @return string
+ */
+ public function LimitWordCount($numWords = 26, $add = '...') {
+ $this->value = trim(Convert::xml2raw($this->value));
+ $ret = explode(' ', $this->value, $numWords + 1);
+
+ if(count($ret) <= $numWords - 1) {
+ $ret = $this->value;
+ } else {
+ array_pop($ret);
+ $ret = implode(' ', $ret) . $add;
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Limit the number of words of the current field's
+ * content. This is XML safe, so characters like &
+ * are converted to &amp;
+ *
+ * @param int $numWords Number of words to limit by.
+ * @param string $add Ellipsis to add to the end of truncated string.
+ *
+ * @return string
+ */
+ public function LimitWordCountXML($numWords = 26, $add = '...') {
+ $ret = $this->LimitWordCount($numWords, $add);
+
+ return Convert::raw2xml($ret);
+ }
+
+ /**
+ * Converts the current value for this StringField to lowercase.
+ *
* @return string
*/
public function LowerCase() {
return mb_strtolower($this->value);
}
/**
- * Converts the current value for this Enum DBField to uppercase.
+ * Converts the current value for this StringField to uppercase.
* @return string
*/
public function UpperCase() {
return mb_strtoupper($this->value);
}
-
}
View
@@ -38,41 +38,24 @@ class Text extends StringField {
* @see DBField::requireField()
*/
public function requireField() {
- $parts=Array(
- 'datatype'=>'mediumtext',
- 'character set'=>'utf8',
- 'collate'=>'utf8_general_ci',
- 'arrayValue'=>$this->arrayValue
+ $parts = array(
+ 'datatype' => 'mediumtext',
+ 'character set' => 'utf8',
+ 'collate' => 'utf8_general_ci',
+ 'arrayValue' => $this->arrayValue
);
- $values=Array('type'=>'text', 'parts'=>$parts);
+
+ $values= array(
+ 'type' => 'text',
+ 'parts' => $parts
+ );
+
DB::requireField($this->tableName, $this->name, $values, $this->default);
}
/**
- * Limit this field's content by a number of words.
- * CAUTION: This is not XML safe. Please use
- * {@link LimitWordCountXML()} instead.
+ * Return the value of the field stripped of html tags.
*
- * @param int $numWords Number of words to limit by
- * @param string $add Ellipsis to add to the end of truncated string
- * @return string
- */
- public function LimitWordCount($numWords = 26, $add = '...') {
- $this->value = trim(Convert::xml2raw($this->value));
- $ret = explode(' ', $this->value, $numWords + 1);
-
- if(count($ret) <= $numWords - 1) {
- $ret = $this->value;
- } else {
- array_pop($ret);
- $ret = implode(' ', $ret) . $add;
- }
-
- return $ret;
- }
-
- /**
- * Return the value of the field stripped of html tags
* @return string
*/
public function NoHTML() {
@@ -86,27 +69,16 @@ public function NoHTML() {
public function AbsoluteLinks() {
return HTTP::absoluteURLs($this->value);
}
-
- /**
- * Limit the number of words of the current field's
- * content. This is XML safe, so characters like &
- * are converted to &amp;
- *
- * @param int $numWords Number of words to limit by
- * @param string $add Ellipsis to add to the end of truncated string
- * @return string
- */
- public function LimitWordCountXML($numWords = 26, $add = '...') {
- $ret = $this->LimitWordCount($numWords, $add);
- return Convert::raw2xml($ret);
- }
/**
* Limit sentences, can be controlled by passing an integer.
+ *
* @param int $sentCount The amount of sentences you want.
*/
public function LimitSentences($sentCount = 2) {
- if(!is_numeric($sentCount)) user_error("Text::LimitSentence() expects one numeric argument", E_USER_NOTICE);
+ if(!is_numeric($sentCount)) {
+ user_error("Text::LimitSentence() expects one numeric argument", E_USER_NOTICE);
+ }
$output = array();
$data = trim(Convert::xml2raw($this->value));
@@ -181,7 +153,7 @@ public function Summary($maxWords = 50) {
}
/**
- * Performs the same function as the big summary, but doesnt trim new paragraphs off data.
+ * Performs the same function as the big summary, but doesn't trim new paragraphs off data.
* Caution: Not XML/HTML-safe - does not respect closing tags.
*/
public function BigSummary($maxWords = 50, $plain = 1) {
@@ -348,5 +320,3 @@ public function scaffoldSearchField($title = null, $params = null) {
return new TextField($this->name, $title);
}
}
-
-

0 comments on commit ddb017a

Please sign in to comment.