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