|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Http\Resources; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Comment; |
|
6
|
|
|
use Coyote\Services\UrlBuilder; |
|
7
|
|
|
use Illuminate\Http\Resources\Json\JsonResource; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @property string $title |
|
11
|
|
|
* @property ?string $text |
|
12
|
|
|
* @property ?string $excerpt |
|
13
|
|
|
* @property string $slug |
|
14
|
|
|
* @property \Coyote\User $user |
|
15
|
|
|
* @property \Coyote\Tag[] $tags |
|
16
|
|
|
* @property Comment[] $commentsWithChildren |
|
17
|
|
|
* @property Comment[] $comments |
|
18
|
|
|
* @property int $comments_count |
|
19
|
|
|
* @property \Coyote\Guide\Vote[]|\Illuminate\Support\Collection $voters[] |
|
20
|
|
|
* @property \Coyote\Guide\Role[]|\Illuminate\Support\Collection $roles[] |
|
21
|
|
|
* @property \Coyote\Models\Subscription[]|\Illuminate\Support\Collection $subscribers |
|
22
|
|
|
* @property string $role |
|
23
|
|
|
* @property \Coyote\Models\Asset $assets |
|
24
|
|
|
*/ |
|
25
|
|
|
class GuideResource extends JsonResource |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Transform the resource into an array. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Illuminate\Http\Request $request |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
public function toArray($request) |
|
34
|
|
|
{ |
|
35
|
|
|
$user = $request->user(); |
|
36
|
|
|
|
|
37
|
|
|
return array_merge( |
|
38
|
|
|
$this->resource->only(['id', 'title', 'created_at', 'votes', 'views']), |
|
39
|
|
|
[ |
|
40
|
|
|
'slug' => $this->slug, |
|
41
|
|
|
'url' => UrlBuilder::guide($this->resource), |
|
42
|
|
|
|
|
43
|
|
|
'user' => new UserResource($this->user), |
|
44
|
|
|
'tags' => TagResource::collection($this->tags), |
|
45
|
|
|
'permissions' => [ |
|
46
|
|
|
'update' => $request->user()?->can('update', $this->resource) |
|
47
|
|
|
], |
|
48
|
|
|
|
|
49
|
|
|
'comments_count'=> $this->comments_count, |
|
50
|
|
|
'subscribers' => $this->subscribers()->count(), |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
$this->mergeWhen($this->text || $this->excerpt, function () { |
|
53
|
|
|
$parser = resolve('parser.post'); |
|
54
|
|
|
|
|
55
|
|
|
return [ |
|
56
|
|
|
'text' => $this->text, |
|
57
|
|
|
'html' => $parser->parse((string) $this->text), |
|
58
|
|
|
'excerpt_html' => $parser->parse((string) $this->excerpt) |
|
59
|
|
|
]; |
|
60
|
|
|
}), |
|
61
|
|
|
|
|
62
|
|
|
'is_voted' => $this->when($user, fn () => $this->voters->contains('user_id', $user->id), false), |
|
|
|
|
|
|
63
|
|
|
'is_subscribed' => $this->when($user, fn () => $this->subscribers->contains('user_id', $user->id), false), |
|
64
|
|
|
'comments' => $this->whenLoaded('commentsWithChildren', fn () => (new CommentCollection($this->commentsWithChildren))->setOwner($this->resource)), |
|
65
|
|
|
'assets' => $this->whenLoaded('assets', fn () => AssetsResource::collection($this->assets), []), |
|
66
|
|
|
'role' => $this->defaultRole() |
|
67
|
|
|
] |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function defaultRole(): string |
|
72
|
|
|
{ |
|
73
|
|
|
if (!$this->resource->relationLoaded('roles')) { |
|
74
|
|
|
return $this->role; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $this->roles[0]->role ?? $this->role; |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|