Debian Codegraph @

vendor/ipl/html/src/TemplateString.php

raw
1 <?php
2
3 namespace ipl\Html;
4
5 use Exception;
6
7 /**
8 * Render {{#mustache}}Mustache{{/mustache}}-like string from {@link ValidHtml} element arguments
9 *
10 * # Example Usage
11 * ```
12 * $info = TemplateString::create(
13 * 'Follow the {{#doc}}HTML documentation{{/doc}} for more information on {{#strong}}HTML elements{{/strong}}',
14 * [
15 * 'doc' => new Link(null, 'doc/html'),
16 * 'strong' => Html::tag('strong')
17 * ]
18 * );
19 * ```
20 */
21 class TemplateString extends FormattedString
22 {
23 /** @var array */
24 protected $templateArgs = [];
25
26 /** @var int */
27 protected $pos = 0;
28
29 /** @var string */
30 protected $string;
31
32 /** @var int */
33 protected $length;
34
35 public function __construct($format, $args = null)
36 {
37 $parentArgs = [];
38 foreach ($args ?: [] as $val) {
39 if (is_array($val) && is_string(key($val))) {
40 $this->templateArgs += $val;
41 } else {
42 $parentArgs[] = $val;
43 }
44 }
45
46 parent::__construct($format, $parentArgs);
47 }
48
49 /**
50 * Parse template strings
51 *
52 * @param ?string $for template name
53 * @return HtmlDocument
54 * @throws Exception in case of missing template argument or unbounded open or close templates
55 */
56 protected function parseTemplates($for = null)
57 {
58 $buffer = '';
59
60 while (($char = $this->readChar()) !== false) {
61 if ($char !== '{') {
62 $buffer .= $char;
63 continue;
64 }
65
66 $nextChar = $this->readChar();
67 if ($nextChar !== '{') {
68 $buffer .= $char . $nextChar;
69 continue;
70 }
71
72 $templateHandle = $this->readChar();
73 $start = $templateHandle === '#';
74 $end = $templateHandle === '/';
75
76 $templateKey = $this->readUntil('}');
77 // if the string following '{{#' is read up to the last character or (length - 1)th character
78 // then it is not a template
79 if ($this->pos >= $this->length - 1) {
80 $buffer .= $char . $nextChar . $templateHandle . $templateKey;
81 continue;
82 }
83
84 $this->pos++;
85 $closeChar = $this->readChar();
86
87 if ($closeChar !== '}') {
88 $buffer .= $char . $nextChar . $templateHandle . $templateKey . '}' . $closeChar;
89 continue;
90 }
91
92 if ($start) {
93 if (isset($this->templateArgs[$templateKey])) {
94 $wrapper = $this->templateArgs[$templateKey];
95
96 $buffer .= $this->parseTemplates($templateKey)->prependWrapper($wrapper);
97 } else {
98 throw new Exception(sprintf(
99 'Missing template argument: %s ',
100 $templateKey
101 ));
102 }
103 } elseif ($for === $templateKey && $end) {
104 // close the template
105 $for = null;
106 break;
107 } else {
108 // throw exception for unbounded closing of templates
109 throw new Exception(sprintf(
110 'Unbound closing of template: %s',
111 $templateKey
112 ));
113 }
114 }
115
116 if ($this->pos === $this->length && $for !== null) {
117 throw new Exception(sprintf(
118 'Unbound opening of template: %s',
119 $for
120 ));
121 }
122
123 return (new HtmlDocument())->addHtml(HtmlString::create($buffer));
124 }
125
126 /**
127 * Read until any of the given chars appears
128 *
129 * @param string ...$chars
130 *
131 * @return string
132 */
133 protected function readUntil(...$chars)
134 {
135 $buffer = '';
136 while (($c = $this->readChar()) !== false) {
137 if (in_array($c, $chars, true)) {
138 $this->pos--;
139 break;
140 }
141
142 $buffer .= $c;
143 }
144
145 return $buffer;
146 }
147
148 /**
149 * Read a single character
150 *
151 * @return false|string false if there is no character left
152 */
153 protected function readChar()
154 {
155 if ($this->length > $this->pos) {
156 return $this->string[$this->pos++];
157 }
158
159 return false;
160 }
161
162 public function render()
163 {
164 $formattedstring = parent::render();
165 if (empty($this->templateArgs)) {
166 return $formattedstring;
167 }
168
169 $this->string = $formattedstring;
170
171 $this->length = strlen($formattedstring);
172
173 return $this->parseTemplates()->render();
174 }
175 }