1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Api\Comments; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Apps\ActiveRecord\CommentAnswer; |
7
|
|
|
use Apps\ActiveRecord\CommentPost; |
8
|
|
|
use Ffcms\Core\App; |
9
|
|
|
use Ffcms\Core\Arch\Model; |
10
|
|
|
use Ffcms\Core\Exception\JsonException; |
11
|
|
|
use Ffcms\Core\Helper\Date; |
12
|
|
|
use Ffcms\Core\Helper\Type\Str; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class EntityCommentPostData. Unified model to get comment specified output to json response. |
16
|
|
|
* @package Apps\Model\Api\Comments |
17
|
|
|
*/ |
18
|
|
|
class EntityCommentData extends Model |
19
|
|
|
{ |
20
|
|
|
/** @var \Apps\ActiveRecord\CommentPost|\Apps\ActiveRecord\CommentAnswer $_record */ |
21
|
|
|
private $_record; |
22
|
|
|
private $_type; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* EntityCommentPostData constructor. Pass inside the model active record object of comment post or answer |
26
|
|
|
* @param $record |
27
|
|
|
* @throws JsonException |
28
|
|
|
*/ |
29
|
|
|
public function __construct($record) |
30
|
|
|
{ |
31
|
|
|
$this->_record = $record; |
32
|
|
|
if ($this->_record instanceof CommentPost) { |
33
|
|
|
$this->_type = 'post'; |
34
|
|
|
} elseif ($this->_record instanceof CommentAnswer) { |
35
|
|
|
$this->_type = 'answer'; |
36
|
|
|
} else { |
37
|
|
|
throw new JsonException('Unknown comment request'); |
38
|
|
|
} |
39
|
|
|
parent::__construct(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Prepare output comment post information array |
44
|
|
|
* @return array|null |
45
|
|
|
*/ |
46
|
|
|
public function make() |
47
|
|
|
{ |
48
|
|
|
if ($this->_record === null) { |
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// build user data |
53
|
|
|
$userName = __('Unknown'); |
54
|
|
|
$userAvatar = App::$Alias->scriptUrl . '/upload/user/avatar/small/default.jpg'; |
55
|
|
|
$userObject = $this->_record->getUser(); |
56
|
|
|
if ($userObject !== null) { |
57
|
|
|
$userName = $userObject->getProfile()->nick; |
58
|
|
|
$userAvatar = $userObject->getProfile()->getAvatarUrl('small'); |
59
|
|
|
} else { |
60
|
|
|
if (!Str::likeEmpty($this->_record->guest_name)) { |
|
|
|
|
61
|
|
|
$userName = App::$Security->strip_tags($this->_record->guest_name); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// return output json data |
66
|
|
|
$res = [ |
67
|
|
|
'type' => $this->_type, |
68
|
|
|
'id' => $this->_record->id, |
69
|
|
|
'text' => $this->_record->message, |
70
|
|
|
'date' => Date::convertToDatetime($this->_record->created_at, Date::FORMAT_TO_HOUR), |
71
|
|
|
'user' => [ |
72
|
|
|
'id' => $this->_record->user_id, |
73
|
|
|
'name' => $userName, |
74
|
|
|
'avatar' => $userAvatar |
75
|
|
|
] |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
if ($this->_type === 'post' && method_exists($this->_record, 'getAnswerCount')) { |
79
|
|
|
$res['answers'] = $this->_record->getAnswerCount(); |
80
|
|
|
} elseif ($this->_type === 'answer') { |
81
|
|
|
$res['comment_id'] = $this->_record->comment_id; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $res; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.