| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\IcalendarGenerator\Builders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Spatie\IcalendarGenerator\ComponentPayload; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Spatie\IcalendarGenerator\PropertyTypes\PropertyType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | final class ComponentBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** @var \Spatie\IcalendarGenerator\ComponentPayload */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $componentPayload; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public function __construct(ComponentPayload $componentPayload) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $this->componentPayload = $componentPayload; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function build(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         return implode("\r\n", $this->buildComponent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function buildComponent(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $lines[] = "BEGIN:V{$this->componentPayload->getType()}"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $lines = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $lines, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $this->buildProperties(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $this->buildSubComponents() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $lines[] = "END:V{$this->componentPayload->getType()}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return $lines; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private function buildProperties(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $lines = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         foreach ($this->componentPayload->getProperties() as $key => $property) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $propertyBuilder = new PropertyBuilder($property); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $lines = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 $lines, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $this->chipLine($propertyBuilder->build()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $this->buildAliasesForProperty($property, $propertyBuilder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $lines; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     private function buildAliasesForProperty( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         PropertyType $property, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         PropertyBuilder $propertyBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     ): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $properties = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         foreach ($this->componentPayload->getAliasesForProperty($property->getName()) as $alias) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $properties = array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $properties, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $this->chipLine($propertyBuilder->build($alias)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $properties; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     private function buildSubComponents(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $lines = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         /** @var \Spatie\IcalendarGenerator\Components\Component $component */ | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         foreach ($this->componentPayload->getSubComponents() as $component) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             $builder = new ComponentBuilder($component->getPayload()); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             $lines = array_merge( | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 $lines, | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 $builder->buildComponent() | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         return $lines; | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     private function chipLine(string $line): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $chippedLines = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         while (strlen($line) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             if (strlen($line) > 75) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $chippedLines[] = mb_strcut($line, 0, 75, 'utf-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $line = ' '.mb_strcut($line, 75, strlen($line), 'utf-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $chippedLines[] = $line; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return $chippedLines; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                        
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.