trait SafeStringTrait in Service Container 7
Same name and namespace in other branches
- 7.2 lib/Drupal/Component/Utility/SafeStringTrait.php \Drupal\Component\Utility\SafeStringTrait
Implements SafeStringInterface and Countable for rendered objects.
Hierarchy
- trait \Drupal\Component\Utility\SafeStringTrait
See also
\Drupal\Component\Utility\SafeStringInterface
File
- lib/
Drupal/ Component/ Utility/ SafeStringTrait.php, line 15 - Contains \Drupal\Component\Utility\SafeStringTrait.
Namespace
Drupal\Component\UtilityView source
trait SafeStringTrait {
/**
* The safe string.
*
* @var string
*/
protected $string;
/**
* Creates a SafeString object if necessary.
*
* If $string is equal to a blank string then it is not necessary to create a
* SafeString object. If $string is an object that implements
* SafeStringInterface it is returned unchanged.
*
* @param mixed $string
* The string to mark as safe. This value will be cast to a string.
*
* @return string|\Drupal\Component\Utility\SafeStringInterface
* A safe string.
*/
public static function create($string) {
if ($string instanceof SafeStringInterface) {
return $string;
}
$string = (string) $string;
if ($string === '') {
return '';
}
$safe_string = new static();
$safe_string->string = $string;
return $safe_string;
}
/**
* Returns the string version of the SafeString object.
*
* @return string
* The safe string content.
*/
public function __toString() {
return $this->string;
}
/**
* Returns the string length.
*
* @return int
* The length of the string.
*/
public function count() {
return Unicode::strlen($this->string);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SafeStringTrait:: |
protected | property | The safe string. | |
SafeStringTrait:: |
public | function | Returns the string length. | |
SafeStringTrait:: |
public static | function | Creates a SafeString object if necessary. | |
SafeStringTrait:: |
public | function | Returns the string version of the SafeString object. |