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