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