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