1 | <?php |
||
2 | |||
3 | namespace Usamamuneerchaudhary\Commentify\Models\Presenters; |
||
4 | |||
5 | use Illuminate\Support\HtmlString; |
||
6 | use Usamamuneerchaudhary\Commentify\Models\Comment; |
||
7 | use Usamamuneerchaudhary\Commentify\Models\User; |
||
8 | |||
9 | class CommentPresenter |
||
10 | { |
||
11 | /** |
||
12 | * @var Comment |
||
13 | */ |
||
14 | public Comment $comment; |
||
15 | |||
16 | /** |
||
17 | * @param Comment $comment |
||
18 | */ |
||
19 | public function __construct(Comment $comment) |
||
20 | { |
||
21 | $this->comment = $comment; |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * @return HtmlString |
||
26 | */ |
||
27 | public function markdownBody(): HtmlString |
||
28 | { |
||
29 | return new HtmlString(app('markdown')->convertToHtml($this->comment->body)); |
||
0 ignored issues
–
show
|
|||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @return mixed |
||
34 | */ |
||
35 | public function relativeCreatedAt(): mixed |
||
36 | { |
||
37 | return $this->comment->created_at->diffForHumans(); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param $text |
||
42 | * @return array|string |
||
43 | */ |
||
44 | public function replaceUserMentions($text): array|string |
||
45 | { |
||
46 | preg_match_all('/@([A-Za-z0-9_]+)/', $text, $matches); |
||
47 | $usernames = $matches[1]; |
||
48 | $replacements = []; |
||
49 | |||
50 | foreach ($usernames as $username) { |
||
51 | $user = User::where('name', $username)->first(); |
||
52 | |||
53 | if ($user) { |
||
54 | $userRoutePrefix = config('commentify.users_route_prefix', 'users'); |
||
55 | |||
56 | $replacements['@'.$username] = '<a href="/'.$userRoutePrefix.'/'.$username.'">@'.$username. |
||
57 | '</a>'; |
||
58 | } else { |
||
59 | $replacements['@'.$username] = '@'.$username; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | return str_replace(array_keys($replacements), array_values($replacements), $text); |
||
64 | } |
||
65 | |||
66 | |||
67 | } |
||
68 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.