1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LPTracker\models; |
4
|
|
|
|
5
|
|
|
use LPTracker\exceptions\LPTrackerSDKException; |
6
|
|
|
|
7
|
|
|
class Contact extends Model |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var integer |
11
|
|
|
*/ |
12
|
|
|
protected $id; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var integer |
16
|
|
|
*/ |
17
|
|
|
protected $projectId; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $profession; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $site; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ContactDetail[] |
36
|
|
|
*/ |
37
|
|
|
protected $details = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ContactField[] |
41
|
|
|
*/ |
42
|
|
|
protected $fields = []; |
43
|
|
|
|
44
|
|
|
public function __construct(array $contactData = []) |
45
|
|
|
{ |
46
|
|
|
if (!empty($contactData['id'])) { |
47
|
|
|
$this->id = (int) $contactData['id']; |
48
|
|
|
} |
49
|
|
|
if (!empty($contactData['project_id'])) { |
50
|
|
|
$this->projectId = (int) $contactData['project_id']; |
51
|
|
|
} |
52
|
|
|
if (!empty($contactData['name'])) { |
53
|
|
|
$this->name = $contactData['name']; |
54
|
|
|
} |
55
|
|
|
if (!empty($contactData['profession'])) { |
56
|
|
|
$this->profession = $contactData['profession']; |
57
|
|
|
} |
58
|
|
|
if (!empty($contactData['site'])) { |
59
|
|
|
$this->site = $contactData['site']; |
60
|
|
|
} |
61
|
|
View Code Duplication |
if (!empty($contactData['details']) && is_array($contactData['details'])) { |
|
|
|
|
62
|
|
|
foreach ($contactData['details'] as $detail) { |
63
|
|
|
$detail['contact_id'] = $this->id; |
64
|
|
|
$detailModel = new ContactDetail($detail); |
65
|
|
|
$this->addDetail($detailModel); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
View Code Duplication |
if (!empty($contactData['fields']) && is_array($contactData['fields'])) { |
|
|
|
|
69
|
|
|
foreach ($contactData['fields'] as $fieldData) { |
70
|
|
|
$fieldData['contact_id'] = $this->id; |
71
|
|
|
$fieldModel = new ContactField($fieldData); |
72
|
|
|
$this->addField($fieldModel); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool |
79
|
|
|
* @throws LPTrackerSDKException |
80
|
|
|
*/ |
81
|
|
|
public function validate() |
82
|
|
|
{ |
83
|
|
|
if (empty($this->id) && empty($this->projectId)) { |
84
|
|
|
throw new LPTrackerSDKException('Project ID or Contact ID is required'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (empty($this->details)) { |
88
|
|
|
throw new LPTrackerSDKException('The contact does not have valid detail'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
foreach ($this->details as $detail) { |
92
|
|
|
if (!$detail->validate()) { |
93
|
|
|
throw new LPTrackerSDKException('The contact does not have valid detail'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function toArray() |
103
|
|
|
{ |
104
|
|
|
$result = [ |
105
|
|
|
'project_id' => $this->getProjectId(), |
106
|
|
|
'details' => [], |
107
|
|
|
]; |
108
|
|
|
if (!empty($this->id)) { |
109
|
|
|
$result['id'] = $this->getId(); |
110
|
|
|
} |
111
|
|
|
if (!empty($this->name)) { |
112
|
|
|
$result['name'] = $this->getName(); |
113
|
|
|
} |
114
|
|
|
if (!empty($this->profession)) { |
115
|
|
|
$result['profession'] = $this->getProfession(); |
116
|
|
|
} |
117
|
|
|
if (!empty($this->site)) { |
118
|
|
|
$result['site'] = $this->getSite(); |
119
|
|
|
} |
120
|
|
|
foreach ($this->getDetails() as $detail) { |
121
|
|
|
$result['details'][] = $detail->toArray(); |
122
|
|
|
} |
123
|
|
|
foreach ($this->getFields() as $field) { |
124
|
|
|
$result['fields'][$field->getId()] = $field->getValue(); |
125
|
|
|
} |
126
|
|
|
return $result; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return int |
131
|
|
|
*/ |
132
|
|
|
public function getId() |
133
|
|
|
{ |
134
|
|
|
return (int) $this->id; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
|
|
public function getProjectId() |
141
|
|
|
{ |
142
|
|
|
return (int) $this->projectId; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function getName() |
149
|
|
|
{ |
150
|
|
|
return $this->name; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $name |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function setName($name) |
158
|
|
|
{ |
159
|
|
|
$this->name = $name; |
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function getProfession() |
167
|
|
|
{ |
168
|
|
|
return $this->profession; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $profession |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function setProfession($profession) |
176
|
|
|
{ |
177
|
|
|
$this->profession = $profession; |
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getSite() |
185
|
|
|
{ |
186
|
|
|
return $this->site; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $site |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
|
|
public function setSite($site) |
194
|
|
|
{ |
195
|
|
|
$this->site = $site; |
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return ContactDetail[] |
201
|
|
|
*/ |
202
|
|
|
public function getDetails() |
203
|
|
|
{ |
204
|
|
|
return $this->details; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param ContactDetail[] $details |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
|
|
public function setDetails(array $details) |
212
|
|
|
{ |
213
|
|
|
foreach ($details as $detail) { |
214
|
|
|
$detail->validate(); |
215
|
|
|
} |
216
|
|
|
$this->details = $details; |
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param ContactDetail $detail |
222
|
|
|
* @return $this |
223
|
|
|
*/ |
224
|
|
|
public function addDetail(ContactDetail $detail) |
225
|
|
|
{ |
226
|
|
|
$detail->validate(); |
227
|
|
|
$this->details[] = $detail; |
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return ContactField[] |
233
|
|
|
*/ |
234
|
|
|
public function getFields() |
235
|
|
|
{ |
236
|
|
|
return $this->fields; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param ContactField[] $fields |
241
|
|
|
* @return $this |
242
|
|
|
*/ |
243
|
|
|
public function setFields(array $fields) |
244
|
|
|
{ |
245
|
|
|
foreach ($fields as $field) { |
246
|
|
|
$field->validate(); |
247
|
|
|
} |
248
|
|
|
$this->fields = $fields; |
249
|
|
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param ContactField $field |
254
|
|
|
* @return $this |
255
|
|
|
*/ |
256
|
|
|
public function addField(ContactField $field) |
257
|
|
|
{ |
258
|
|
|
$field->validate(); |
259
|
|
|
$this->fields[] = $field; |
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
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.