Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Component / Render / MarkupTrait.php
1 <?php
2
3 namespace Drupal\Component\Render;
4
5 /**
6  * Implements MarkupInterface and Countable for rendered objects.
7  *
8  * @see \Drupal\Component\Render\MarkupInterface
9  */
10 trait MarkupTrait {
11
12   /**
13    * The safe string.
14    *
15    * @var string
16    */
17   protected $string;
18
19   /**
20    * Creates a Markup object if necessary.
21    *
22    * If $string is equal to a blank string then it is not necessary to create a
23    * Markup object. If $string is an object that implements MarkupInterface it
24    * is returned unchanged.
25    *
26    * @param mixed $string
27    *   The string to mark as safe. This value will be cast to a string.
28    *
29    * @return string|\Drupal\Component\Render\MarkupInterface
30    *   A safe string.
31    */
32   public static function create($string) {
33     if ($string instanceof MarkupInterface) {
34       return $string;
35     }
36     $string = (string) $string;
37     if ($string === '') {
38       return '';
39     }
40     $safe_string = new static();
41     $safe_string->string = $string;
42     return $safe_string;
43   }
44
45   /**
46    * Returns the string version of the Markup object.
47    *
48    * @return string
49    *   The safe string content.
50    */
51   public function __toString() {
52     return $this->string;
53   }
54
55   /**
56    * Returns the string length.
57    *
58    * @return int
59    *   The length of the string.
60    */
61   public function count() {
62     return mb_strlen($this->string);
63   }
64
65   /**
66    * Returns a representation of the object for use in JSON serialization.
67    *
68    * @return string
69    *   The safe string content.
70    */
71   public function jsonSerialize() {
72     return $this->__toString();
73   }
74
75 }