|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Genkgo\Mail\Protocol\Imap\MessageData; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class SectionList |
|
8
|
|
|
* @package Genkgo\Mail\Protocol\Imap\MessageData |
|
9
|
|
|
*/ |
|
10
|
|
|
final class SectionList |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
private CONST RFC_3501_SECTION_FIXED = [ |
|
16
|
|
|
'HEADER' => true, |
|
17
|
|
|
'TEXT' => true, |
|
18
|
|
|
]; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $sections = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* SectionList constructor. |
|
27
|
|
|
* @param array $sections |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(array $sections = []) |
|
30
|
|
|
{ |
|
31
|
|
|
foreach ($sections as $section) { |
|
32
|
|
|
$this->validateSection($section); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this->sections = $sections; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __toString(): string |
|
42
|
|
|
{ |
|
43
|
|
|
return sprintf( |
|
44
|
|
|
'[%s]', |
|
45
|
|
|
implode(' ', $this->sections) |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $sections |
|
51
|
|
|
* @return SectionList |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function fromString(string $sections): self |
|
54
|
|
|
{ |
|
55
|
|
|
$result = preg_match('/^\[(.*?)\]$/', $sections, $matches); |
|
56
|
|
|
if ($result !== 1) { |
|
57
|
|
|
throw new \InvalidArgumentException('No section list'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$sections = array_filter(explode(' ', $matches[1])); |
|
61
|
|
|
for ($i = 0; $i < count($sections); $i++) { |
|
|
|
|
|
|
62
|
|
|
if ($sections[$i] === 'HEADER.FIELDS' || $sections[$i] === 'HEADER.FIELDS.NOT') { |
|
63
|
|
|
if (!isset($sections[$i + 1][0]) || $sections[$i + 1][0] !== '(') { |
|
64
|
|
|
throw new \InvalidArgumentException( |
|
65
|
|
|
'HEADER.FIELDS or HEADER.FIELDS.NOT requires header-list' |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$appendIndex = $i; |
|
70
|
|
|
$i++; |
|
71
|
|
|
do { |
|
72
|
|
|
$sections[$appendIndex] .= ' ' . $sections[$i]; |
|
73
|
|
|
unset($sections[$i]); |
|
74
|
|
|
$i++; |
|
75
|
|
|
|
|
76
|
|
|
if (substr($sections[$appendIndex], -1) === ')') { |
|
77
|
|
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
} while (isset($sections[$i])); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return new self($sections); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $section |
|
88
|
|
|
*/ |
|
89
|
|
|
private function validateSection(string $section): void |
|
90
|
|
|
{ |
|
91
|
|
|
if ($section === '') { |
|
92
|
|
|
throw new \InvalidArgumentException('Empty section'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (isset(self::RFC_3501_SECTION_FIXED[$section])) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if (preg_match('/^HEADER.FIELDS(\.NOT)? \((.*?)\)$/', $section) === 1) { |
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
throw new \InvalidArgumentException('Invalid section item ' . $section); |
|
104
|
|
|
} |
|
105
|
|
|
} |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: