| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * File containing the {@see Mailcode_Commands_Highlighter} class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @package Mailcode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @subpackage Commands | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @see Mailcode_Commands_Highlighter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Mailcode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Base command class with the common functionality for all commands. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package Mailcode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @subpackage Commands | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author Sebastian Mordziol <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class Mailcode_Commands_Normalizer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var Mailcode_Commands_Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $parts = array(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public function __construct(Mailcode_Commands_Command $command) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $this->command = $command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function normalize() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if(!$this->command->isValid()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->parts = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->parts[] = '{'.$this->command->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->addType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->addParams($this->command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->addLogicKeywords(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->parts[] = '}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return implode('', $this->parts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     private function addType() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if(!$this->command->supportsType() || !$this->command->hasType()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->parts[] = ' '.$this->command->getType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     private function addParams(Mailcode_Commands_Command $command) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if(!$command->requiresParameters()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $params = $command->getParams(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if($params === null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->parts[] = ': '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->parts[] = $params->getNormalized(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     private function addLogicKeywords() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if(!$this->command->supportsLogicKeywords()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $keywords = $this->command->getLogicKeywords()->getKeywords(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         foreach($keywords as $keyword) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $this->parts[] = ' '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $this->parts[] = $keyword->getKeywordString(); // e.g. "if variable" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $this->addParams($keyword->getCommand()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  |