1
+ <?php
2
+
3
+ namespace Moip \Resource ;
4
+
5
+ use stdClass ;
6
+ use UnexpectedValueException ;
7
+
8
+ /**
9
+ * Class Holder.
10
+ */
11
+ class Holder extends MoipResource {
12
+
13
+ /**
14
+ * Address Type.
15
+ *
16
+ * @const string
17
+ */
18
+ const ADDRESS_BILLING = 'BILLING ' ;
19
+
20
+ /**
21
+ * Standard country .
22
+ *
23
+ * @const string
24
+ */
25
+ const ADDRESS_COUNTRY = 'BRA ' ;
26
+
27
+ /**
28
+ * Standard document type.
29
+ *
30
+ * @const string
31
+ */
32
+ const TAX_DOCUMENT = 'CPF ' ;
33
+
34
+ /**
35
+ * Initialize a new instance.
36
+ */
37
+ public function initialize ()
38
+ {
39
+ $ this ->data = new stdClass ();
40
+ }
41
+
42
+ /**
43
+ * Add a new address to the holder.
44
+ *
45
+ * @param string $type Address type: BILLING.
46
+ * @param string $street Street address.
47
+ * @param string $number Number address.
48
+ * @param string $district Neighborhood address.
49
+ * @param string $city City address.
50
+ * @param string $state State address.
51
+ * @param string $zip The zip code billing address.
52
+ * @param string $complement Address complement.
53
+ * @param string $country Country ISO-alpha3 format, BRA example.
54
+ *
55
+ * @return $this
56
+ */
57
+ public function setAddress ($ type = self ::ADDRESS_BILLING , $ street , $ number , $ district , $ city , $ state , $ zip , $ complement = null , $ country = self ::ADDRESS_COUNTRY )
58
+ {
59
+ $ address = new stdClass ();
60
+ $ address ->street = $ street ;
61
+ $ address ->streetNumber = $ number ;
62
+ $ address ->complement = $ complement ;
63
+ $ address ->district = $ district ;
64
+ $ address ->city = $ city ;
65
+ $ address ->state = $ state ;
66
+ $ address ->country = $ country ;
67
+ $ address ->zipCode = $ zip ;
68
+
69
+ $ this ->data ->billingAddress = $ address ;
70
+
71
+ return $ this ;
72
+ }
73
+
74
+ /**
75
+ * Get holder address.
76
+ *
77
+ * @return \stdClass Holder's address.
78
+ */
79
+ public function getBillingAddress ()
80
+ {
81
+ return $ this ->getIfSet ('billingAddress ' );
82
+ }
83
+
84
+ /**
85
+ * Get holser fullname.
86
+ *
87
+ * @return string Holder's full name.
88
+ */
89
+ public function getFullname ()
90
+ {
91
+ return $ this ->getIfSet ('fullname ' );
92
+ }
93
+
94
+ /**
95
+ * Get birth date from holder.
96
+ *
97
+ * @return \DateTime|null Date of birth of the credit card holder.
98
+ */
99
+ public function getBirthDate ()
100
+ {
101
+ return $ this ->getIfSetDate ('birthDate ' );
102
+ }
103
+
104
+ /**
105
+ * Get phone area code from holder.
106
+ *
107
+ * @return int DDD telephone.
108
+ */
109
+ public function getPhoneAreaCode ()
110
+ {
111
+ return $ this ->getIfSet ('areaCode ' , $ this ->data ->phone );
112
+ }
113
+
114
+ /**
115
+ * Get phone country code from holder.
116
+ *
117
+ * @return int Country code.
118
+ */
119
+ public function getPhoneCountryCode ()
120
+ {
121
+ return $ this ->getIfSet ('countryCode ' , $ this ->data ->phone );
122
+ }
123
+
124
+ /**
125
+ * Get phone number from holder.
126
+ *
127
+ * @return int Telephone number.
128
+ */
129
+ public function getPhoneNumber ()
130
+ {
131
+ return $ this ->getIfSet ('number ' , $ this ->data ->phone );
132
+ }
133
+
134
+ /**
135
+ * Get tax document type from holder.
136
+ *
137
+ * @return string Type of value: CPF and CNPJ
138
+ */
139
+ public function getTaxDocumentType ()
140
+ {
141
+ return $ this ->getIfSet ('type ' , $ this ->data ->taxDocument );
142
+ }
143
+
144
+ /**
145
+ * Get tax document number from holder.
146
+ *
147
+ * @return string Document Number.
148
+ */
149
+ public function getTaxDocumentNumber ()
150
+ {
151
+ return $ this ->getIfSet ('number ' , $ this ->data ->taxDocument );
152
+ }
153
+
154
+ /**
155
+ * Mount the buyer structure from holder.
156
+ *
157
+ * @param \stdClass $response
158
+ *
159
+ * @return Holder information.
160
+ */
161
+ protected function populate (stdClass $ response )
162
+ {
163
+ $ holder = clone $ this ;
164
+ $ holder ->data = new stdClass ();
165
+ $ holder ->data ->fullname = $ this ->getIfSet ('fullname ' , $ response );
166
+ $ holder ->data ->phone = new stdClass ();
167
+
168
+ $ phone = $ this ->getIfSet ('phone ' , $ response );
169
+
170
+ $ holder ->data ->phone ->countryCode = $ this ->getIfSet ('countryCode ' , $ phone );
171
+ $ holder ->data ->phone ->areaCode = $ this ->getIfSet ('areaCode ' , $ phone );
172
+ $ holder ->data ->phone ->number = $ this ->getIfSet ('number ' , $ phone );
173
+ $ holder ->data ->birthDate = $ this ->getIfSet ('birthDate ' , $ response );
174
+ $ holder ->data ->taxDocument = new stdClass ();
175
+ $ holder ->data ->taxDocument ->type = $ this ->getIfSet ('type ' , $ this ->getIfSet ('taxDocument ' , $ response ));
176
+ $ holder ->data ->taxDocument ->number = $ this ->getIfSet ('number ' , $ this ->getIfSet ('taxDocument ' , $ response ));
177
+ //$holder->data->addresses = [];
178
+ $ holder ->data ->billingAddress = $ this ->getIfSet ('billingAddress ' , $ response );
179
+
180
+ return $ holder ;
181
+ }
182
+
183
+ /**
184
+ * Set fullname from holder.
185
+ *
186
+ * @param string $fullname Holder's full name.
187
+ *
188
+ * @return $this
189
+ */
190
+ public function setFullname ($ fullname )
191
+ {
192
+ $ this ->data ->fullname = $ fullname ;
193
+
194
+ return $ this ;
195
+ }
196
+
197
+ /**
198
+ * Set birth date from holder.
199
+ *
200
+ * @param \DateTime|string $birthDate Date of birth of the credit card holder.
201
+ *
202
+ * @return $this
203
+ */
204
+ public function setBirthDate ($ birthDate )
205
+ {
206
+ if ($ birthDate instanceof \DateTime) {
207
+ $ birthDate = $ birthDate ->format ('Y-m-d ' );
208
+ }
209
+
210
+ $ this ->data ->birthDate = $ birthDate ;
211
+
212
+ return $ this ;
213
+ }
214
+
215
+ /**
216
+ * Set tax document from holder.
217
+ *
218
+ * @param string $number Document number.
219
+ * @param string $type Document type.
220
+ *
221
+ * @return $this
222
+ */
223
+ public function setTaxDocument ($ number , $ type = self ::TAX_DOCUMENT )
224
+ {
225
+ $ this ->data ->taxDocument = new stdClass ();
226
+ $ this ->data ->taxDocument ->type = $ type ;
227
+ $ this ->data ->taxDocument ->number = $ number ;
228
+
229
+ return $ this ;
230
+ }
231
+
232
+ /**
233
+ * Set phone from holder.
234
+ *
235
+ * @param int $areaCode DDD telephone.
236
+ * @param int $number Telephone number.
237
+ * @param int $countryCode Country code.
238
+ *
239
+ * @return $this
240
+ */
241
+ public function setPhone ($ areaCode , $ number , $ countryCode = 55 )
242
+ {
243
+ $ this ->data ->phone = new stdClass ();
244
+ $ this ->data ->phone ->countryCode = $ countryCode ;
245
+ $ this ->data ->phone ->areaCode = $ areaCode ;
246
+ $ this ->data ->phone ->number = $ number ;
247
+
248
+ return $ this ;
249
+ }
250
+ }
0 commit comments