|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Taskforcedev\LaravelForum\Models; |
|
4
|
|
|
|
|
5
|
|
|
use \Exception; |
|
6
|
|
|
use Taskforcedev\LaravelForum\Helpers\UserHelper; |
|
7
|
|
|
use \Validator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ForumPost |
|
11
|
|
|
* |
|
12
|
|
|
* @property integer $id |
|
13
|
|
|
* @property string $title |
|
14
|
|
|
* @property string $body |
|
15
|
|
|
* @property integer $forum_id |
|
16
|
|
|
* @property integer $author_id |
|
17
|
|
|
* @property integer $sticky |
|
18
|
|
|
* @property integer $locked |
|
19
|
|
|
* |
|
20
|
|
|
* @package Taskforcedev\LaravelForum\Models |
|
21
|
|
|
*/ |
|
22
|
|
|
class ForumPost extends AbstractModel |
|
23
|
|
|
{ |
|
24
|
|
|
public $table = 'forum_posts'; |
|
25
|
|
|
|
|
26
|
|
|
public $fillable = ['title', 'body', 'forum_id', 'author_id', 'sticky', 'locked']; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Eloquent Relation. |
|
30
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
31
|
|
|
*/ |
|
32
|
|
|
public function author() |
|
33
|
|
|
{ |
|
34
|
|
|
$userHelper = new UserHelper(); |
|
35
|
|
|
$model = $userHelper->getUserModel(); |
|
36
|
|
|
return $this->belongsTo($model); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Eloquent Relation. |
|
41
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
42
|
|
|
*/ |
|
43
|
|
|
public function forum() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->belongsTo('Taskforcedev\LaravelForum\Forum'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Eloquent Relation. |
|
50
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
51
|
|
|
*/ |
|
52
|
|
|
public function replies() |
|
53
|
|
|
{ |
|
54
|
|
|
$local = 'id'; |
|
55
|
|
|
$foreign = 'post_id'; |
|
56
|
|
|
return $this->hasMany('Taskforcedev\LaravelForum\ForumReply', $foreign, $local); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Is model data valid. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array|object $data The data to validate. |
|
63
|
|
|
* |
|
64
|
|
|
* @return boolean |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function valid($data) |
|
67
|
|
|
{ |
|
68
|
|
|
$rules = [ |
|
69
|
|
|
'title' => 'required|min:3|max:100', |
|
70
|
|
|
'body' => 'required|min:5', |
|
71
|
|
|
'forum_id' => 'required|exists:forums,id', |
|
72
|
|
|
'author_id' => 'required|min:1|exists:users,id', |
|
73
|
|
|
'sticky' => 'min:1|max:1', |
|
74
|
|
|
'locked' => 'min:1|max:1', |
|
75
|
|
|
]; |
|
76
|
|
|
$validator = Validator::make($data, $rules); |
|
77
|
|
|
|
|
78
|
|
|
if ($validator->passes()) { |
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Gets the last reply for the current forum post. |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
public function lastReply() |
|
90
|
|
|
{ |
|
91
|
|
|
try { |
|
92
|
|
|
$replies = ForumReply::where('post_id', $this->id)->get(); |
|
93
|
|
|
|
|
94
|
|
|
if ($replies->isEmpty()) { |
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$reply = $replies->last(); |
|
99
|
|
|
|
|
100
|
|
|
return [ |
|
101
|
|
|
'author' => $reply->author->name, |
|
102
|
|
|
'date' => $reply->created_at |
|
103
|
|
|
]; |
|
104
|
|
|
} catch (Exception $e) { |
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
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.