ptlis /
conneg
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @copyright (c) 2006-present brian ridley |
||
| 5 | * @author brian ridley <[email protected]> |
||
| 6 | * @license http://opensource.org/licenses/MIT MIT |
||
| 7 | */ |
||
| 8 | |||
| 9 | namespace ptlis\ConNeg\Parser; |
||
| 10 | |||
| 11 | use ptlis\ConNeg\Preference\Preference; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Simple tokenizer |
||
| 15 | */ |
||
| 16 | class FieldTokenizer |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Tokenize the HTTP field for subsequent processing. |
||
| 20 | * |
||
| 21 | * Note: we don't need to worry about multi-byte characters; HTTP fields must be ISO-8859-1 encoded. |
||
| 22 | * |
||
| 23 | * @param string $httpField |
||
| 24 | * @param string $fromField |
||
| 25 | * |
||
| 26 | * @return array<string> |
||
| 27 | */ |
||
| 28 | 80 | public function tokenize($httpField, $fromField) |
|
| 29 | { |
||
| 30 | 80 | $quoteSeparators = array('"', "'"); |
|
| 31 | 80 | $tokenList = array(); |
|
| 32 | 80 | $stringAccumulator = ''; |
|
| 33 | 80 | $lastQuote = ''; |
|
| 34 | |||
| 35 | // Iterate through field, character-by-character |
||
| 36 | 80 | for ($i = 0; $i < strlen($httpField); $i++) { |
|
| 37 | 72 | $chr = substr($httpField, $i, 1); |
|
| 38 | |||
| 39 | switch (true) { |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 40 | |||
| 41 | // We are at the end of a quoted string |
||
| 42 | 72 | case $chr === $lastQuote: |
|
| 43 | 5 | $tokenList[] = $stringAccumulator; |
|
| 44 | 5 | $stringAccumulator = ''; |
|
| 45 | 5 | $lastQuote = ''; |
|
| 46 | 5 | break; |
|
| 47 | |||
| 48 | // We have found the beginning of a quoted string |
||
| 49 | 72 | case in_array($chr, $quoteSeparators): |
|
| 50 | 5 | $lastQuote = $chr; |
|
| 51 | 5 | break; |
|
| 52 | |||
| 53 | // We are already within a quoted string, but not yet at the end |
||
| 54 | 72 | case strlen($lastQuote): |
|
| 55 | 5 | $stringAccumulator .= $chr; |
|
| 56 | 5 | break; |
|
| 57 | |||
| 58 | // Separators found, add previously accumulated string & separator to token list |
||
| 59 | 72 | case Tokens::isSeparator($chr, Preference::MIME === $fromField): |
|
| 60 | 72 | if (strlen($stringAccumulator)) { |
|
| 61 | 72 | $tokenList[] = $stringAccumulator; |
|
| 62 | 72 | $stringAccumulator = ''; |
|
| 63 | } |
||
| 64 | |||
| 65 | 72 | $tokenList[] = $chr; |
|
| 66 | 72 | break; |
|
| 67 | |||
| 68 | // Simply accumulate characters |
||
| 69 | default: |
||
| 70 | 72 | $stringAccumulator .= $chr; |
|
| 71 | 72 | break; |
|
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | // Handle final component |
||
| 76 | 80 | if (strlen($stringAccumulator)) { |
|
| 77 | 72 | $tokenList[] = $stringAccumulator; |
|
| 78 | } |
||
| 79 | |||
| 80 | // Remove any padding whitespace from token list |
||
| 81 | 80 | $tokenList = array_map('trim', $tokenList); |
|
| 82 | |||
| 83 | 80 | return $tokenList; |
|
| 84 | } |
||
| 85 | } |
||
| 86 |