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