Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Decoder/Decoder.php

raw symbols SCIP
1 <?php
2 /*
3 * File: Decoder.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 12.04.24 20:14
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP\Decoder;
14
15 use Webklex\PHPIMAP\EncodingAliases;
16
17 /**
18 * Class Decoder
19 *
20 * @package Webklex\PHPIMAP
21 */
22 abstract class Decoder implements DecoderInterface {
23
24 /**
25 * Decoder constructor.
26 * @param array $options Decoder options
27 * @param string $fallback_encoding Fallback encoding
28 */
29 public function __construct(
30 /**
31 * Options
32 *
33 * @var array
34 */
35 protected array $options = [],
36
37 /**
38 * Fallback Encoding
39 *
40 * @var string
41 */
42 protected string $fallback_encoding = 'UTF-8',
43 ) {
44 $this->options = array_merge([
45 'header' => 'utf-8',
46 'message' => 'utf-8',
47 'attachment' => 'utf-8',
48 ], $this->options);
49 }
50
51 /**
52 * Decode a given value
53 * @param array|string|null $value
54 * @param string|null $encoding
55 * @return mixed
56 */
57 public function decode(array|string|null $value, ?string $encoding = null): mixed {
58 return $value;
59 }
60
61 /**
62 * Convert the encoding
63 * @param string $str The string to convert
64 * @param string $from The source encoding
65 * @param string $to The target encoding
66 *
67 * @return mixed|string
68 */
69 public function convertEncoding(string $str, string $from = "ISO-8859-2", string $to = "UTF-8"): mixed {
70 $from = EncodingAliases::get($from, $this->fallback_encoding);
71 $to = EncodingAliases::get($to, $this->fallback_encoding);
72
73 if ($from === $to) {
74 return $str;
75 }
76
77 return EncodingAliases::convert($str, $from, $to);
78 }
79
80 /**
81 * Decode MIME header elements
82 * @link https://php.net/manual/en/function.imap-mime-header-decode.php
83 * @param string $text The MIME text
84 *
85 * @return array<int,object> Returns an array of objects. Each *object has two properties, charset and text.
86 */
87 public function mimeHeaderDecode(string $text): array {
88 if (extension_loaded('imap')) {
89 $result = \imap_mime_header_decode($text);
90 return is_array($result) ? $result : [];
91 }
92 $charset = $this->getEncoding($text);
93 return [(object)[
94 "charset" => $charset,
95 "text" => $this->convertEncoding($text, $charset)
96 ]];
97 }
98
99 /**
100 * Test if a given value is utf-8 encoded
101 * @param $value
102 *
103 * @return bool
104 */
105 public static function isUTF8($value): bool {
106 return str_starts_with(strtolower($value), '=?utf-8?');
107 }
108
109 /**
110 * Check if a given pair of strings has been decoded
111 * @param $encoded
112 * @param $decoded
113 *
114 * @return bool
115 */
116 public static function notDecoded($encoded, $decoded): bool {
117 return str_starts_with($decoded, '=?')
118 && strlen($decoded) - 2 === strpos($decoded, '?=')
119 && str_contains($encoded, $decoded);
120 }
121
122 /**
123 * Set the configuration used for decoding
124 * @param array $config
125 *
126 * @return Decoder
127 */
128 public function setOptions(array $config): static {
129 $this->options = $config;
130 return $this;
131 }
132
133 /**
134 * Get the configuration used for decoding
135 *
136 * @return array
137 */
138 public function getOptions(): array {
139 return $this->options;
140 }
141
142 /**
143 * Get the fallback encoding
144 *
145 * @return string
146 */
147 public function getFallbackEncoding(): string {
148 return $this->fallback_encoding;
149 }
150
151 /**
152 * Set the fallback encoding
153 *
154 * @param string $fallback_encoding
155 * @return Decoder
156 */
157 public function setFallbackEncoding(string $fallback_encoding): static {
158 $this->fallback_encoding = $fallback_encoding;
159 return $this;
160 }
161 }