3 * @see https://github.com/zendframework/zend-mail for the canonical source repository
4 * @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-mail/blob/master/LICENSE.md New BSD License
8 namespace Zend\Mail\Header
;
10 use function in_array
;
17 const CHAR_QUOTES
= ['\'', '"'];
18 const CHAR_DELIMS
= [',', ';'];
19 const CHAR_ESCAPE
= '\\';
22 * @param string $value
23 * @param array $delims Delimiters allowed between values; parser will
24 * split on these, as long as they are not within quotes. Defaults
25 * to ListParser::CHAR_DELIMS.
28 public static function parse($value, array $delims = self
::CHAR_DELIMS
)
31 $length = strlen($value);
35 $currentQuoteDelim = null;
37 for ($i = 0; $i < $length; $i +
= 1) {
40 // If we are in an escape sequence, append the character and continue.
42 $currentValue .= $char;
47 // If we are not in a quoted string, and have a delimiter, append
48 // the current value to the list, and reset the current value.
49 if (in_array($char, $delims, true) && ! $inQuote) {
50 $values [] = $currentValue;
55 // Append the character to the current value
56 $currentValue .= $char;
58 // Escape sequence discovered.
59 if (self
::CHAR_ESCAPE
=== $char) {
64 // If the character is not a quote character, we are done
66 if (! in_array($char, self
::CHAR_QUOTES
)) {
70 // If the character matches a previously matched quote delimiter,
71 // we reset our quote status and the currently opened quote
73 if ($char === $currentQuoteDelim) {
75 $currentQuoteDelim = null;
79 // Otherwise, we're starting a quoted string.
81 $currentQuoteDelim = $char;
84 // If we reached the end of the string and still have a current value,
85 // append it to the list (no delimiter was reached).
86 if ('' !== $currentValue) {
87 $values [] = $currentValue;