|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FileEye\MimeMap; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class for parsing RFC 2045 Content-Type Header Fields. |
|
7
|
|
|
*/ |
|
8
|
|
|
class TypeParser |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Parses a part of the content MIME type string. |
|
12
|
|
|
* |
|
13
|
|
|
* Splits string and comment until a delimiter is found. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $string Input string. |
|
16
|
|
|
* @param int $offset Offset to start parsing from. |
|
17
|
|
|
* @param string $delimiter Stop parsing when delimiter found. |
|
18
|
|
|
* |
|
19
|
|
|
* @return array An array with the following keys: |
|
20
|
|
|
* 'string' - the uncommented part of $string |
|
21
|
|
|
* 'comment' - the comment part of $string |
|
22
|
|
|
* 'delimiter_matched' - true if a $delimiter stopped the parsing, false |
|
23
|
|
|
* otherwise |
|
24
|
|
|
* 'end_offset' - the last position parsed in $string. |
|
25
|
|
|
*/ |
|
26
|
53 |
|
public static function parseStringPart($string, $offset, $delimiter) |
|
27
|
|
|
{ |
|
28
|
53 |
|
$inquote = false; |
|
29
|
53 |
|
$escaped = false; |
|
30
|
53 |
|
$incomment = 0; |
|
31
|
53 |
|
$newstring = ''; |
|
32
|
53 |
|
$comment = ''; |
|
33
|
|
|
|
|
34
|
53 |
|
for ($n = $offset; $n < strlen($string); $n++) { |
|
35
|
51 |
|
if ($string[$n] === $delimiter && !$escaped && !$inquote && $incomment === 0) { |
|
36
|
49 |
|
break; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
50 |
View Code Duplication |
if ($escaped) { |
|
|
|
|
|
|
40
|
4 |
|
if ($incomment == 0) { |
|
41
|
2 |
|
$newstring .= $string[$n]; |
|
42
|
|
|
} else { |
|
43
|
2 |
|
$comment .= $string[$n]; |
|
44
|
|
|
} |
|
45
|
4 |
|
$escaped = false; |
|
46
|
4 |
|
continue; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
50 |
View Code Duplication |
if ($string[$n] == '\\') { |
|
|
|
|
|
|
50
|
4 |
|
if ($incomment > 0) { |
|
51
|
2 |
|
$comment .= $string[$n]; |
|
52
|
|
|
} |
|
53
|
4 |
|
$escaped = true; |
|
54
|
4 |
|
continue; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
50 |
|
if (!$inquote && $incomment > 0 && $string[$n] == ')') { |
|
58
|
21 |
|
$incomment--; |
|
59
|
21 |
|
if ($incomment == 0) { |
|
60
|
19 |
|
$comment .= ' '; |
|
61
|
|
|
} |
|
62
|
21 |
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
50 |
|
if (!$inquote && $string[$n] == '(') { |
|
66
|
21 |
|
$incomment++; |
|
67
|
21 |
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
50 |
View Code Duplication |
if ($string[$n] == '"') { |
|
|
|
|
|
|
71
|
9 |
|
if ($incomment > 0) { |
|
72
|
1 |
|
$comment .= $string[$n]; |
|
73
|
|
|
} else { |
|
74
|
8 |
|
if ($inquote) { |
|
75
|
8 |
|
$inquote = false; |
|
76
|
|
|
} else { |
|
77
|
8 |
|
$inquote = true; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
9 |
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
50 |
|
if ($incomment == 0) { |
|
84
|
50 |
|
$newstring .= $string[$n]; |
|
85
|
50 |
|
continue; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
21 |
|
$comment .= $string[$n]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
53 |
|
if ($incomment > 0) { |
|
92
|
2 |
|
throw new MalformedTypeException('Comment closing bracket missing: ' . $comment); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return [ |
|
96
|
52 |
|
'string' => empty($newstring) ? null : trim($newstring), |
|
97
|
52 |
|
'comment' => empty($comment) ? null : trim($comment), |
|
98
|
52 |
|
'delimiter_matched' => isset($string[$n]) ? ($string[$n] === $delimiter) : false, |
|
99
|
52 |
|
'end_offset' => $n, |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.