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\Util\Util; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait GenericIdTrait. |
18
|
|
|
* |
19
|
|
|
* @author Beñat Espiña <[email protected]> |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
trait GenericIdTrait |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The answer id. |
25
|
|
|
* |
26
|
|
|
* @var int|null |
27
|
|
|
*/ |
28
|
|
|
protected $answerId; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The body. |
32
|
|
|
* |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected $body; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The question id. |
39
|
|
|
* |
40
|
|
|
* @var int|null |
41
|
|
|
*/ |
42
|
|
|
protected $questionId; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Sets the answer id. |
46
|
|
|
* |
47
|
|
|
* @param int $answerId The answer id |
48
|
|
|
* |
49
|
|
|
* @return $this self Object |
50
|
|
|
*/ |
51
|
|
|
public function setAnswerId($answerId) |
52
|
|
|
{ |
53
|
|
|
$this->answerId = $answerId; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Gets the answer id. |
60
|
|
|
* |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
|
|
public function getAnswerId() |
64
|
|
|
{ |
65
|
|
|
return $this->answerId; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Sets body. |
70
|
|
|
* |
71
|
|
|
* @param string|null $body The body |
72
|
|
|
* |
73
|
|
|
* @return $this self Object |
74
|
|
|
*/ |
75
|
|
|
public function setBody($body) |
76
|
|
|
{ |
77
|
|
|
$this->body = $body; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets body. |
84
|
|
|
* |
85
|
|
|
* @return string|null |
86
|
|
|
*/ |
87
|
|
|
public function getBody() |
88
|
|
|
{ |
89
|
|
|
return $this->body; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sets the question id. |
94
|
|
|
* |
95
|
|
|
* @param int|null $questionId The question id |
96
|
|
|
* |
97
|
|
|
* @return $this self Object |
98
|
|
|
*/ |
99
|
|
|
public function setQuestionId($questionId) |
100
|
|
|
{ |
101
|
|
|
$this->questionId = $questionId; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Gets the question id. |
108
|
|
|
* |
109
|
|
|
* @return int|null |
110
|
|
|
*/ |
111
|
|
|
public function getQuestionId() |
112
|
|
|
{ |
113
|
|
|
return $this->questionId; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Loads the variables if the data exist into resource. It works like a constructor. |
118
|
|
|
* |
119
|
|
|
* @param null|mixed[] $resource The resource |
120
|
|
|
*/ |
121
|
|
|
protected function loadGenericId($resource) |
122
|
|
|
{ |
123
|
|
|
$this->answerId = Util::setIfIntegerExists($resource, 'answer_id'); |
124
|
|
|
$this->body = Util::setIfStringExists($resource, 'body'); |
125
|
|
|
$this->questionId = Util::setIfIntegerExists($resource, 'question_id'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.