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 LastTrait. |
18
|
|
|
* |
19
|
|
|
* @author Beñat Espiña <[email protected]> |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
trait LastTrait |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The last body. |
25
|
|
|
* |
26
|
|
|
* @var string|null |
27
|
|
|
*/ |
28
|
|
|
protected $lastBody; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Array that contains the last tags. |
32
|
|
|
* |
33
|
|
|
* @var string[] |
34
|
|
|
*/ |
35
|
|
|
protected $lastTags = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The last title. |
39
|
|
|
* |
40
|
|
|
* @var string|null |
41
|
|
|
*/ |
42
|
|
|
protected $lastTitle; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Sets last body. |
46
|
|
|
* |
47
|
|
|
* @param string|null $lastBody The last body |
48
|
|
|
* |
49
|
|
|
* @return $this self Object |
50
|
|
|
*/ |
51
|
|
|
public function setLastBody($lastBody) |
52
|
|
|
{ |
53
|
|
|
$this->lastBody = $lastBody; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Gets last body. |
60
|
|
|
* |
61
|
|
|
* @return string|null |
62
|
|
|
*/ |
63
|
|
|
public function getLastBody() |
64
|
|
|
{ |
65
|
|
|
return $this->lastBody; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Adds last tag. |
70
|
|
|
* |
71
|
|
|
* @param string|null $lastTag The last tag |
72
|
|
|
* |
73
|
|
|
* @return $this self Object |
74
|
|
|
*/ |
75
|
|
|
public function addLastTag($lastTag) |
76
|
|
|
{ |
77
|
|
|
$this->lastTags[] = $lastTag; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Removes last tag. |
84
|
|
|
* |
85
|
|
|
* @param string|null $lastTag The last tag |
86
|
|
|
* |
87
|
|
|
* @return $this self Object |
88
|
|
|
*/ |
89
|
|
|
public function removeLastTag($lastTag) |
90
|
|
|
{ |
91
|
|
|
$this->lastTags = Util::removeElement($lastTag, $this->lastTags); |
|
|
|
|
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets array of last tags. |
98
|
|
|
* |
99
|
|
|
* @return string[] |
100
|
|
|
*/ |
101
|
|
|
public function getLastTags() |
102
|
|
|
{ |
103
|
|
|
return $this->lastTags; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sets last title. |
108
|
|
|
* |
109
|
|
|
* @param string|null $lastTitle The last title |
110
|
|
|
* |
111
|
|
|
* @return $this self Object |
112
|
|
|
*/ |
113
|
|
|
public function setLastTitle($lastTitle) |
114
|
|
|
{ |
115
|
|
|
$this->lastTitle = $lastTitle; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets last title. |
122
|
|
|
* |
123
|
|
|
* @return string|null |
124
|
|
|
*/ |
125
|
|
|
public function getLastTitle() |
126
|
|
|
{ |
127
|
|
|
return $this->lastTitle; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Loads the variables if the data exist into resource. It works like a constructor. |
132
|
|
|
* |
133
|
|
|
* @param null|mixed[] $resource The resource |
134
|
|
|
*/ |
135
|
|
|
protected function loadLast($resource) |
136
|
|
|
{ |
137
|
|
|
$this->lastBody = Util::setIfStringExists($resource, 'last_body'); |
138
|
|
|
$this->lastTags = Util::setIfArrayExists($resource, 'last_tags'); |
139
|
|
|
$this->lastTitle = Util::setIfStringExists($resource, 'last_title'); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.