|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ZBateson\MailMimeParser project. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace ZBateson\MailMimeParser\Header\Consumer; |
|
9
|
|
|
|
|
10
|
|
|
use Iterator; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use ZBateson\MailMimeParser\Header\IHeaderPart; |
|
13
|
|
|
use ZBateson\MailMimeParser\Header\Part\MimeTokenPartFactory; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Consumes all tokens within parentheses as comments. |
|
17
|
|
|
* |
|
18
|
|
|
* Parenthetical comments in mime-headers can be nested within one another. The |
|
19
|
|
|
* outer-level continues after an inner-comment ends. Additionally, |
|
20
|
|
|
* quoted-literals may exist with comments as well meaning a parenthesis inside |
|
21
|
|
|
* a quoted string would not begin or end a comment section. |
|
22
|
|
|
* |
|
23
|
|
|
* In order to satisfy these specifications, CommentConsumerService inherits |
|
24
|
|
|
* from GenericConsumerService which defines CommentConsumerService and |
|
25
|
|
|
* QuotedStringConsumerService as sub-consumers. |
|
26
|
|
|
* |
|
27
|
|
|
* Examples: |
|
28
|
|
|
* X-Mime-Header: Some value (comment) |
|
29
|
|
|
* X-Mime-Header: Some value (comment (nested comment) still in comment) |
|
30
|
|
|
* X-Mime-Header: Some value (comment "and part of original ) comment" - |
|
31
|
|
|
* still a comment) |
|
32
|
|
|
* |
|
33
|
|
|
* @author Zaahid Bateson |
|
34
|
|
|
*/ |
|
35
|
|
|
class CommentConsumerService extends GenericConsumerService |
|
36
|
|
|
{ |
|
37
|
5 |
|
public function __construct( |
|
38
|
|
|
LoggerInterface $logger, |
|
39
|
|
|
MimeTokenPartFactory $partFactory, |
|
40
|
|
|
QuotedStringConsumerService $quotedStringConsumerService |
|
41
|
|
|
) { |
|
42
|
5 |
|
parent::__construct( |
|
43
|
5 |
|
$logger, |
|
44
|
5 |
|
$partFactory, |
|
45
|
5 |
|
$this, |
|
46
|
5 |
|
$quotedStringConsumerService |
|
47
|
5 |
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns patterns matching open and close parenthesis characters |
|
52
|
|
|
* as separators. |
|
53
|
|
|
* |
|
54
|
|
|
* @return string[] the patterns |
|
55
|
|
|
*/ |
|
56
|
7 |
|
protected function getTokenSeparators() : array |
|
57
|
|
|
{ |
|
58
|
7 |
|
return \array_merge(parent::getTokenSeparators(), ['\(', '\)']); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns true if the token is an open parenthesis character, '('. |
|
63
|
|
|
*/ |
|
64
|
111 |
|
protected function isStartToken(string $token) : bool |
|
65
|
|
|
{ |
|
66
|
111 |
|
return ($token === '('); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns true if the token is a close parenthesis character, ')'. |
|
71
|
|
|
*/ |
|
72
|
19 |
|
protected function isEndToken(string $token) : bool |
|
73
|
|
|
{ |
|
74
|
19 |
|
return ($token === ')'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Instantiates and returns Part\Token objects. |
|
79
|
|
|
* |
|
80
|
|
|
* Tokens from this and sub-consumers are combined into a Part\CommentPart |
|
81
|
|
|
* in processParts. |
|
82
|
|
|
*/ |
|
83
|
19 |
|
protected function getPartForToken(string $token, bool $isLiteral) : ?IHeaderPart |
|
84
|
|
|
{ |
|
85
|
19 |
|
return $this->partFactory->newInstance($token); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Calls $tokens->next() and returns. |
|
90
|
|
|
* |
|
91
|
|
|
* The default implementation checks if the current token is an end token, |
|
92
|
|
|
* and will not advance past it. Because a comment part of a header can be |
|
93
|
|
|
* nested, its implementation must advance past its own 'end' token. |
|
94
|
|
|
*/ |
|
95
|
19 |
|
protected function advanceToNextToken(Iterator $tokens, bool $isStartToken) : static |
|
96
|
|
|
{ |
|
97
|
19 |
|
$tokens->next(); |
|
98
|
19 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Post processing involves creating a single Part\CommentPart out of |
|
103
|
|
|
* generated parts from tokens. The Part\CommentPart is returned in an |
|
104
|
|
|
* array. |
|
105
|
|
|
* |
|
106
|
|
|
* @param IHeaderPart[] $parts |
|
107
|
|
|
* @return IHeaderPart[] |
|
108
|
|
|
*/ |
|
109
|
19 |
|
protected function processParts(array $parts) : array |
|
110
|
|
|
{ |
|
111
|
19 |
|
return [$this->partFactory->newCommentPart($parts)]; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|