-
Notifications
You must be signed in to change notification settings - Fork 2
/
MarkupTrait.php
75 lines (67 loc) · 1.55 KB
/
MarkupTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace Drupal\Component\Render;
/**
* Implements MarkupInterface and Countable for rendered objects.
*
* @see \Drupal\Component\Render\MarkupInterface
*/
trait MarkupTrait {
/**
* The safe string.
*
* @var string
*/
protected $string;
/**
* Creates a Markup object if necessary.
*
* If $string is equal to a blank string then it is not necessary to create a
* Markup object. If $string is an object that implements MarkupInterface 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\Render\MarkupInterface
* A safe string.
*/
public static function create($string) {
if ($string instanceof MarkupInterface) {
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 Markup 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(): int {
return mb_strlen($this->string);
}
/**
* Returns a representation of the object for use in JSON serialization.
*
* @return string
* The safe string content.
*/
public function jsonSerialize(): string {
return $this->__toString();
}
}