1 | <?php |
||
19 | class MimeLiteralPart extends LiteralPart |
||
20 | { |
||
21 | /** |
||
22 | * @var string regex pattern matching a mime-encoded part |
||
23 | */ |
||
24 | const MIME_PART_PATTERN = '=\?[A-Za-z\-_0-9\*]+\?[QBqb]\?[^\?]+\?='; |
||
25 | |||
26 | /** |
||
27 | * @var bool set to true to ignore spaces before this part |
||
28 | */ |
||
29 | protected $canIgnoreSpacesBefore = false; |
||
30 | |||
31 | /** |
||
32 | * @var bool set to true to ignore spaces after this part |
||
33 | */ |
||
34 | protected $canIgnoreSpacesAfter = false; |
||
35 | |||
36 | /** |
||
37 | * @var array maintains an array mapping rfc1766 language tags to parts of |
||
38 | * text in the value. |
||
39 | * |
||
40 | * Each array element is an array containing two elements, one with key |
||
41 | * 'lang', and another with key 'value'. |
||
42 | */ |
||
43 | protected $languages = []; |
||
44 | |||
45 | /** |
||
46 | * Decoding the passed token value if it's mime-encoded and assigns the |
||
47 | * decoded value to a member variable. Sets canIgnoreSpacesBefore and |
||
48 | * canIgnoreSpacesAfter. |
||
49 | * |
||
50 | * @param CharsetConverter $charsetConverter |
||
51 | * @param string $token |
||
52 | */ |
||
53 | 10 | public function __construct(CharsetConverter $charsetConverter, $token) |
|
62 | |||
63 | /** |
||
64 | * Finds and replaces mime parts with their values. |
||
65 | * |
||
66 | * The method splits the token value into an array on mime-part-patterns, |
||
67 | * either replacing a mime part with its value by calling iconv_mime_decode |
||
68 | * or converts the encoding on the text part by calling convertEncoding. |
||
69 | * |
||
70 | * @param string $value |
||
71 | * @return string |
||
72 | */ |
||
73 | 10 | protected function decodeMime($value) |
|
86 | |||
87 | /** |
||
88 | * Decodes a matched mime entity part into a string and returns it, after |
||
89 | * adding the string into the languages array. |
||
90 | * |
||
91 | * @param string[] $matches |
||
92 | * @return string |
||
93 | */ |
||
94 | 8 | private function decodeMatchedEntity($matches) |
|
107 | |||
108 | /** |
||
109 | * Decodes a single mime-encoded entity. |
||
110 | * |
||
111 | * Unfortunately, mb_decode_header fails for many charsets on PHP 5.4 and |
||
112 | * PHP 5.5 (even if they're listed as supported). iconv_mime_decode doesn't |
||
113 | * support all charsets. |
||
114 | * |
||
115 | * Parsing out the charset and body of the encoded entity seems to be the |
||
116 | * way to go to support the most charsets. |
||
117 | * |
||
118 | * @param string $entity |
||
119 | * @return string |
||
120 | */ |
||
121 | 10 | private function decodeSplitPart($entity) |
|
130 | |||
131 | /** |
||
132 | * Returns true if spaces before this part should be ignored. |
||
133 | * |
||
134 | * Overridden to return $this->canIgnoreSpacesBefore which is setup in the |
||
135 | * constructor. |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | 3 | public function ignoreSpacesBefore() |
|
143 | |||
144 | /** |
||
145 | * Returns true if spaces before this part should be ignored. |
||
146 | * |
||
147 | * Overridden to return $this->canIgnoreSpacesAfter which is setup in the |
||
148 | * constructor. |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | 3 | public function ignoreSpacesAfter() |
|
156 | |||
157 | /** |
||
158 | * Adds the passed part into the languages array with the given language. |
||
159 | * |
||
160 | * @param string $part |
||
161 | * @param string|null $language |
||
162 | */ |
||
163 | 10 | protected function addToLanguage($part, $language = null) |
|
170 | |||
171 | /** |
||
172 | * Returns an array of parts mapped to languages in the header value, for |
||
173 | * instance the string: |
||
174 | * |
||
175 | * 'Hello and =?UTF-8*fr-be?Q?bonjour_?= =?UTF-8*it?Q?mi amici?=. Welcome!' |
||
176 | * |
||
177 | * Would be mapped in the returned array as follows: |
||
178 | * |
||
179 | * ```php |
||
180 | * [ |
||
181 | * 0 => [ 'lang' => null, 'value' => 'Hello and ' ], |
||
182 | * 1 => [ 'lang' => 'fr-be', 'value' => 'bonjour ' ], |
||
183 | * 3 => [ 'lang' => 'it', 'value' => 'mi amici' ], |
||
184 | * 4 => [ 'lang' => null, 'value' => ' Weolcome!' ] |
||
185 | * ] |
||
186 | * ``` |
||
187 | * |
||
188 | * @return string[][] |
||
189 | */ |
||
190 | 3 | public function getLanguageArray() |
|
194 | } |
||
195 |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.