1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wingsline\Blog\Posts; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
trait PostPresenter |
8
|
|
|
{ |
9
|
|
|
public function getExcerptAttribute(): string |
10
|
|
|
{ |
11
|
|
|
// If we have an external url, we return the content, since we |
12
|
|
|
// do not have a block post, we rather have a linked content |
13
|
|
|
if ($this->external_url) { |
14
|
|
|
return $this->text; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
$excerpt = trim($this->text); |
18
|
|
|
|
19
|
|
|
// before the 1st blockquote |
20
|
|
|
$excerpt = Str::before($excerpt, '<blockquote>'); |
21
|
|
|
|
22
|
|
|
// after the h1, since we should have only one h1 on top of the article |
23
|
|
|
$excerpt = Str::after($excerpt, '</h1>'); |
24
|
|
|
|
25
|
|
|
// remove html |
26
|
|
|
$excerpt = strip_tags($excerpt); |
27
|
|
|
|
28
|
|
|
// replace multiple spaces |
29
|
|
|
$excerpt = preg_replace('/\\s+/', ' ', $excerpt); |
30
|
|
|
|
31
|
|
|
if (0 == \strlen($excerpt)) { |
32
|
|
|
return ''; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (\strlen($excerpt) <= 150) { |
36
|
|
|
return $excerpt; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$ww = wordwrap($excerpt, 150, "\n"); |
40
|
|
|
|
41
|
|
|
$excerpt = substr($ww, 0, strpos($ww, "\n")).'…'; |
42
|
|
|
|
43
|
|
|
return $excerpt; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getFormattedTitleAttribute(): string |
47
|
|
|
{ |
48
|
|
|
$prefix = $this->original_content |
49
|
|
|
? '★ ' |
50
|
|
|
: ''; |
51
|
|
|
|
52
|
|
|
return $prefix.$this->title; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getTagsTextAttribute(): string |
56
|
|
|
{ |
57
|
|
|
return $this |
58
|
|
|
->tags |
59
|
|
|
->pluck('name') |
60
|
|
|
->implode(', '); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getUrlAttribute(): string |
64
|
|
|
{ |
65
|
|
|
if ($this->external_url) { |
66
|
|
|
return $this->external_url; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return route('post', $this->slug); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|