1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LPTracker\models; |
4
|
|
|
|
5
|
|
|
use LPTracker\exceptions\LPTrackerSDKException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ContactDetail |
9
|
|
|
* @package LPTracker\models |
10
|
|
|
*/ |
11
|
|
|
class ContactDetail extends Model |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
const TYPE_PHONE = 'phone'; |
15
|
|
|
|
16
|
|
|
const TYPE_EMAIL = 'email'; |
17
|
|
|
|
18
|
|
|
const TYPE_SKYPE = 'skype'; |
19
|
|
|
|
20
|
|
|
const TYPE_ICQ = 'icq'; |
21
|
|
|
|
22
|
|
|
const TYPE_FACEBOOK = 'facebook'; |
23
|
|
|
|
24
|
|
|
const TYPE_VK = 'vk'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var integer |
28
|
|
|
*/ |
29
|
|
|
protected $id; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var integer |
33
|
|
|
*/ |
34
|
|
|
protected $contactId; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $type; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $data; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* ContactDetail constructor. |
49
|
|
|
* |
50
|
|
|
* @param array $detailData |
51
|
|
|
*/ |
52
|
|
|
public function __construct(array $detailData = []) |
53
|
|
|
{ |
54
|
|
|
if (isset($detailData['id'])) { |
55
|
|
|
$this->id = $detailData['id']; |
56
|
|
|
} |
57
|
|
|
if (isset($detailData['contact_id'])) { |
58
|
|
|
$this->contactId = $detailData['contact_id']; |
59
|
|
|
} |
60
|
|
|
if (isset($detailData['type'])) { |
61
|
|
|
$this->type = $detailData['type']; |
62
|
|
|
} |
63
|
|
|
if (isset($detailData['data'])) { |
64
|
|
|
$this->data = $detailData['data']; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
* @throws LPTrackerSDKException |
72
|
|
|
*/ |
73
|
|
|
public function validate() |
74
|
|
|
{ |
75
|
|
|
if (empty($this->type)) { |
76
|
|
|
throw new LPTrackerSDKException('Detail type can not be null: '.$this->__toString()); |
77
|
|
|
} |
78
|
|
|
if ( ! in_array($this->type, self::getAllTypes())) { |
79
|
|
|
throw new LPTrackerSDKException('Detail type not in ('.implode(',', |
80
|
|
|
self::getAllTypes()).'): '.$this->__toString()); |
81
|
|
|
} |
82
|
|
|
if (empty($this->data)) { |
83
|
|
|
throw new LPTrackerSDKException('Detail data can not be null: '.$this->__toString()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public static function getAllTypes() |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
self::TYPE_PHONE, |
97
|
|
|
self::TYPE_EMAIL, |
98
|
|
|
self::TYPE_SKYPE, |
99
|
|
|
self::TYPE_ICQ, |
100
|
|
|
self::TYPE_FACEBOOK, |
101
|
|
|
self::TYPE_VK |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function toArray() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$result = [ |
112
|
|
|
'type' => $this->type, |
113
|
|
|
'data' => $this->data |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
if ( ! empty($this->id)) { |
117
|
|
|
$result['id'] = $this->getId(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $result; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
|
|
public function getId() |
128
|
|
|
{ |
129
|
|
|
return $this->id; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
|
|
public function getContactId() |
137
|
|
|
{ |
138
|
|
|
return $this->contactId; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getType() |
146
|
|
|
{ |
147
|
|
|
return $this->type; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $type |
153
|
|
|
* |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function setType($type) |
157
|
|
|
{ |
158
|
|
|
$this->type = $type; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
public function getData() |
168
|
|
|
{ |
169
|
|
|
return $this->data; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $data |
175
|
|
|
* |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function setData($data) |
179
|
|
|
{ |
180
|
|
|
$this->data = $data; |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
} |
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.