composer package updates
[openemr.git] / vendor / zendframework / zend-mail / src / Header / ListParser.php
blobd523d011896bc62903241a811739972b0dd86819
1 <?php
2 /**
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
6 */
8 namespace Zend\Mail\Header;
10 use function in_array;
12 /**
13 * @internal
15 class ListParser
17 const CHAR_QUOTES = ['\'', '"'];
18 const CHAR_DELIMS = [',', ';'];
19 const CHAR_ESCAPE = '\\';
21 /**
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.
26 * @return array
28 public static function parse($value, array $delims = self::CHAR_DELIMS)
30 $values = [];
31 $length = strlen($value);
32 $currentValue = '';
33 $inEscape = false;
34 $inQuote = false;
35 $currentQuoteDelim = null;
37 for ($i = 0; $i < $length; $i += 1) {
38 $char = $value[$i];
40 // If we are in an escape sequence, append the character and continue.
41 if ($inEscape) {
42 $currentValue .= $char;
43 $inEscape = false;
44 continue;
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;
51 $currentValue = '';
52 continue;
55 // Append the character to the current value
56 $currentValue .= $char;
58 // Escape sequence discovered.
59 if (self::CHAR_ESCAPE === $char) {
60 $inEscape = true;
61 continue;
64 // If the character is not a quote character, we are done
65 // processing it.
66 if (! in_array($char, self::CHAR_QUOTES)) {
67 continue;
70 // If the character matches a previously matched quote delimiter,
71 // we reset our quote status and the currently opened quote
72 // delimiter.
73 if ($char === $currentQuoteDelim) {
74 $inQuote = false;
75 $currentQuoteDelim = null;
76 continue;
79 // Otherwise, we're starting a quoted string.
80 $inQuote = true;
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;
90 return $values;