2015-09-04 13:20:09 -07:00
< ? php
/**
* @ file
2015-10-08 11:40:12 -07:00
* Contains \Drupal\Component\Render\MarkupTrait .
2015-09-04 13:20:09 -07:00
*/
2015-10-08 11:40:12 -07:00
namespace Drupal\Component\Render ;
use Drupal\Component\Utility\Unicode ;
2015-09-04 13:20:09 -07:00
/**
2015-10-08 11:40:12 -07:00
* Implements MarkupInterface and Countable for rendered objects .
2015-09-04 13:20:09 -07:00
*
2015-10-08 11:40:12 -07:00
* @ see \Drupal\Component\Render\MarkupInterface
2015-09-04 13:20:09 -07:00
*/
2015-10-08 11:40:12 -07:00
trait MarkupTrait {
2015-09-04 13:20:09 -07:00
/**
* The safe string .
*
* @ var string
*/
protected $string ;
/**
2015-10-08 11:40:12 -07:00
* Creates a Markup object if necessary .
2015-09-04 13:20:09 -07:00
*
* If $string is equal to a blank string then it is not necessary to create a
2015-10-08 11:40:12 -07:00
* Markup object . If $string is an object that implements MarkupInterface it
* is returned unchanged .
2015-09-04 13:20:09 -07:00
*
* @ param mixed $string
* The string to mark as safe . This value will be cast to a string .
*
2015-10-08 11:40:12 -07:00
* @ return string | \Drupal\Component\Render\MarkupInterface
2015-09-04 13:20:09 -07:00
* A safe string .
*/
public static function create ( $string ) {
2015-10-08 11:40:12 -07:00
if ( $string instanceof MarkupInterface ) {
2015-09-04 13:20:09 -07:00
return $string ;
}
$string = ( string ) $string ;
if ( $string === '' ) {
return '' ;
}
$safe_string = new static ();
$safe_string -> string = $string ;
return $safe_string ;
}
/**
2015-10-08 11:40:12 -07:00
* Returns the string version of the Markup object .
2015-09-04 13:20:09 -07:00
*
* @ 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 );
}
/**
* Returns a representation of the object for use in JSON serialization .
*
* @ return string
* The safe string content .
*/
public function jsonSerialize () {
return $this -> __toString ();
}
}