update patch 1
This commit is contained in:
+21
-6
@@ -23,6 +23,8 @@ use Symfony\Component\Mime\Header\Headers;
|
||||
*/
|
||||
class TextPart extends AbstractPart
|
||||
{
|
||||
private const DEFAULT_ENCODERS = ['quoted-printable', 'base64', '8bit'];
|
||||
|
||||
/** @internal */
|
||||
protected Headers $_headers;
|
||||
|
||||
@@ -45,13 +47,13 @@ class TextPart extends AbstractPart
|
||||
parent::__construct();
|
||||
|
||||
if (!\is_string($body) && !\is_resource($body) && !$body instanceof File) {
|
||||
throw new \TypeError(sprintf('The body of "%s" must be a string, a resource, or an instance of "%s" (got "%s").', self::class, File::class, get_debug_type($body)));
|
||||
throw new \TypeError(\sprintf('The body of "%s" must be a string, a resource, or an instance of "%s" (got "%s").', self::class, File::class, get_debug_type($body)));
|
||||
}
|
||||
|
||||
if ($body instanceof File) {
|
||||
$path = $body->getPath();
|
||||
if ((is_file($path) && !is_readable($path)) || is_dir($path)) {
|
||||
throw new InvalidArgumentException(sprintf('Path "%s" is not readable.', $path));
|
||||
throw new InvalidArgumentException(\sprintf('Path "%s" is not readable.', $path));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,8 +65,8 @@ class TextPart extends AbstractPart
|
||||
if (null === $encoding) {
|
||||
$this->encoding = $this->chooseEncoding();
|
||||
} else {
|
||||
if ('quoted-printable' !== $encoding && 'base64' !== $encoding && '8bit' !== $encoding) {
|
||||
throw new InvalidArgumentException(sprintf('The encoding must be one of "quoted-printable", "base64", or "8bit" ("%s" given).', $encoding));
|
||||
if (!\in_array($encoding, self::DEFAULT_ENCODERS, true) && !\array_key_exists($encoding, self::$encoders)) {
|
||||
throw new InvalidArgumentException(\sprintf('The encoding must be one of "%s" ("%s" given).', implode('", "', array_unique(array_merge(self::DEFAULT_ENCODERS, array_keys(self::$encoders)))), $encoding));
|
||||
}
|
||||
$this->encoding = $encoding;
|
||||
}
|
||||
@@ -151,7 +153,7 @@ class TextPart extends AbstractPart
|
||||
if ($this->body instanceof File) {
|
||||
$path = $this->body->getPath();
|
||||
if (false === $handle = @fopen($path, 'r', false)) {
|
||||
throw new InvalidArgumentException(sprintf('Unable to open path "%s".', $path));
|
||||
throw new InvalidArgumentException(\sprintf('Unable to open path "%s".', $path));
|
||||
}
|
||||
|
||||
yield from $this->getEncoder()->encodeByteStream($handle);
|
||||
@@ -211,7 +213,20 @@ class TextPart extends AbstractPart
|
||||
return self::$encoders[$this->encoding] ??= new QpContentEncoder();
|
||||
}
|
||||
|
||||
return self::$encoders[$this->encoding] ??= new Base64ContentEncoder();
|
||||
if ('base64' === $this->encoding) {
|
||||
return self::$encoders[$this->encoding] ??= new Base64ContentEncoder();
|
||||
}
|
||||
|
||||
return self::$encoders[$this->encoding];
|
||||
}
|
||||
|
||||
public static function addEncoder(ContentEncoderInterface $encoder): void
|
||||
{
|
||||
if (\in_array($encoder->getName(), self::DEFAULT_ENCODERS, true)) {
|
||||
throw new InvalidArgumentException('You are not allowed to change the default encoders ("quoted-printable", "base64", and "8bit").');
|
||||
}
|
||||
|
||||
self::$encoders[$encoder->getName()] = $encoder;
|
||||
}
|
||||
|
||||
private function chooseEncoding(): string
|
||||
|
||||
Reference in New Issue
Block a user