1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LPTracker\models; |
4
|
|
|
|
5
|
|
|
use LPTracker\exceptions\LPTrackerSDKException; |
6
|
|
|
|
7
|
|
|
class Lead extends Model |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var integer |
11
|
|
|
*/ |
12
|
|
|
protected $id; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var integer |
16
|
|
|
*/ |
17
|
|
|
protected $contactId; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Contact |
21
|
|
|
*/ |
22
|
|
|
protected $contact; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $name; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var integer |
31
|
|
|
*/ |
32
|
|
|
protected $funnelId; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var integer |
36
|
|
|
*/ |
37
|
|
|
protected $viewId; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var View |
41
|
|
|
*/ |
42
|
|
|
protected $view; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var integer |
46
|
|
|
*/ |
47
|
|
|
protected $ownerId; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var \DateTime |
51
|
|
|
*/ |
52
|
|
|
protected $createdAt; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Payment[] |
56
|
|
|
*/ |
57
|
|
|
protected $payments = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Custom[] |
61
|
|
|
*/ |
62
|
|
|
protected $customs = []; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $options = []; |
68
|
|
|
|
69
|
|
|
public function __construct(array $leadData = []) |
70
|
|
|
{ |
71
|
|
|
if (!empty($leadData['id'])) { |
72
|
|
|
$this->id = (int) $leadData['id']; |
73
|
|
|
} |
74
|
|
|
if (!empty($leadData['contact_id'])) { |
75
|
|
|
$this->contactId = (int) $leadData['contact_id']; |
76
|
|
|
} |
77
|
|
|
if (!empty($leadData['contact'])) { |
78
|
|
|
$this->contact = new Contact($leadData['contact']); |
79
|
|
|
} |
80
|
|
|
if (!empty($leadData['name'])) { |
81
|
|
|
$this->name = $leadData['name']; |
82
|
|
|
} |
83
|
|
|
if (!empty($leadData['funnel'])) { |
84
|
|
|
$this->funnelId = (int) $leadData['funnel']; |
85
|
|
|
} |
86
|
|
|
if (!empty($leadData['stage_id'])) { |
87
|
|
|
$this->funnelId = (int) $leadData['stage_id']; |
88
|
|
|
} |
89
|
|
|
if (!empty($leadData['view_id'])) { |
90
|
|
|
$this->viewId = (int) $leadData['view_id']; |
91
|
|
|
} |
92
|
|
|
if (!empty($leadData['view'])) { |
93
|
|
|
$this->view = new View($leadData['view']); |
94
|
|
|
} |
95
|
|
|
if (isset($leadData['owner'])) { |
96
|
|
|
$this->ownerId = (int) $leadData['owner']; |
97
|
|
|
} |
98
|
|
|
if (isset($leadData['owner_id'])) { |
99
|
|
|
$this->ownerId = (int) $leadData['owner_id']; |
100
|
|
|
} |
101
|
|
|
if (!empty($leadData['payments']) && is_array($leadData['payments'])) { |
102
|
|
|
foreach ($leadData['payments'] as $paymentData) { |
103
|
|
|
$paymentModel = new Payment($paymentData); |
104
|
|
|
$this->addPayment($paymentModel); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
if (!empty($leadData['custom']) && is_array($leadData['custom'])) { |
108
|
|
|
foreach ($leadData['custom'] as $customData) { |
109
|
|
|
$customModel = new Custom($customData, $this->id); |
110
|
|
|
$this->addCustom($customModel); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
View Code Duplication |
if (!empty($leadData['lead_date'])) { |
|
|
|
|
114
|
|
|
$date = \DateTime::createFromFormat('d.m.Y H:i', $leadData['lead_date']); |
115
|
|
|
$this->setCreatedAt($date); |
|
|
|
|
116
|
|
|
} |
117
|
|
View Code Duplication |
if (!empty($leadData['created_at'])) { |
|
|
|
|
118
|
|
|
$date = \DateTime::createFromFormat('d.m.Y H:i', $leadData['created_at']); |
119
|
|
|
$this->setCreatedAt($date); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
if (!empty($leadData['deal_date'])) { |
122
|
|
|
$this->options['deal_date'] = $leadData['deal_date']; |
123
|
|
|
} |
124
|
|
|
if (!empty($leadData['params']) && is_array($leadData['params'])) { |
125
|
|
|
$this->options['params'] = $leadData['params']; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param bool $toSave |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function toArray($toSave = false) |
134
|
|
|
{ |
135
|
|
|
$result = []; |
136
|
|
|
if (!empty($this->contactId)) { |
137
|
|
|
$result['contact_id'] = $this->contactId; |
138
|
|
|
} |
139
|
|
View Code Duplication |
if (!empty($this->contact)) { |
|
|
|
|
140
|
|
|
if ($toSave) { |
141
|
|
|
if (!empty($this->contact->getId())) { |
142
|
|
|
$result['contact_id'] = $this->contact->getId(); |
143
|
|
|
} else { |
144
|
|
|
$result['contact'] = $this->contact->toArray(); |
145
|
|
|
} |
146
|
|
|
} else { |
147
|
|
|
$result['contact'] = $this->contact->toArray(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
if (!empty($this->id)) { |
151
|
|
|
$result['id'] = $this->getId(); |
152
|
|
|
} |
153
|
|
|
if (!empty($this->name)) { |
154
|
|
|
$result['name'] = $this->getName(); |
155
|
|
|
} |
156
|
|
|
if (!empty($this->funnelId)) { |
157
|
|
|
$result['funnel'] = $this->getFunnelId(); |
158
|
|
|
$result['stage_id'] = $this->getFunnelId(); |
159
|
|
|
} |
160
|
|
|
if (!empty($this->ownerId)) { |
161
|
|
|
$result['owner'] = $this->getOwnerId(); |
162
|
|
|
$result['owner_id'] = $this->getOwnerId(); |
163
|
|
|
} |
164
|
|
|
if (!empty($this->createdAt)) { |
165
|
|
|
$result['lead_date'] = $this->getCreatedAt()->format('d.m.Y H:i'); |
166
|
|
|
} |
167
|
|
|
if (!empty($this->viewId)) { |
168
|
|
|
$result['view_id'] = $this->viewId; |
169
|
|
|
} |
170
|
|
View Code Duplication |
if (!empty($this->view)) { |
|
|
|
|
171
|
|
|
if ($toSave) { |
172
|
|
|
if (!empty($this->view->getId())) { |
173
|
|
|
$result['view_id'] = $this->view->getId(); |
174
|
|
|
} else { |
175
|
|
|
$result['view'] = $this->view->toArray(); |
176
|
|
|
} |
177
|
|
|
} else { |
178
|
|
|
$result['view'] = $this->view->toArray(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
foreach ($this->getPayments() as $payment) { |
182
|
|
|
$result['payments'][] = $payment->toArray(); |
183
|
|
|
} |
184
|
|
|
foreach ($this->getCustoms() as $custom) { |
185
|
|
|
if ($toSave) { |
186
|
|
|
$result['custom'][$custom->getId()] = $custom->getValue(); |
187
|
|
|
} else { |
188
|
|
|
$result['custom'][] = $custom->toArray(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
foreach ($this->options as $key => $value) { |
192
|
|
|
$result[$key] = $value; |
193
|
|
|
} |
194
|
|
|
return $result; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return bool |
199
|
|
|
* @throws LPTrackerSDKException |
200
|
|
|
*/ |
201
|
|
|
public function validate() |
202
|
|
|
{ |
203
|
|
|
if (empty($this->contactId) && empty($this->contact)) { |
204
|
|
|
throw new LPTrackerSDKException('Contact ID or contact is required'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if ((int) $this->contactId < 0) { |
208
|
|
|
throw new LPTrackerSDKException('Invalid contact ID'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (!empty($this->funnelId) && (int) $this->funnelId <= 0) { |
212
|
|
|
throw new LPTrackerSDKException('Invalid funnel ID'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if (!empty($this->ownerId) && (int) $this->ownerId < 0) { |
216
|
|
|
throw new LPTrackerSDKException('Invalid owner ID'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
foreach ($this->getPayments() as $payment) { |
220
|
|
|
$payment->validate(); |
221
|
|
|
} |
222
|
|
|
return true; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return int |
227
|
|
|
*/ |
228
|
|
|
public function getId() |
229
|
|
|
{ |
230
|
|
|
return (int) $this->id; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return int |
235
|
|
|
*/ |
236
|
|
|
public function getContactId() |
237
|
|
|
{ |
238
|
|
|
return (int) $this->contactId; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return Contact |
243
|
|
|
*/ |
244
|
|
|
public function getContact() |
245
|
|
|
{ |
246
|
|
|
return $this->contact; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return string |
251
|
|
|
*/ |
252
|
|
|
public function getName() |
253
|
|
|
{ |
254
|
|
|
return $this->name; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param string $name |
259
|
|
|
* @return $this |
260
|
|
|
*/ |
261
|
|
|
public function setName($name) |
262
|
|
|
{ |
263
|
|
|
$this->name = $name; |
264
|
|
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return int |
269
|
|
|
*/ |
270
|
|
|
public function getFunnelId() |
271
|
|
|
{ |
272
|
|
|
return $this->funnelId; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param int $funnelId |
277
|
|
|
* @return $this |
278
|
|
|
*/ |
279
|
|
|
public function setFunnelId($funnelId) |
280
|
|
|
{ |
281
|
|
|
$this->funnelId = (int) $funnelId; |
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return int |
287
|
|
|
*/ |
288
|
|
|
public function getViewId() |
289
|
|
|
{ |
290
|
|
|
return (int) $this->viewId; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @return View |
295
|
|
|
*/ |
296
|
|
|
public function getView() |
297
|
|
|
{ |
298
|
|
|
return $this->view; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param View $view |
303
|
|
|
* @return $this |
304
|
|
|
*/ |
305
|
|
|
public function setView(View $view) |
306
|
|
|
{ |
307
|
|
|
$this->view = $view; |
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return Payment[] |
313
|
|
|
*/ |
314
|
|
|
public function getPayments() |
315
|
|
|
{ |
316
|
|
|
return $this->payments; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param array $payments |
321
|
|
|
* @return $this |
322
|
|
|
*/ |
323
|
|
|
public function setPayments(array $payments) |
324
|
|
|
{ |
325
|
|
|
$this->payments = $payments; |
326
|
|
|
return $this; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param Payment $payment |
331
|
|
|
* @return $this |
332
|
|
|
*/ |
333
|
|
|
public function addPayment(Payment $payment) |
334
|
|
|
{ |
335
|
|
|
$this->payments[] = $payment; |
336
|
|
|
return $this; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @return int |
341
|
|
|
*/ |
342
|
|
|
public function getOwnerId() |
343
|
|
|
{ |
344
|
|
|
return $this->ownerId; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param int $ownerId |
349
|
|
|
* @return $this |
350
|
|
|
*/ |
351
|
|
|
public function setOwnerId($ownerId) |
352
|
|
|
{ |
353
|
|
|
$this->ownerId = (int) $ownerId; |
354
|
|
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @return Custom[] |
359
|
|
|
*/ |
360
|
|
|
public function getCustoms() |
361
|
|
|
{ |
362
|
|
|
return $this->customs; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @param Custom[] $customs |
367
|
|
|
* @return $this |
368
|
|
|
*/ |
369
|
|
|
public function setCustoms(array $customs) |
370
|
|
|
{ |
371
|
|
|
$this->customs = $customs; |
372
|
|
|
return $this; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @param Custom $custom |
377
|
|
|
* @return $this |
378
|
|
|
*/ |
379
|
|
|
public function addCustom(Custom $custom) |
380
|
|
|
{ |
381
|
|
|
$this->customs[] = $custom; |
382
|
|
|
return $this; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @return \DateTime |
387
|
|
|
*/ |
388
|
|
|
public function getCreatedAt() |
389
|
|
|
{ |
390
|
|
|
return $this->createdAt; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @param \DateTime $createdAt |
395
|
|
|
* @return $this |
396
|
|
|
*/ |
397
|
|
|
public function setCreatedAt($createdAt) |
398
|
|
|
{ |
399
|
|
|
$this->createdAt = $createdAt; |
400
|
|
|
return $this; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
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.