|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Controller\Api\Profile; |
|
4
|
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\WallAnswer; |
|
6
|
|
|
use Apps\ActiveRecord\WallPost; |
|
7
|
|
|
use Ffcms\Core\Exception\NativeException; |
|
8
|
|
|
use Ffcms\Core\Helper\Date; |
|
9
|
|
|
use Ffcms\Core\Helper\Type\Any; |
|
10
|
|
|
use Ffcms\Core\Network\Request; |
|
11
|
|
|
use Ffcms\Core\Network\Response; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Trait ActionShowWallAnswers |
|
15
|
|
|
* @package Apps\Controller\Api\Profile |
|
16
|
|
|
* @property Request $request |
|
17
|
|
|
* @property Response $response |
|
18
|
|
|
* @method void setJsonHeader() |
|
19
|
|
|
*/ |
|
20
|
|
|
trait ActionShowWallAnswers |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Show all answers for this post id |
|
24
|
|
|
* @param string $postId |
|
25
|
|
|
* @return string |
|
26
|
|
|
* @throws NativeException |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
public function showWallAnswers(string $postId): ?string |
|
30
|
|
|
{ |
|
31
|
|
|
$this->setJsonHeader(); |
|
32
|
|
|
|
|
33
|
|
|
// check input post id num |
|
34
|
|
|
if (!Any::isInt($postId) || $postId < 1) { |
|
35
|
|
|
throw new NativeException('Wrong input data'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// try to find this post |
|
39
|
|
|
$object = WallPost::find($postId); |
|
40
|
|
|
|
|
41
|
|
|
// check if post is exist |
|
42
|
|
|
if (!$object) { |
|
43
|
|
|
throw new NativeException('Wrong input data'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// get answer object with relation to user, profile and role table |
|
47
|
|
|
$answers = WallAnswer::with(['user', 'user.profile', 'user.role']) |
|
|
|
|
|
|
48
|
|
|
->where('post_id', $postId) |
|
49
|
|
|
->orderBy('id', 'DESC') |
|
50
|
|
|
->take(200) |
|
51
|
|
|
->get(); |
|
52
|
|
|
|
|
53
|
|
|
$response = []; |
|
54
|
|
|
/** @var WallAnswer[] $answers */ |
|
55
|
|
|
foreach ($answers as $answer) { |
|
56
|
|
|
// check if user exist |
|
57
|
|
|
if ($answer->user === null || $answer->user->id < 1) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
// generate response array |
|
61
|
|
|
$response[] = [ |
|
62
|
|
|
'answer_id' => $answer->id, |
|
63
|
|
|
'user_id' => $answer->user_id, |
|
64
|
|
|
'user_nick' => $answer->user->profile->getNickname(), |
|
65
|
|
|
'user_avatar' => $answer->user->profile->getAvatarUrl('small'), |
|
66
|
|
|
'user_color' => $answer->user->role->color, |
|
67
|
|
|
'answer_message' => $answer->message, |
|
68
|
|
|
'answer_date' => Date::humanize($answer->created_at) |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// encode and show json result |
|
73
|
|
|
return json_encode(['status' => 1, 'data' => $response]); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: