1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ipag\Classes; |
4
|
|
|
|
5
|
|
|
use Ipag\Classes\Contracts\Emptiable; |
6
|
|
|
use Ipag\Classes\Contracts\ObjectSerializable; |
7
|
|
|
use Ipag\Classes\Traits\EmptiableTrait; |
8
|
|
|
|
9
|
|
|
final class Customer extends BaseResource implements Emptiable, ObjectSerializable |
10
|
|
|
{ |
11
|
|
|
use EmptiableTrait; |
12
|
|
|
|
13
|
|
|
const INDIVIDUAL = 'f'; |
14
|
|
|
const BUSINESS = 'j'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $type; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $taxpayerId; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $email; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $birthdate; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Phone |
43
|
|
|
*/ |
44
|
|
|
private $phone; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Address |
48
|
|
|
*/ |
49
|
|
|
private $address; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Address |
53
|
|
|
*/ |
54
|
|
|
public function getAddress() |
55
|
|
|
{ |
56
|
|
|
if (is_null($this->address)) { |
57
|
|
|
$this->address = new Address(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->address; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getName() |
67
|
|
|
{ |
68
|
|
|
return $this->name; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getType() |
75
|
|
|
{ |
76
|
|
|
return $this->type; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getTaxpayerId() |
83
|
|
|
{ |
84
|
|
|
return $this->taxpayerId; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getEmail() |
91
|
|
|
{ |
92
|
|
|
return $this->email; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getPhone() |
99
|
|
|
{ |
100
|
|
|
if (is_null($this->phone)) { |
101
|
|
|
$this->phone = new Phone(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->phone->getAreaCode().$this->phone->getNumber(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $name |
109
|
|
|
*/ |
110
|
|
|
public function setName($name) |
111
|
|
|
{ |
112
|
|
|
$this->name = substr((string) $name, 0, 50); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function setType() |
118
|
|
|
{ |
119
|
|
|
if ($this->isIndividual()) { |
120
|
|
|
$this->type = self::INDIVIDUAL; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->type = self::BUSINESS; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $taxpayerId |
132
|
|
|
*/ |
133
|
|
|
public function setTaxpayerId($taxpayerId) |
134
|
|
|
{ |
135
|
|
|
$this->taxpayerId = substr($this->getNumberUtil()->getOnlyNumbers($taxpayerId), 0, 14); |
136
|
|
|
|
137
|
|
|
$this->setType(); |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $email |
144
|
|
|
*/ |
145
|
|
|
public function setEmail($email) |
146
|
|
|
{ |
147
|
|
|
$this->email = substr((string) $email, 0, 50); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $areaCode |
154
|
|
|
* @param string $number |
155
|
|
|
*/ |
156
|
|
|
public function setPhone($areaCode, $number) |
157
|
|
|
{ |
158
|
|
|
$this->phone = new Phone(); |
159
|
|
|
$this->phone |
160
|
|
|
->setAreaCode($areaCode) |
161
|
|
|
->setNumber($number); |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param Address $address |
168
|
|
|
*/ |
169
|
|
|
public function setAddress(Address $address) |
170
|
|
|
{ |
171
|
|
|
$this->address = $address; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getBirthdate() |
180
|
|
|
{ |
181
|
|
|
return $this->birthdate; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $birthdate |
186
|
|
|
* |
187
|
|
|
* @return self |
188
|
|
|
*/ |
189
|
|
|
public function setBirthdate($birthdate) |
190
|
|
|
{ |
191
|
|
|
$this->birthdate = $birthdate; |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
private function isIndividual() |
197
|
|
|
{ |
198
|
|
|
return (bool) (strlen($this->taxpayerId) <= 11); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function serialize() |
202
|
|
|
{ |
203
|
|
|
if ($this->isEmpty()) { |
204
|
|
|
return []; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return array_merge( |
208
|
|
|
[ |
209
|
|
|
'nome' => urlencode($this->getName()), |
210
|
|
|
'email' => urlencode($this->getEmail()), |
211
|
|
|
'doc' => urlencode($this->getTaxpayerId()), |
212
|
|
|
'fone' => urlencode($this->getPhone()), |
213
|
|
|
'birthdate' => urlencode($this->getBirthdate()), |
214
|
|
|
], |
215
|
|
|
$this->getAddress()->serialize() |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|