1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LPTracker\models; |
4
|
|
|
|
5
|
|
|
use LPTracker\exceptions\LPTrackerSDKException; |
6
|
|
|
|
7
|
|
|
class ContactField extends Model |
8
|
|
|
{ |
9
|
|
|
const TYPE_STRING = 'string'; |
10
|
|
|
const TYPE_TEXT = 'text'; |
11
|
|
|
const TYPE_DATE = 'date'; |
12
|
|
|
const TYPE_NUMBER = 'number'; |
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 string |
31
|
|
|
*/ |
32
|
|
|
protected $type; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var mixed |
36
|
|
|
*/ |
37
|
|
|
protected $value; |
38
|
|
|
|
39
|
|
View Code Duplication |
public function __construct(array $fieldData = []) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
if (isset($fieldData['id'])) { |
42
|
|
|
$this->id = $fieldData['id']; |
43
|
|
|
} |
44
|
|
|
if (isset($fieldData['contact_id'])) { |
45
|
|
|
$this->contactId = $fieldData['contact_id']; |
46
|
|
|
} |
47
|
|
|
if (isset($fieldData['name'])) { |
48
|
|
|
$this->name = $fieldData['name']; |
49
|
|
|
} |
50
|
|
|
if (isset($fieldData['type'])) { |
51
|
|
|
$this->type = $fieldData['type']; |
52
|
|
|
} |
53
|
|
|
if (isset($fieldData['value'])) { |
54
|
|
|
$this->value = $fieldData['value']; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public static function getAllTypes() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
self::TYPE_STRING, |
65
|
|
|
self::TYPE_TEXT, |
66
|
|
|
self::TYPE_DATE, |
67
|
|
|
self::TYPE_NUMBER, |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return bool |
73
|
|
|
* @throws LPTrackerSDKException |
74
|
|
|
*/ |
75
|
|
|
public function validate() |
76
|
|
|
{ |
77
|
|
|
if (empty($this->id)) { |
78
|
|
|
throw new LPTrackerSDKException('Field ID is required'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function toArray() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$result = [ |
90
|
|
|
'id' => $this->id, |
91
|
|
|
'name' => $this->name, |
92
|
|
|
'type' => $this->type, |
93
|
|
|
]; |
94
|
|
|
if (!empty($this->value)) { |
95
|
|
|
$result['value'] = $this->getValue(); |
96
|
|
|
} |
97
|
|
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function getId() |
104
|
|
|
{ |
105
|
|
|
return $this->id; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getName() |
112
|
|
|
{ |
113
|
|
|
return $this->name; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getType() |
120
|
|
|
{ |
121
|
|
|
return $this->type; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
public function getValue() |
128
|
|
|
{ |
129
|
|
|
return $this->value; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param mixed $value |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function setValue($value) |
137
|
|
|
{ |
138
|
|
|
$this->value = $value; |
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return int |
144
|
|
|
*/ |
145
|
|
|
public function getContactId() |
146
|
|
|
{ |
147
|
|
|
return $this->contactId; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.