|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ReliQArts\Mardin\Transformers; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use ReliQArts\Mardin\Contracts\Thread; |
|
7
|
|
|
use League\Fractal\TransformerAbstract; |
|
8
|
|
|
use ReliQArts\Mardin\Helpers\StringHelper; |
|
9
|
|
|
use ReliQArts\Mardin\Contracts\UserTransformer; |
|
10
|
|
|
|
|
11
|
|
|
class ThreadTransformer extends TransformerAbstract |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* List of resources available to include. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $availableIncludes = [ |
|
19
|
|
|
'messages', |
|
20
|
|
|
'unreadMessages', |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* List of resources to automatically include. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $defaultIncludes = [ |
|
29
|
|
|
'latestMessage', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Transform the data. |
|
34
|
|
|
* @return array API suitable information. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function transform(Thread $thread) |
|
37
|
|
|
{ |
|
38
|
|
|
$user = auth()->user(); |
|
39
|
|
|
$userId = $user ? $user->id : 0; |
|
40
|
|
|
$unread = $thread->isUnread($userId); |
|
41
|
|
|
$unreadCount = $thread->userUnreadMessagesCount($userId); |
|
42
|
|
|
$participants = $thread->participantsString($user); |
|
43
|
|
|
|
|
44
|
|
|
return [ |
|
45
|
|
|
'id' => (int) $thread->id, |
|
|
|
|
|
|
46
|
|
|
'subject' => $thread->subject, |
|
|
|
|
|
|
47
|
|
|
'url' => route('show-message', ['id' => $thread->id]), |
|
|
|
|
|
|
48
|
|
|
'new' => $unreadCount, |
|
49
|
|
|
'unread' => $unread, |
|
50
|
|
|
'unread_count' => $unreadCount, |
|
51
|
|
|
'participants' => $participants, |
|
52
|
|
|
'participant_count' => $thread->participants()->count(), |
|
53
|
|
|
'deleted' => $thread->deleted_at, |
|
|
|
|
|
|
54
|
|
|
'created_at' => StringHelper::date(Carbon::createFromFormat('Y-n-j G:i:s', $thread->created_at)), |
|
|
|
|
|
|
55
|
|
|
'created_at_raw' => $thread->created_at, |
|
|
|
|
|
|
56
|
|
|
'updated_at' => $thread->updated_at ? StringHelper::date(Carbon::createFromFormat('Y-n-j G:i:s', $thread->updated_at)) : 'N/A', |
|
|
|
|
|
|
57
|
|
|
'updated_at_raw' => $thread->updated_at, |
|
|
|
|
|
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Include Messages. |
|
63
|
|
|
* |
|
64
|
|
|
* @param Thread $thread |
|
65
|
|
|
* @return \League\Fractal\Resource\Collection |
|
|
|
|
|
|
66
|
|
|
*/ |
|
67
|
|
|
public function includeMessages(Thread $thread) |
|
68
|
|
|
{ |
|
69
|
|
|
if ($messages = $thread->messages) { |
|
|
|
|
|
|
70
|
|
|
return $this->collection($messages, new MessageTransformer); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Include unread Messages. |
|
76
|
|
|
* |
|
77
|
|
|
* @param Thread $thread |
|
78
|
|
|
* @return \League\Fractal\Resource\Collection |
|
|
|
|
|
|
79
|
|
|
*/ |
|
80
|
|
|
public function includeUnreadMessages(Thread $thread) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($messages = $thread->userUnreadMessages(auth()->user()->id)) { |
|
83
|
|
|
return $this->collection($messages, new MessageTransformer); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Include last Message. |
|
89
|
|
|
* |
|
90
|
|
|
* @param Thread $thread |
|
91
|
|
|
* @return \League\Fractal\Resource\Item |
|
|
|
|
|
|
92
|
|
|
*/ |
|
93
|
|
|
public function includeLatestMessage(Thread $thread) |
|
94
|
|
|
{ |
|
95
|
|
|
if ($message = $thread->latestMessage) { |
|
|
|
|
|
|
96
|
|
|
return $this->item($message, new MessageTransformer); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Include Participants. |
|
102
|
|
|
* |
|
103
|
|
|
* @param Thread $thread |
|
104
|
|
|
* @return \League\Fractal\Resource\Collection |
|
|
|
|
|
|
105
|
|
|
*/ |
|
106
|
|
|
public function includeParticipants(Thread $thread) |
|
107
|
|
|
{ |
|
108
|
|
|
if ($participants = $thread->users) { |
|
|
|
|
|
|
109
|
|
|
return $this->collection($participants, new UserTransformer); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: