Mercurial > hg > isophonics-drupal-site
annotate core/lib/Drupal/Component/Render/MarkupTrait.php @ 12:7a779792577d
Update Drupal core to v8.4.5 (via Composer)
author | Chris Cannam |
---|---|
date | Fri, 23 Feb 2018 15:52:07 +0000 |
parents | 4c8ae668cc8c |
children | 129ea1e6d783 |
rev | line source |
---|---|
Chris@0 | 1 <?php |
Chris@0 | 2 |
Chris@0 | 3 namespace Drupal\Component\Render; |
Chris@0 | 4 |
Chris@0 | 5 use Drupal\Component\Utility\Unicode; |
Chris@0 | 6 |
Chris@0 | 7 /** |
Chris@0 | 8 * Implements MarkupInterface and Countable for rendered objects. |
Chris@0 | 9 * |
Chris@0 | 10 * @see \Drupal\Component\Render\MarkupInterface |
Chris@0 | 11 */ |
Chris@0 | 12 trait MarkupTrait { |
Chris@0 | 13 |
Chris@0 | 14 /** |
Chris@0 | 15 * The safe string. |
Chris@0 | 16 * |
Chris@0 | 17 * @var string |
Chris@0 | 18 */ |
Chris@0 | 19 protected $string; |
Chris@0 | 20 |
Chris@0 | 21 /** |
Chris@0 | 22 * Creates a Markup object if necessary. |
Chris@0 | 23 * |
Chris@0 | 24 * If $string is equal to a blank string then it is not necessary to create a |
Chris@0 | 25 * Markup object. If $string is an object that implements MarkupInterface it |
Chris@0 | 26 * is returned unchanged. |
Chris@0 | 27 * |
Chris@0 | 28 * @param mixed $string |
Chris@0 | 29 * The string to mark as safe. This value will be cast to a string. |
Chris@0 | 30 * |
Chris@0 | 31 * @return string|\Drupal\Component\Render\MarkupInterface |
Chris@0 | 32 * A safe string. |
Chris@0 | 33 */ |
Chris@0 | 34 public static function create($string) { |
Chris@0 | 35 if ($string instanceof MarkupInterface) { |
Chris@0 | 36 return $string; |
Chris@0 | 37 } |
Chris@0 | 38 $string = (string) $string; |
Chris@0 | 39 if ($string === '') { |
Chris@0 | 40 return ''; |
Chris@0 | 41 } |
Chris@0 | 42 $safe_string = new static(); |
Chris@0 | 43 $safe_string->string = $string; |
Chris@0 | 44 return $safe_string; |
Chris@0 | 45 } |
Chris@0 | 46 |
Chris@0 | 47 /** |
Chris@0 | 48 * Returns the string version of the Markup object. |
Chris@0 | 49 * |
Chris@0 | 50 * @return string |
Chris@0 | 51 * The safe string content. |
Chris@0 | 52 */ |
Chris@0 | 53 public function __toString() { |
Chris@0 | 54 return $this->string; |
Chris@0 | 55 } |
Chris@0 | 56 |
Chris@0 | 57 /** |
Chris@0 | 58 * Returns the string length. |
Chris@0 | 59 * |
Chris@0 | 60 * @return int |
Chris@0 | 61 * The length of the string. |
Chris@0 | 62 */ |
Chris@0 | 63 public function count() { |
Chris@0 | 64 return Unicode::strlen($this->string); |
Chris@0 | 65 } |
Chris@0 | 66 |
Chris@0 | 67 /** |
Chris@0 | 68 * Returns a representation of the object for use in JSON serialization. |
Chris@0 | 69 * |
Chris@0 | 70 * @return string |
Chris@0 | 71 * The safe string content. |
Chris@0 | 72 */ |
Chris@0 | 73 public function jsonSerialize() { |
Chris@0 | 74 return $this->__toString(); |
Chris@0 | 75 } |
Chris@0 | 76 |
Chris@0 | 77 } |