1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* LibreDTE: Biblioteca PHP (Núcleo). |
7
|
|
|
* Copyright (C) LibreDTE <https://www.libredte.cl> |
8
|
|
|
* |
9
|
|
|
* Este programa es software libre: usted puede redistribuirlo y/o modificarlo |
10
|
|
|
* bajo los términos de la Licencia Pública General Affero de GNU publicada por |
11
|
|
|
* la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o |
12
|
|
|
* (a su elección) cualquier versión posterior de la misma. |
13
|
|
|
* |
14
|
|
|
* Este programa se distribuye con la esperanza de que sea útil, pero SIN |
15
|
|
|
* GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD |
16
|
|
|
* PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública |
17
|
|
|
* General Affero de GNU para obtener una información más detallada. |
18
|
|
|
* |
19
|
|
|
* Debería haber recibido una copia de la Licencia Pública General Affero de |
20
|
|
|
* GNU junto a este programa. |
21
|
|
|
* |
22
|
|
|
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace libredte\lib\Core\Package\Billing\Component\Exchange\Support; |
26
|
|
|
|
27
|
|
|
use DateTimeImmutable; |
28
|
|
|
use DateTimeInterface; |
29
|
|
|
use Derafu\Lib\Core\Helper\Str; |
30
|
|
|
use Derafu\Lib\Core\Support\Store\Bag; |
31
|
|
|
use Derafu\Lib\Core\Support\Store\Contract\BagInterface; |
32
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\DocumentInterface; |
33
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\EnvelopeInterface; |
34
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\ReceiverInterface; |
35
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\SenderInterface; |
36
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Enum\DocumentType; |
37
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Enum\ProcessType; |
38
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Exception\ExchangeException; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Clase que representa un sobre con documentos. |
42
|
|
|
* |
43
|
|
|
* Los sobres típicamente tendrán solo un documento. Y si llegasen a tener más |
44
|
|
|
* de un documento serían siempre del mismo remitente y al mismo destinatario. |
45
|
|
|
*/ |
46
|
|
|
class Envelope implements EnvelopeInterface |
47
|
|
|
{ |
48
|
|
|
/** |
49
|
|
|
* Remitente del sobre. |
50
|
|
|
* |
51
|
|
|
* @var SenderInterface |
52
|
|
|
*/ |
53
|
|
|
private SenderInterface $sender; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Receptor del sobre. |
57
|
|
|
* |
58
|
|
|
* @var ReceiverInterface |
59
|
|
|
*/ |
60
|
|
|
private ReceiverInterface $receiver; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Tipo de documento que el sobre tiene. |
64
|
|
|
* |
65
|
|
|
* @var DocumentType |
66
|
|
|
*/ |
67
|
|
|
private DocumentType $documentType; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Tipo de proceso asociado al intercambio de documentos del sobre. |
71
|
|
|
* |
72
|
|
|
* @var ProcessType |
73
|
|
|
*/ |
74
|
|
|
private ProcessType $process; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Fecha y hora de creación del sobre. |
78
|
|
|
* |
79
|
|
|
* @var DateTimeInterface |
80
|
|
|
*/ |
81
|
|
|
private DateTimeInterface $creationDateAndTime; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Identificador único del mensaje comercial encapsulado. |
85
|
|
|
* |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
private string $businessMessageID; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Identificador del mensaje original al que se responde. |
92
|
|
|
* |
93
|
|
|
* @var string|null |
94
|
|
|
*/ |
95
|
|
|
private ?string $originalBusinessMessageID; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Documentos que están en el sobre. |
99
|
|
|
* |
100
|
|
|
* Normalmente un sobre tendría solo un documento. Sin embargo, el |
101
|
|
|
* componente de intercambio soporta múltiples documentos en un sobre. Cómo |
102
|
|
|
* serán manejados los múltiples documentos de un sobre es una |
103
|
|
|
* responsabilidad de cada estrategia de intercambio. |
104
|
|
|
* |
105
|
|
|
* @var DocumentInterface[] |
106
|
|
|
*/ |
107
|
|
|
private array $documents; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Metadatos del sobre. |
111
|
|
|
* |
112
|
|
|
* Estos metadatos están directamente relacionados con el sobre que se está |
113
|
|
|
* intercambiando y el tipo de transporte que se esté usando para dicho |
114
|
|
|
* intercambio. |
115
|
|
|
* |
116
|
|
|
* @var BagInterface |
117
|
|
|
*/ |
118
|
|
|
private BagInterface $metadata; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Constructor del sobre. |
122
|
|
|
* |
123
|
|
|
* @param SenderInterface $sender |
124
|
|
|
* @param ReceiverInterface $receiver |
125
|
|
|
* @param DocumentType $documentType |
126
|
|
|
* @param ProcessType $process |
127
|
|
|
* @param string|null $businessMessageID |
128
|
|
|
* @param string|null $originalBusinessMessageID |
129
|
|
|
* @param DateTimeInterface|null $creationDateAndTime |
130
|
|
|
* @param array $documents |
131
|
|
|
* @param BagInterface|array $metadata |
132
|
|
|
*/ |
133
|
|
|
public function __construct( |
134
|
|
|
SenderInterface $sender, |
135
|
|
|
ReceiverInterface $receiver, |
136
|
|
|
DocumentType $documentType = DocumentType::B2B, |
137
|
|
|
ProcessType $process = ProcessType::BILLING, |
138
|
|
|
?string $businessMessageID = null, |
139
|
|
|
?string $originalBusinessMessageID = null, |
140
|
|
|
?DateTimeInterface $creationDateAndTime = null, |
141
|
|
|
array $documents = [], |
142
|
|
|
BagInterface|array $metadata = [] |
143
|
|
|
) { |
144
|
|
|
$this->sender = $sender; |
145
|
|
|
$this->receiver = $receiver; |
146
|
|
|
$this->documentType = $documentType; |
147
|
|
|
$this->process = $process; |
148
|
|
|
$this->businessMessageID = $businessMessageID ?? Str::uuid4(); |
149
|
|
|
$this->originalBusinessMessageID = $originalBusinessMessageID; |
150
|
|
|
$this->creationDateAndTime = $creationDateAndTime |
151
|
|
|
?? new DateTimeImmutable() |
152
|
|
|
; |
153
|
|
|
$this->setDocuments($documents); |
154
|
|
|
$this->setMetadata($metadata); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritDoc} |
159
|
|
|
*/ |
160
|
|
|
public function getSender(): SenderInterface |
161
|
|
|
{ |
162
|
|
|
return $this->sender; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritDoc} |
167
|
|
|
*/ |
168
|
|
|
public function getReceiver(): ReceiverInterface |
169
|
|
|
{ |
170
|
|
|
return $this->receiver; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritDoc} |
175
|
|
|
*/ |
176
|
|
|
public function getDocumentType(): DocumentType |
177
|
|
|
{ |
178
|
|
|
return $this->documentType; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritDoc} |
183
|
|
|
*/ |
184
|
|
|
public function getProcess(): ProcessType |
185
|
|
|
{ |
186
|
|
|
return $this->process; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritDoc} |
191
|
|
|
*/ |
192
|
|
|
public function getBusinessMessageID(): string |
193
|
|
|
{ |
194
|
|
|
return $this->businessMessageID; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritDoc} |
199
|
|
|
*/ |
200
|
|
|
public function getOriginalBusinessMessageID(): ?string |
201
|
|
|
{ |
202
|
|
|
return $this->originalBusinessMessageID; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritDoc} |
207
|
|
|
*/ |
208
|
|
|
public function getCreationDateAndTime(): DateTimeInterface |
209
|
|
|
{ |
210
|
|
|
return $this->creationDateAndTime; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritDoc} |
215
|
|
|
*/ |
216
|
|
|
public function setDocuments(array $documents): static |
217
|
|
|
{ |
218
|
|
|
foreach ($documents as $document) { |
219
|
|
|
$this->addDocument($document); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritDoc} |
227
|
|
|
*/ |
228
|
|
|
public function addDocument(DocumentInterface $document): static |
229
|
|
|
{ |
230
|
|
|
if ($document->getType() !== $this->getDocumentType()) { |
231
|
|
|
throw new ExchangeException(sprintf( |
232
|
|
|
'El tipo del documento %s no coincide con los tipos de documento que el sobre puede contener %s.', |
233
|
|
|
$document->getType()->getID(), |
234
|
|
|
$this->getDocumentType()->getID() |
235
|
|
|
)); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$this->documents[] = $document; |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritDoc} |
245
|
|
|
*/ |
246
|
|
|
public function getDocuments(): array |
247
|
|
|
{ |
248
|
|
|
return $this->documents; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* {@inheritDoc} |
253
|
|
|
*/ |
254
|
|
|
public function countDocuments(): int |
255
|
|
|
{ |
256
|
|
|
return count($this->documents); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* {@inheritDoc} |
261
|
|
|
*/ |
262
|
|
|
public function setMetadata(BagInterface|array $metadata): static |
263
|
|
|
{ |
264
|
|
|
$this->metadata = is_array($metadata) |
|
|
|
|
265
|
|
|
? new Bag($metadata) |
266
|
|
|
: $metadata |
267
|
|
|
; |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* {@inheritDoc} |
274
|
|
|
*/ |
275
|
|
|
public function addMetadata(string $key, mixed $value): static |
276
|
|
|
{ |
277
|
|
|
$this->metadata->set($key, $value); |
278
|
|
|
|
279
|
|
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* {@inheritDoc} |
284
|
|
|
*/ |
285
|
|
|
public function getMetadata(): BagInterface |
286
|
|
|
{ |
287
|
|
|
return $this->metadata; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|