1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stack Exchange Api Client library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015 Beñat Espiña <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BenatEspina\StackExchangeApiClient\Model\Traits; |
13
|
|
|
|
14
|
|
|
use BenatEspina\StackExchangeApiClient\Model\Comment; |
15
|
|
|
use BenatEspina\StackExchangeApiClient\Model\Interfaces\CommentInterface; |
16
|
|
|
use BenatEspina\StackExchangeApiClient\Util\Util; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class CommentCountTrait. |
20
|
|
|
* |
21
|
|
|
* @author Beñat Espiña <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
trait CommentCountTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Number of comments. |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $commentCount; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* An array of comments. |
34
|
|
|
* |
35
|
|
|
* @var array<\BenatEspina\StackExchangeApiClient\Model\Interfaces\CommentInterface>|null |
36
|
|
|
*/ |
37
|
|
|
protected $comments = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sets number of comments. |
41
|
|
|
* |
42
|
|
|
* @param int $commentCount The number of comments |
43
|
|
|
* |
44
|
|
|
* @return $this self Object |
45
|
|
|
*/ |
46
|
|
|
public function setCommentCount($commentCount) |
47
|
|
|
{ |
48
|
|
|
$this->commentCount = $commentCount; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets number of comments. |
55
|
|
|
* |
56
|
|
|
* @return int |
57
|
|
|
*/ |
58
|
|
|
public function getCommentCount() |
59
|
|
|
{ |
60
|
|
|
return $this->commentCount; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Adds comment. |
65
|
|
|
* |
66
|
|
|
* @param \BenatEspina\StackExchangeApiClient\Model\Interfaces\CommentInterface|null $comment The comment object |
|
|
|
|
67
|
|
|
* |
68
|
|
|
* @return $this self Object |
69
|
|
|
*/ |
70
|
|
|
public function addComment(CommentInterface $comment) |
71
|
|
|
{ |
72
|
|
|
$this->comments[] = $comment; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Removes comment. |
79
|
|
|
* |
80
|
|
|
* @param \BenatEspina\StackExchangeApiClient\Model\Interfaces\CommentInterface|null $comment The comment object |
|
|
|
|
81
|
|
|
* |
82
|
|
|
* @return $this self Object |
83
|
|
|
*/ |
84
|
|
|
public function removeComment(CommentInterface $comment) |
85
|
|
|
{ |
86
|
|
|
$this->comments = Util::removeElement($comment, $this->comments); |
|
|
|
|
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Gets array of comments. |
93
|
|
|
* |
94
|
|
|
* @return array<\BenatEspina\StackExchangeApiClient\Model\Interfaces\CommentInterface>|null |
95
|
|
|
*/ |
96
|
|
|
public function getComments() |
97
|
|
|
{ |
98
|
|
|
return $this->comments; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Loads the variables if the data exist into resource. It works like a constructor. |
103
|
|
|
* |
104
|
|
|
* @param null|mixed[] $resource The resource |
105
|
|
|
*/ |
106
|
|
|
protected function loadCommentCount($resource) |
107
|
|
|
{ |
108
|
|
|
$this->commentCount = Util::setIfIntegerExists($resource, 'comment_count'); |
109
|
|
|
$comments = Util::setIfArrayExists($resource, 'comments'); |
110
|
|
|
foreach ($comments as $comment) { |
111
|
|
|
$this->comments[] = new Comment($comment); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|