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 InvalidArgumentException; |
28
|
|
|
use libredte\lib\Core\Package\Billing\Component\Exchange\Contract\AttachmentInterface; |
29
|
|
|
use Symfony\Component\Mime\MimeTypes; |
30
|
|
|
use Symfony\Component\Mime\Part\DataPart; |
31
|
|
|
use Symfony\Component\Mime\Part\File; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Clase para representar un archivo adjunto en un documento. |
35
|
|
|
*/ |
36
|
|
|
class Attachment extends DataPart implements AttachmentInterface |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Tamaño del archivo. |
40
|
|
|
* |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
private int $size; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor del archivo adjunto. |
47
|
|
|
* |
48
|
|
|
* @param string|File $body |
49
|
|
|
* @param string $filename |
50
|
|
|
* @param string|null $contentType |
51
|
|
|
* @param string|null $encoding |
52
|
|
|
* @param int|null $size |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
string|File $body, |
56
|
|
|
string $filename = null, |
57
|
|
|
?string $contentType = null, |
58
|
|
|
?string $encoding = null, |
59
|
|
|
?int $size = null |
60
|
|
|
) { |
61
|
|
|
if ($contentType === null) { |
62
|
|
|
$contentType = $this->guessContentType($filename); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
parent::__construct($body, $filename, $contentType, $encoding); |
66
|
|
|
|
67
|
|
|
if ($size !== null) { |
68
|
|
|
$this->size = $size; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function getContentType(): string |
76
|
|
|
{ |
77
|
|
|
return $this->getMediaType() . '/' . $this->getMediaSubtype(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
|
|
public function getSize(): int |
84
|
|
|
{ |
85
|
|
|
if (!isset($this->size)) { |
86
|
|
|
$this->size = strlen($this->getBody()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->size; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function toArray(): array |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
'data' => $this->getBody(), |
99
|
|
|
'name' => $this->getFilename(), |
100
|
|
|
'type' => $this->getContentType(), |
101
|
|
|
'size' => $this->getSize(), |
102
|
|
|
]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Adivina el Content Type del contenido del archivo a partir de la |
107
|
|
|
* extensión del nombre del archivo. |
108
|
|
|
* |
109
|
|
|
* @param string $filename |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
private function guessContentType(string $filename): string |
113
|
|
|
{ |
114
|
|
|
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$mimeTypes = MimeTypes::getDefault()->getMimeTypes($extension); |
117
|
|
|
|
118
|
|
|
if (!isset($mimeTypes[0])) { |
119
|
|
|
throw new InvalidArgumentException(sprintf( |
120
|
|
|
'El archivo %s tiene una extensión inválida. Corrige la extensión o especifica el $contentType.', |
121
|
|
|
$filename |
122
|
|
|
)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $mimeTypes[0]; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|