| 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 | 15 |  |     public function __construct(array $sections = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 15 |  |         foreach ($sections as $section) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 10 |  |             $this->validateSection($section); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 11 |  |         $this->sections = $sections; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 11 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 10 |  |     public function __toString(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 10 |  |         return sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 10 |  |             '[%s]', | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 10 |  |             implode(' ', $this->sections) | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $sections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return SectionList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 6 |  |     public static function fromString(string $sections): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 6 |  |         $result = preg_match('/^\[(.*?)\]$/', $sections, $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |         if ($result !== 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |             throw new \InvalidArgumentException('No section list'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 5 |  |         $sections = array_filter(explode(' ', $matches[1])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 5 |  |         for ($i = 0; $i < count($sections); $i++) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |             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 | 3 |  |         return new self($sections); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param string $section | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 10 |  |     private function validateSection(string $section): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 10 |  |         if ($section === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |             throw new \InvalidArgumentException('Empty section'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 9 |  |         if (isset(self::RFC_3501_SECTION_FIXED[$section])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 3 |  |             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: