1
|
|
|
<?php |
2
|
|
|
namespace Eduardokum\CorreiosPhp\Entities; |
3
|
|
|
|
4
|
|
|
use Eduardokum\CorreiosPhp\Contracts\Config\Config as ConfigContract; |
5
|
|
|
use Eduardokum\CorreiosPhp\Contracts\Render\Printable as PrintableContract; |
6
|
|
|
use Eduardokum\CorreiosPhp\Exception\InvalidArgumentException; |
7
|
|
|
use Eduardokum\CorreiosPhp\Validator; |
8
|
|
|
|
9
|
|
|
class MailingList implements PrintableContract |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
private $id = 1; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* MailingList constructor. |
19
|
|
|
* |
20
|
|
|
* @param $id |
21
|
|
|
*/ |
22
|
|
|
public function __construct($id) |
23
|
|
|
{ |
24
|
|
|
$this->id = $id; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var PostalObject[] |
29
|
|
|
*/ |
30
|
|
|
private $objects = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $tags = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \DOMDocument |
39
|
|
|
*/ |
40
|
|
|
private $dom; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $model = PrintableContract::MODEL_MULTIPLE; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $size = PrintableContract::SIZE_SMALL; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param PostalObject $postalObject |
54
|
|
|
*/ |
55
|
|
|
public function addObject(PostalObject $postalObject) |
56
|
|
|
{ |
57
|
|
|
if (in_array($postalObject->getTag(), $this->tags)) { |
58
|
|
|
throw new InvalidArgumentException(sprintf("Tag '%s' already added", $postalObject->getTagDv())); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->tags[] = $postalObject->getTag(); |
62
|
|
|
$this->objects[] = $postalObject; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function getTags() |
69
|
|
|
{ |
70
|
|
|
return $this->tags; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return int |
75
|
|
|
*/ |
76
|
|
|
public function getId() |
77
|
|
|
{ |
78
|
|
|
return $this->id; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ConfigContract $config |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
* @throws \Exception |
86
|
|
|
*/ |
87
|
|
|
public function save(ConfigContract $config) |
88
|
|
|
{ |
89
|
|
|
$this->dom = new \DOMDocument('1.0', 'ISO-8859-1'); |
90
|
|
|
$this->dom->preserveWhiteSpace = false; |
91
|
|
|
$this->dom->formatOutput = false; |
92
|
|
|
|
93
|
|
|
$correioslog = $this->dom->createElement('correioslog'); |
94
|
|
|
$correioslog->appendChild($this->dom->createElement('tipo_arquivo', 'Postagem')); |
95
|
|
|
$correioslog->appendChild($this->dom->createElement('versao_arquivo', '2.3')); |
96
|
|
|
$correioslog->appendChild($this->plp($config)); |
97
|
|
|
$correioslog->appendChild($this->remetente($config)); |
98
|
|
|
$correioslog->appendChild($this->dom->createElement('forma_pagamento')); |
99
|
|
|
|
100
|
|
|
foreach ($this->objects as $object) { |
101
|
|
|
$correioslog->appendChild($this->objectPostal($object)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->dom->appendChild($correioslog); |
105
|
|
|
$xml = utf8_encode($this->dom->saveXML()); |
106
|
|
|
Validator::isValid($xml, realpath(CORREIOS_PHP_BASE . '/storage/schemes/') . '/plp.xsd'); |
107
|
|
|
return htmlentities($xml); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getModel() |
114
|
|
|
{ |
115
|
|
|
return $this->model; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $model |
120
|
|
|
* |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function setModel($model) |
124
|
|
|
{ |
125
|
|
|
$this->model = $model; |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getSize() |
133
|
|
|
{ |
134
|
|
|
return $this->size; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param $size |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function setSize($size) |
143
|
|
|
{ |
144
|
|
|
$this->size = $size; |
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function toPrint() |
152
|
|
|
{ |
153
|
|
|
return $this->objects; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param ConfigContract $config |
158
|
|
|
* |
159
|
|
|
* @return mixed |
160
|
|
|
*/ |
161
|
|
|
private function plp(ConfigContract $config) |
162
|
|
|
{ |
163
|
|
|
$plp = $this->dom->createElement('plp'); |
164
|
|
|
$plp->appendChild($this->dom->createElement('id_plp')); |
165
|
|
|
$plp->appendChild($this->dom->createElement('valor_global')); |
166
|
|
|
$plp->appendChild($this->dom->createElement('mcu_unidade_postagem')); |
167
|
|
|
$plp->appendChild($this->dom->createElement('nome_unidade_postagem')); |
168
|
|
|
$plp->appendChild($this->dom->createElement('cartao_postagem', $config->getPostCard())); |
169
|
|
|
|
170
|
|
|
return $plp; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param ConfigContract $config |
175
|
|
|
* |
176
|
|
|
* @return mixed |
177
|
|
|
*/ |
178
|
|
|
private function remetente(ConfigContract $config) |
179
|
|
|
{ |
180
|
|
|
$remetente = $this->dom->createElement('remetente'); |
181
|
|
|
$remetente->appendChild( |
182
|
|
|
$this->dom->createElement('numero_contrato', $config->getContract()) |
183
|
|
|
); |
184
|
|
|
$remetente->appendChild( |
185
|
|
|
$this->dom->createElement('numero_diretoria', $config->getDirection()) |
186
|
|
|
); |
187
|
|
|
$remetente->appendChild( |
188
|
|
|
$this->dom->createElement('codigo_administrativo', $config->getAdministrativeCode()) |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
$remetente->appendChild( |
192
|
|
|
$nome_remetente = $this->dom->createElement('nome_remetente') |
193
|
|
|
); |
194
|
|
|
$nome_remetente->appendChild($this->dom->createCDATASection($config->getSender()->getName())); |
195
|
|
|
|
196
|
|
|
$remetente->appendChild( |
197
|
|
|
$logradouro_remetente = $this->dom->createElement('logradouro_remetente') |
198
|
|
|
); |
199
|
|
|
$logradouro_remetente->appendChild($this->dom->createCDATASection($config->getSender()->getStreet())); |
200
|
|
|
|
201
|
|
|
$remetente->appendChild( |
202
|
|
|
$this->dom->createElement('numero_remetente', $config->getSender()->getNumber()) |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
$remetente->appendChild( |
206
|
|
|
$complemento_remetente = $this->dom->createElement('complemento_remetente') |
207
|
|
|
); |
208
|
|
|
$complemento_remetente->appendChild($this->dom->createCDATASection($config->getSender()->getComplement())); |
209
|
|
|
|
210
|
|
|
$remetente->appendChild( |
211
|
|
|
$bairro_remetente = $this->dom->createElement('bairro_remetente') |
212
|
|
|
); |
213
|
|
|
$bairro_remetente->appendChild($this->dom->createCDATASection($config->getSender()->getDistrict())); |
214
|
|
|
|
215
|
|
|
$remetente->appendChild( |
216
|
|
|
$cep_remetente = $this->dom->createElement('cep_remetente') |
217
|
|
|
); |
218
|
|
|
$cep_remetente->appendChild($this->dom->createCDATASection($config->getSender()->getCep())); |
219
|
|
|
|
220
|
|
|
$remetente->appendChild( |
221
|
|
|
$cidade_remetente = $this->dom->createElement('cidade_remetente') |
222
|
|
|
); |
223
|
|
|
$cidade_remetente->appendChild($this->dom->createCDATASection($config->getSender()->getCity())); |
224
|
|
|
|
225
|
|
|
$remetente->appendChild( |
226
|
|
|
$this->dom->createElement('uf_remetente', $config->getSender()->getState()) |
227
|
|
|
); |
228
|
|
|
|
229
|
|
|
$remetente->appendChild( |
230
|
|
|
$telefone_remetente = $this->dom->createElement('telefone_remetente') |
231
|
|
|
); |
232
|
|
|
$telefone_remetente->appendChild($this->dom->createCDATASection($config->getSender()->getPhone())); |
233
|
|
|
|
234
|
|
|
$remetente->appendChild( |
235
|
|
|
$fax_remetente = $this->dom->createElement('fax_remetente') |
236
|
|
|
); |
237
|
|
|
$fax_remetente->appendChild($this->dom->createCDATASection('')); |
238
|
|
|
|
239
|
|
|
$remetente->appendChild( |
240
|
|
|
$email_remetente = $this->dom->createElement('email_remetente') |
241
|
|
|
); |
242
|
|
|
$email_remetente->appendChild($this->dom->createCDATASection($config->getSender()->getMail())); |
243
|
|
|
|
244
|
|
|
$remetente->appendChild( |
245
|
|
|
$email_remetente = $this->dom->createElement('celular_remetente') |
246
|
|
|
); |
247
|
|
|
$email_remetente->appendChild($this->dom->createCDATASection($config->getSender()->getCellphone())); |
248
|
|
|
|
249
|
|
|
return $remetente; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param PostalObject $object |
254
|
|
|
* |
255
|
|
|
* @return \DOMElement |
256
|
|
|
*/ |
257
|
|
|
private function objectPostal(PostalObject $object) |
258
|
|
|
{ |
259
|
|
|
$objeto_postal = $this->dom->createElement('objeto_postal'); |
260
|
|
|
$objeto_postal->appendChild($this->dom->createElement('numero_etiqueta', $object->getTagDv())); |
261
|
|
|
$objeto_postal->appendChild($this->dom->createElement('codigo_objeto_cliente')); |
262
|
|
|
$objeto_postal->appendChild($this->dom->createElement('codigo_servico_postagem', $object->getService())); |
263
|
|
|
$objeto_postal->appendChild($this->dom->createElement('cubagem', $object->getCubing())); |
264
|
|
|
$objeto_postal->appendChild($this->dom->createElement('peso', $object->getWeight())); |
265
|
|
|
$objeto_postal->appendChild($this->dom->createElement('rt1')); |
266
|
|
|
$objeto_postal->appendChild($this->dom->createElement('rt2')); |
267
|
|
|
$objeto_postal->appendChild($this->destinatario($object)); |
268
|
|
|
$objeto_postal->appendChild($this->nacional($object)); |
269
|
|
|
$objeto_postal->appendChild($this->adicional($object)); |
270
|
|
|
$objeto_postal->appendChild($this->dimensao($object)); |
271
|
|
|
$objeto_postal->appendChild($this->dom->createElement('data_postagem_sara')); |
272
|
|
|
$objeto_postal->appendChild($this->dom->createElement('status_processamento', '0')); |
273
|
|
|
$objeto_postal->appendChild($this->dom->createElement('numero_comprovante_postagem')); |
274
|
|
|
$objeto_postal->appendChild($this->dom->createElement('valor_cobrado')); |
275
|
|
|
|
276
|
|
|
return $objeto_postal; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param PostalObject $object |
281
|
|
|
* |
282
|
|
|
* @return \DOMElement |
283
|
|
|
*/ |
284
|
|
|
private function destinatario(PostalObject $object) |
285
|
|
|
{ |
286
|
|
|
$destinatario = $this->dom->createElement('destinatario'); |
287
|
|
|
$destinatario->appendChild( |
288
|
|
|
$nome_destinatario = $this->dom->createElement('nome_destinatario') |
289
|
|
|
); |
290
|
|
|
$nome_destinatario->appendChild($this->dom->createCDATASection($object->getRecipient()->getName())); |
291
|
|
|
|
292
|
|
|
$destinatario->appendChild( |
293
|
|
|
$telefone_destinatario = $this->dom->createElement('telefone_destinatario') |
294
|
|
|
); |
295
|
|
|
$telefone_destinatario->appendChild($this->dom->createCDATASection($object->getRecipient()->getPhone())); |
296
|
|
|
|
297
|
|
|
$destinatario->appendChild( |
298
|
|
|
$celular_destinatario = $this->dom->createElement('celular_destinatario') |
299
|
|
|
); |
300
|
|
|
$celular_destinatario->appendChild($this->dom->createCDATASection($object->getRecipient()->getCellphone())); |
301
|
|
|
|
302
|
|
|
$destinatario->appendChild( |
303
|
|
|
$email_destinatario = $this->dom->createElement('email_destinatario') |
304
|
|
|
); |
305
|
|
|
$email_destinatario->appendChild($this->dom->createCDATASection($object->getRecipient()->getMail())); |
306
|
|
|
|
307
|
|
|
$destinatario->appendChild( |
308
|
|
|
$logradouro_destinatario = $this->dom->createElement('logradouro_destinatario') |
309
|
|
|
); |
310
|
|
|
$logradouro_destinatario->appendChild($this->dom->createCDATASection($object->getRecipient()->getStreet())); |
311
|
|
|
|
312
|
|
|
$destinatario->appendChild( |
313
|
|
|
$complemento_destinatario = $this->dom->createElement('complemento_destinatario') |
314
|
|
|
); |
315
|
|
|
$complemento_destinatario->appendChild($this->dom->createCDATASection($object->getRecipient()->getComplement())); |
316
|
|
|
|
317
|
|
|
$destinatario->appendChild($this->dom->createElement( |
318
|
|
|
'numero_end_destinatario', |
319
|
|
|
$object->getRecipient()->getNumber() |
320
|
|
|
)); |
321
|
|
|
|
322
|
|
|
return $destinatario; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param PostalObject $object |
327
|
|
|
* |
328
|
|
|
* @return \DOMElement |
329
|
|
|
*/ |
330
|
|
|
private function nacional(PostalObject $object) |
331
|
|
|
{ |
332
|
|
|
if ($object->getRecipient()->isNational()) { |
333
|
|
|
$nacional = $this->dom->createElement('nacional'); |
334
|
|
|
$nacional->appendChild( |
335
|
|
|
$bairro_destinatario = $this->dom->createElement('bairro_destinatario') |
336
|
|
|
); |
337
|
|
|
$bairro_destinatario->appendChild($this->dom->createCDATASection($object->getRecipient()->getDistrict())); |
338
|
|
|
$nacional->appendChild( |
339
|
|
|
$cidade_destinatario = $this->dom->createElement('cidade_destinatario') |
340
|
|
|
); |
341
|
|
|
$cidade_destinatario->appendChild($this->dom->createCDATASection($object->getRecipient()->getCity())); |
342
|
|
|
$nacional->appendChild($this->dom->createElement( |
343
|
|
|
'uf_destinatario', |
344
|
|
|
$object->getRecipient()->getState() |
345
|
|
|
)); |
346
|
|
|
$nacional->appendChild( |
347
|
|
|
$cep_destinatario = $this->dom->createElement('cep_destinatario') |
348
|
|
|
); |
349
|
|
|
$cep_destinatario->appendChild($this->dom->createCDATASection($object->getRecipient()->getCep())); |
350
|
|
|
$nacional->appendChild($this->dom->createElement( |
351
|
|
|
'codigo_usuario_postal', |
352
|
|
|
$object->getPostalUserCode() |
353
|
|
|
)); |
354
|
|
|
$nacional->appendChild($this->dom->createElement( |
355
|
|
|
'centro_custo_cliente', |
356
|
|
|
$object->getCostCenter() |
357
|
|
|
)); |
358
|
|
|
$nacional->appendChild($this->dom->createElement( |
359
|
|
|
'numero_nota_fiscal', |
360
|
|
|
$object->getInvoiceNumber() |
361
|
|
|
)); |
362
|
|
|
$nacional->appendChild($this->dom->createElement( |
363
|
|
|
'serie_nota_fiscal', |
364
|
|
|
$object->getInvoiceSeries() |
365
|
|
|
)); |
366
|
|
|
$nacional->appendChild($this->dom->createElement( |
367
|
|
|
'valor_nota_fiscal', |
368
|
|
|
$object->getInvoiceValue() |
369
|
|
|
)); |
370
|
|
|
$nacional->appendChild($this->dom->createElement('natureza_nota_fiscal')); |
371
|
|
|
$nacional->appendChild( |
372
|
|
|
$descricao_objeto = $this->dom->createElement('descricao_objeto') |
373
|
|
|
); |
374
|
|
|
$descricao_objeto->appendChild($this->dom->createCDATASection($object->getDescription())); |
375
|
|
|
$nacional->appendChild($this->dom->createElement( |
376
|
|
|
'valor_a_cobrar', |
377
|
|
|
$object->getValueCharge() |
378
|
|
|
)); |
379
|
|
|
} else { |
380
|
|
|
$nacional = $this->dom->createElement('internacional'); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
return $nacional; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @param PostalObject $object |
388
|
|
|
* |
389
|
|
|
* @return \DOMElement |
390
|
|
|
*/ |
391
|
|
|
private function adicional(PostalObject $object) |
392
|
|
|
{ |
393
|
|
|
$servico_adicional = $this->dom->createElement('servico_adicional'); |
394
|
|
|
foreach ($object->getAdditionalServices() as $aditional_service) { |
395
|
|
|
$servico_adicional->appendChild($this->dom->createElement('codigo_servico_adicional', $aditional_service)); |
396
|
|
|
} |
397
|
|
|
$servico_adicional->appendChild($this->dom->createElement('valor_declarado', $object->getValueDeclared())); |
398
|
|
|
return $servico_adicional; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* @param PostalObject $object |
403
|
|
|
* |
404
|
|
|
* @return \DOMElement |
405
|
|
|
*/ |
406
|
|
|
private function dimensao(PostalObject $object) |
407
|
|
|
{ |
408
|
|
|
$dimensao_objeto = $this->dom->createElement('dimensao_objeto'); |
409
|
|
|
$dimensao_objeto->appendChild($this->dom->createElement('tipo_objeto', $object->getType())); |
410
|
|
|
$dimensao_objeto->appendChild($this->dom->createElement('dimensao_altura', $object->getHeight())); |
411
|
|
|
$dimensao_objeto->appendChild($this->dom->createElement('dimensao_largura', $object->getWidth())); |
412
|
|
|
$dimensao_objeto->appendChild($this->dom->createElement('dimensao_comprimento', $object->getLength())); |
413
|
|
|
$dimensao_objeto->appendChild($this->dom->createElement('dimensao_diametro', $object->getDiameter() ?: '0')); |
414
|
|
|
|
415
|
|
|
return $dimensao_objeto; |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
|