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