1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stack Exchange Api Client library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2016 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; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class badge model class. |
16
|
|
|
* |
17
|
|
|
* @author Beñat Espiña <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Badge implements Model |
20
|
|
|
{ |
21
|
|
|
const BADGE_TYPES = ['named', 'tag_based']; |
22
|
|
|
const RANKS = ['gold', 'silver', 'bronze']; |
23
|
|
|
|
24
|
|
|
protected $id; |
25
|
|
|
protected $awardCount; |
26
|
|
|
protected $badgeType; |
27
|
|
|
protected $description; |
28
|
|
|
protected $link; |
29
|
|
|
protected $name; |
30
|
|
|
protected $rank; |
31
|
|
|
protected $user; |
32
|
|
|
|
33
|
|
View Code Duplication |
public static function fromJson(array $data) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$instance = new self(); |
36
|
|
|
$instance |
37
|
|
|
->setId(array_key_exists('badge_id', $data) ? $data['badge_id'] : null) |
38
|
|
|
->setAwardCount(array_key_exists('award_count', $data) ? $data['award_count'] : null) |
39
|
|
|
->setBadgeType(array_key_exists('badge_type', $data) ? $data['badge_type'] : null) |
40
|
|
|
->setLink(array_key_exists('link', $data) ? $data['link'] : null) |
41
|
|
|
->setName(array_key_exists('name', $data) ? $data['name'] : null) |
42
|
|
|
->setRank(array_key_exists('rank', $data) ? $data['rank'] : null) |
43
|
|
|
->setUser(array_key_exists('user', $data) ? ShallowUser::fromJson($data['user']) : null) |
|
|
|
|
44
|
|
|
->setDescription(array_key_exists('description', $data) ? $data['description'] : null); |
45
|
|
|
|
46
|
|
|
return $instance; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function fromProperties( |
50
|
|
|
$id, |
51
|
|
|
$awardCount, |
52
|
|
|
$badgeType, |
53
|
|
|
$link, |
54
|
|
|
$name, |
55
|
|
|
$rank, |
56
|
|
|
$user, |
57
|
|
|
$description = null |
58
|
|
|
) { |
59
|
|
|
$instance = new self(); |
60
|
|
|
$instance |
61
|
|
|
->setId($id) |
62
|
|
|
->setAwardCount($awardCount) |
63
|
|
|
->setBadgeType($badgeType) |
64
|
|
|
->setLink($link) |
65
|
|
|
->setName($name) |
66
|
|
|
->setRank($rank) |
67
|
|
|
->setUser($user) |
68
|
|
|
->setDescription($description); |
69
|
|
|
|
70
|
|
|
return $instance; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setId($id) |
74
|
|
|
{ |
75
|
|
|
$this->id = $id; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getId() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
return $this->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setAwardCount($awardCount) |
86
|
|
|
{ |
87
|
|
|
$this->awardCount = $awardCount; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getAwardCount() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
return $this->awardCount; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setBadgeType($badgeType) |
98
|
|
|
{ |
99
|
|
|
if (in_array($badgeType, self::BADGE_TYPES, true)) { |
100
|
|
|
$this->badgeType = $badgeType; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getBadgeType() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
return $this->badgeType; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function setDescription($description) |
112
|
|
|
{ |
113
|
|
|
$this->description = $description; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getDescription() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
return $this->description; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function setLink($link) |
124
|
|
|
{ |
125
|
|
|
$this->link = $link; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getLink() |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
return $this->link; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function setName($name) |
136
|
|
|
{ |
137
|
|
|
$this->name = $name; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getName() |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
return $this->name; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function setRank($rank) |
148
|
|
|
{ |
149
|
|
|
if (in_array($rank, self::RANKS, true)) { |
150
|
|
|
$this->rank = $rank; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function getRank() |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
return $this->rank; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function setUser(ShallowUser $user = null) |
162
|
|
|
{ |
163
|
|
|
$this->user = $user; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function getUser() |
169
|
|
|
{ |
170
|
|
|
return $this->user; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
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.