1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Xetaravel\Models\Presenters; |
||||
6 | |||||
7 | use Illuminate\Database\Eloquent\Casts\Attribute; |
||||
8 | |||||
9 | trait BlogArticlePresenter |
||||
10 | { |
||||
11 | /** |
||||
12 | * The default banner used when there is no banner for the article. |
||||
13 | * |
||||
14 | * @var string |
||||
15 | */ |
||||
16 | protected string $defaultBanner = '/images/articles/default_banner.jpg'; |
||||
17 | |||||
18 | /** |
||||
19 | * Get the is_display field. |
||||
20 | * |
||||
21 | * @return Attribute |
||||
22 | */ |
||||
23 | protected function isDisplay(): Attribute |
||||
24 | { |
||||
25 | return Attribute::make( |
||||
26 | get: fn () => !(is_null($this->published_at) || $this->published_at > now()) |
||||
27 | ); |
||||
28 | } |
||||
29 | |||||
30 | /** |
||||
31 | * Parse a media and return it if isset or return the default banner. |
||||
32 | * |
||||
33 | * @param string $type The type of the media to get. |
||||
34 | * |
||||
35 | * @return string |
||||
36 | */ |
||||
37 | protected function parseMedia(string $type): string |
||||
38 | { |
||||
39 | if (isset($this->getMedia('article')[0])) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
40 | return $this->getMedia('article')[0]->getUrl($type); |
||||
41 | } |
||||
42 | |||||
43 | return $this->defaultBanner; |
||||
44 | } |
||||
45 | |||||
46 | /** |
||||
47 | * Get the show url. |
||||
48 | * |
||||
49 | * @return Attribute |
||||
50 | */ |
||||
51 | protected function showUrl(): Attribute |
||||
52 | { |
||||
53 | return Attribute::make( |
||||
54 | get: fn () => route('blog.article.show', ['slug' => $this->slug, 'id' => $this->getKey()]) |
||||
0 ignored issues
–
show
It seems like
getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
55 | ); |
||||
56 | } |
||||
57 | |||||
58 | /** |
||||
59 | * Get the article banner. |
||||
60 | * |
||||
61 | * @return Attribute |
||||
62 | */ |
||||
63 | public function articleBanner(): Attribute |
||||
64 | { |
||||
65 | return Attribute::make( |
||||
66 | get: fn () => $this->parseMedia('article.banner') |
||||
67 | ); |
||||
68 | } |
||||
69 | } |
||||
70 |