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
|
17 |
|
public function __construct(array $sections = []) |
30
|
|
|
{ |
31
|
17 |
|
foreach ($sections as $section) { |
32
|
12 |
|
$this->validateSection($section); |
33
|
|
|
} |
34
|
|
|
|
35
|
13 |
|
$this->sections = $sections; |
36
|
13 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
11 |
|
public function __toString(): string |
42
|
|
|
{ |
43
|
11 |
|
return sprintf( |
44
|
11 |
|
'[%s]', |
45
|
11 |
|
implode(' ', $this->sections) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $sections |
51
|
|
|
* @return SectionList |
52
|
|
|
*/ |
53
|
8 |
|
public static function fromString(string $sections): self |
54
|
|
|
{ |
55
|
8 |
|
$result = preg_match('/^\[(.*?)\]$/', $sections, $matches); |
56
|
8 |
|
if ($result !== 1) { |
57
|
1 |
|
throw new \InvalidArgumentException('No section list'); |
58
|
|
|
} |
59
|
|
|
|
60
|
7 |
|
$sections = array_filter(explode(' ', $matches[1])); |
61
|
7 |
|
for ($i = 0; $i < count($sections); $i++) { |
|
|
|
|
62
|
6 |
|
if ($sections[$i] === 'HEADER.FIELDS' || $sections[$i] === 'HEADER.FIELDS.NOT') { |
63
|
3 |
|
if (!isset($sections[$i + 1][0]) || $sections[$i + 1][0] !== '(') { |
64
|
2 |
|
throw new \InvalidArgumentException( |
65
|
2 |
|
'HEADER.FIELDS or HEADER.FIELDS.NOT requires header-list' |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$appendIndex = $i; |
70
|
1 |
|
$i++; |
71
|
|
|
do { |
72
|
1 |
|
$sections[$appendIndex] .= ' ' . $sections[$i]; |
73
|
1 |
|
unset($sections[$i]); |
74
|
1 |
|
$i++; |
75
|
|
|
|
76
|
1 |
|
if (substr($sections[$appendIndex], -1) === ')') { |
77
|
1 |
|
break; |
78
|
|
|
} |
79
|
1 |
|
} while (isset($sections[$i])); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
5 |
|
return new self($sections); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $section |
88
|
|
|
*/ |
89
|
12 |
|
private function validateSection(string $section): void |
90
|
|
|
{ |
91
|
12 |
|
if ($section === '') { |
92
|
1 |
|
throw new \InvalidArgumentException('Empty section'); |
93
|
|
|
} |
94
|
|
|
|
95
|
11 |
|
if (isset(self::RFC_3501_SECTION_FIXED[$section])) { |
96
|
5 |
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
6 |
|
if (preg_match('/^HEADER.FIELDS(\.NOT)? \((.*?)\)$/', $section) === 1) { |
100
|
3 |
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
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: