|
1
|
|
|
<?php |
|
2
|
|
|
namespace App; |
|
3
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
4
|
|
|
use Illuminate\Contracts\Support\Renderable; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class Text |
|
8
|
|
|
* @package App |
|
9
|
|
|
*/ |
|
10
|
|
|
class Text implements \JsonSerializable, Renderable |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $content; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Text constructor. |
|
19
|
|
|
* @param mixed $content |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($content) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->setContent($content); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param $result |
|
28
|
|
|
* @return null|string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function parse($result) |
|
31
|
|
|
{ |
|
32
|
|
|
switch (true) { |
|
33
|
|
|
case is_string($result) || is_int($result) || |
|
34
|
|
|
(is_object($result) && method_exists($result, '__toString')): |
|
35
|
|
|
return (string)$result; |
|
36
|
|
|
|
|
37
|
|
|
case is_array($result): |
|
38
|
|
|
return implode("\n", $result); |
|
39
|
|
|
|
|
40
|
|
|
case is_object($result) && method_exists($result, 'toString'): |
|
41
|
|
|
return $result->toString(); |
|
42
|
|
|
|
|
43
|
|
|
case is_object($result) && $result instanceof Arrayable: |
|
44
|
|
|
return implode("\n", $result->toArray()); |
|
45
|
|
|
|
|
46
|
|
|
case ($result instanceof static): |
|
47
|
|
|
return $result->getContent(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return Text[]|array |
|
55
|
|
|
*/ |
|
56
|
|
|
public function sentences() |
|
57
|
|
|
{ |
|
58
|
|
|
$result = []; |
|
59
|
|
|
foreach (preg_split('/(\n|:[a-z_]+:|\.|\){2,})/isu', $this->content) as $text) { |
|
60
|
|
|
$text = trim($text); |
|
61
|
|
|
if ($text) { $result[] = new Text($text); } |
|
62
|
|
|
} |
|
63
|
|
|
return $result; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return Text |
|
68
|
|
|
* @TODO Improve regular expressions needed |
|
69
|
|
|
*/ |
|
70
|
|
|
public function escape() |
|
71
|
|
|
{ |
|
72
|
|
|
$content = $this->content; |
|
73
|
|
|
|
|
74
|
|
|
$content = preg_replace('/(?P<char>(?:_|\*))(.+?)(?P=char)/isu', '\\\$1$2\\\$1', $content); |
|
75
|
|
|
$content = preg_replace('/\*\*(.+?)\*\*/isu', '*\\*$1*\\*', $content); |
|
76
|
|
|
$content = preg_replace('/\-\-(\-)+/isu', '\-\-\-', $content); |
|
77
|
|
|
$content = preg_replace('/\n*^(?!\w\s+)(#)/isu', '\\#', $content); |
|
78
|
|
|
$content = preg_replace('/\[(.*?)\]\((.*?)\)/isu', '\\[$1\\]\\($2\\)', $content); |
|
79
|
|
|
|
|
80
|
|
|
return new Text($content); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param $type |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
public function typeof($type) |
|
88
|
|
|
{ |
|
89
|
|
|
$function = 'is_' . strtolower($type); |
|
90
|
|
|
|
|
91
|
|
|
return $function($this->content); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $content |
|
96
|
|
|
* @param bool $ignoreCase |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function is($content, $ignoreCase = false) |
|
100
|
|
|
{ |
|
101
|
|
|
if ($ignoreCase) { |
|
102
|
|
|
return mb_strtolower($this->content) === mb_strtolower($content); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->content === $content; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param $pattern |
|
110
|
|
|
* @return int |
|
111
|
|
|
*/ |
|
112
|
|
|
public function match($pattern) |
|
113
|
|
|
{ |
|
114
|
|
|
return preg_match('/^' . $pattern . '$/isu', $this->content); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param $pattern |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
|
|
public function matches($pattern) |
|
122
|
|
|
{ |
|
123
|
|
|
preg_match('/^' . $pattern . '$/isu', $this->content, $matches); |
|
124
|
|
|
|
|
125
|
|
|
return $matches; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param $content |
|
130
|
|
|
* @param $ignoreCase |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function contains($content, $ignoreCase = false) |
|
134
|
|
|
{ |
|
135
|
|
|
if ($ignoreCase) { |
|
136
|
|
|
return mb_strstr(mb_strtolower($this->content), mb_strtolower($content)); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return mb_strstr($this->content, $content); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return Text |
|
144
|
|
|
*/ |
|
145
|
|
|
public function onlyWords() |
|
146
|
|
|
{ |
|
147
|
|
|
$content = $this->content; |
|
148
|
|
|
|
|
149
|
|
|
$content = preg_replace('/@[a-z0-9_\-]+/iu', '', $content); |
|
150
|
|
|
$content = preg_replace('/[^\s\w]/iu', '', $content); |
|
151
|
|
|
$content = str_replace(["\n", "\r"], '', $content); |
|
152
|
|
|
|
|
153
|
|
|
return new Text(trim($content)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
public function toLowerCase() |
|
160
|
|
|
{ |
|
161
|
|
|
return new Text(mb_strtolower($this->content)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
public function toUpperCase() |
|
168
|
|
|
{ |
|
169
|
|
|
return new Text(mb_strtoupper($this->content)); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getContent() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->content; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param $content |
|
182
|
|
|
* @return Text |
|
183
|
|
|
*/ |
|
184
|
|
|
public function setContent($content) |
|
185
|
|
|
{ |
|
186
|
|
|
$content = $this->parse($content); |
|
187
|
|
|
|
|
188
|
|
|
$this->content = $content; |
|
189
|
|
|
|
|
190
|
|
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return Text |
|
195
|
|
|
*/ |
|
196
|
|
|
public function italic() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->wrap('_'); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return Text |
|
203
|
|
|
*/ |
|
204
|
|
|
public function bold() |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->wrap('**'); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param string|null $lang |
|
211
|
|
|
* @return Text |
|
212
|
|
|
*/ |
|
213
|
|
|
public function code(string $lang = null) |
|
214
|
|
|
{ |
|
215
|
|
|
if ($lang === null) { |
|
216
|
|
|
return $this->wrap('`'); |
|
217
|
|
|
} |
|
218
|
|
|
return $this->wrap('```' . $lang . "\n", "\n```"); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param $src |
|
223
|
|
|
* @param bool $after |
|
224
|
|
|
* @return Text |
|
225
|
|
|
*/ |
|
226
|
|
|
public function image($src, $after = true) |
|
227
|
|
|
{ |
|
228
|
|
|
$code = ''; |
|
229
|
|
|
if ($after) { |
|
230
|
|
|
return $this->after($code); |
|
231
|
|
|
} |
|
232
|
|
|
return $this->before($code); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* @param bool $after |
|
237
|
|
|
* @return Text |
|
238
|
|
|
*/ |
|
239
|
|
|
public function hr($after = true) |
|
240
|
|
|
{ |
|
241
|
|
|
if ($after) { |
|
242
|
|
|
return $this->after('---', true); |
|
243
|
|
|
} |
|
244
|
|
|
return $this->before('---', true); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @return Text |
|
249
|
|
|
*/ |
|
250
|
|
|
public function quote() |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->before('> '); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param string $symbol |
|
257
|
|
|
* @param string|null $symbolAfter |
|
258
|
|
|
* @return Text |
|
259
|
|
|
*/ |
|
260
|
|
|
public function wrap(string $symbol, string $symbolAfter = null) |
|
261
|
|
|
{ |
|
262
|
|
|
if ($symbolAfter === null) { |
|
263
|
|
|
$symbolAfter = $symbol; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
$this->setContent($symbol . $this->getContent() . $symbolAfter); |
|
267
|
|
|
return $this; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @param $text |
|
272
|
|
|
* @param bool $newline |
|
273
|
|
|
* @return Text |
|
274
|
|
|
*/ |
|
275
|
|
|
public function before($text, $newline = false) |
|
276
|
|
|
{ |
|
277
|
|
|
return $this->wrap($text . ($newline ? "\n" : ''), ''); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @param $text |
|
282
|
|
|
* @param bool $newline |
|
283
|
|
|
* @return Text |
|
284
|
|
|
*/ |
|
285
|
|
|
public function after($text, $newline = false) |
|
286
|
|
|
{ |
|
287
|
|
|
return $this->wrap('', ($newline ? "\n" : '') . $text); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @return string |
|
292
|
|
|
*/ |
|
293
|
|
|
public function __toString() |
|
294
|
|
|
{ |
|
295
|
|
|
return $this->toString(); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @return string |
|
300
|
|
|
*/ |
|
301
|
|
|
public function toString() |
|
302
|
|
|
{ |
|
303
|
|
|
return (string)$this->content; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* @return string |
|
308
|
|
|
*/ |
|
309
|
|
|
public function jsonSerialize() |
|
310
|
|
|
{ |
|
311
|
|
|
return (string)$this; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* @return string |
|
316
|
|
|
*/ |
|
317
|
|
|
public function render() |
|
318
|
|
|
{ |
|
319
|
|
|
return $this->toString(); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
|