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\HeaderPartFactory; |
||
14 | use ZBateson\MailMimeParser\Header\Part\ParameterPart; |
||
15 | |||
16 | /** |
||
17 | * Reads headers separated into parameters consisting of an optional main value, |
||
18 | * and subsequent name/value pairs - for example text/html; charset=utf-8. |
||
19 | * |
||
20 | * A ParameterConsumerService's parts are separated by a semi-colon. Its |
||
21 | * name/value pairs are separated with an '=' character. |
||
22 | * |
||
23 | * Parts may be mime-encoded entities, or RFC-2231 split/encoded parts. |
||
24 | * Additionally, a value can be quoted and comments may exist. |
||
25 | * |
||
26 | * Actual processing of parameters is done in ParameterNameValueConsumerService, |
||
27 | * with ParameterConsumerService processing all collected parts into split |
||
28 | * parameter parts as necessary. |
||
29 | * |
||
30 | * @author Zaahid Bateson |
||
31 | */ |
||
32 | class ParameterConsumerService extends AbstractGenericConsumerService |
||
33 | { |
||
34 | use QuotedStringMimeLiteralPartTokenSplitPatternTrait; |
||
35 | |||
36 | 19 | public function __construct( |
|
37 | LoggerInterface $logger, |
||
38 | HeaderPartFactory $partFactory, |
||
39 | ParameterNameValueConsumerService $parameterNameValueConsumerService, |
||
40 | CommentConsumerService $commentConsumerService, |
||
41 | QuotedStringConsumerService $quotedStringConsumerService |
||
42 | ) { |
||
43 | 19 | parent::__construct( |
|
44 | 19 | $logger, |
|
45 | 19 | $partFactory, |
|
46 | 19 | [$parameterNameValueConsumerService, $commentConsumerService, $quotedStringConsumerService] |
|
47 | 19 | ); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Disables advancing for start tokens. |
||
52 | */ |
||
53 | 121 | protected function advanceToNextToken(Iterator $tokens, bool $isStartToken) : static |
|
54 | { |
||
55 | 121 | if ($isStartToken) { |
|
56 | 121 | return $this; |
|
57 | } |
||
58 | 121 | parent::advanceToNextToken($tokens, $isStartToken); |
|
59 | 121 | return $this; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Post processing involves looking for split parameter parts with matching |
||
64 | * names and combining them into a SplitParameterPart, and otherwise |
||
65 | * returning ParameterParts from ParameterNameValueConsumer as-is. |
||
66 | * |
||
67 | * @param IHeaderPart[] $parts The parsed parts. |
||
68 | * @return IHeaderPart[] Array of resulting final parts. |
||
69 | */ |
||
70 | 121 | protected function processParts(array $parts) : array |
|
71 | { |
||
72 | 121 | $factory = $this->partFactory; |
|
73 | 121 | return \array_values(\array_map( |
|
74 | 121 | function($partsArray) use ($factory) { |
|
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
75 | 121 | if (\count($partsArray) > 1) { |
|
76 | 8 | return $factory->newSplitParameterPart($partsArray); |
|
77 | } |
||
78 | 121 | return $partsArray[0]; |
|
79 | 121 | }, |
|
80 | 121 | \array_merge_recursive(...\array_map( |
|
81 | 121 | function($p) { |
|
0 ignored issues
–
show
|
|||
82 | // if $p->getIndex is non-null, it's a split-parameter part |
||
83 | // and an array of one element consisting of name => ParameterPart |
||
84 | // is returned, which is then merged into name => array-of-parameter-parts |
||
85 | // or ';' object_id . ';' for non-split parts with a value of a single |
||
86 | // element array of [ParameterPart] |
||
87 | 121 | if ($p instanceof ParameterPart && $p->getIndex() !== null) { |
|
88 | 9 | return [\strtolower($p->getName()) => [$p]]; |
|
89 | } |
||
90 | 121 | return [';' . \spl_object_id($p) . ';' => [$p]]; |
|
91 | 121 | }, |
|
92 | 121 | $parts |
|
93 | 121 | )) |
|
94 | 121 | )); |
|
95 | } |
||
96 | } |
||
97 |