| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * File containing the {@see Mailcode_Commands_LogicKeywords_Keyword} class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @package Mailcode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @subpackage Commands | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @see Mailcode_Commands_LogicKeywords_Keyword | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Mailcode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use AppUtils\OperationResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Container for individual sub-commands, along with the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * exact logic keyword string used to append the command.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @package Mailcode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @subpackage Commands | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author Sebastian Mordziol <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class Mailcode_Commands_LogicKeywords_Keyword extends OperationResult | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     const ERROR_CANNOT_GET_INVALID_COMMAND = 60601; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     const ERROR_CANNOT_OVERWRITE_PARAMETERS = 60602; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     * @var Mailcode_Commands_LogicKeywords | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $keywords; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private $matchedString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     private $params = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     private $paramsSet = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     * @var Mailcode_Collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     private $collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function __construct(Mailcode_Commands_LogicKeywords $keywords, string $name, string $matchedString, string $type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->type = $type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->keywords = $keywords; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->matchedString = $matchedString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     * The keyword name, e.g. "and". Always lowercase. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function getName() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $this->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function getType() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this->getType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function getKeywordString() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $string = $this->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if(!empty($this->type)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $string .= ' '.$this->type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     * The full string that was matched in the command's parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     * string. Examples: "and:", "and variable:"... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function getMatchedString() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $this->matchedString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     * Sets the parameters string matching this logic keyword, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     * which is used to build the actual sub-command. Set by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     * the LogicKeywords class instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     * @param string $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     public function setParamsString(string $params) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         if($this->paramsSet) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             throw new Mailcode_Exception( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 'Cannot set parameters twice', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 'The setParamsString() method is only called once by the keywords class, and may not be called again.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 self::ERROR_CANNOT_OVERWRITE_PARAMETERS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $this->params = $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->paramsSet = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $this->createCommand(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     * Retrieves the full command string used to create  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     * the actual command. For opening commands like IF, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     * this includes the closing {end} command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  |     */ | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |     public function getCommandString() : string | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |         $command = $this->keywords->getCommand(); | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |         $string = sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |             '{%s %s: %s}', | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |             $this->keywords->getCommand()->getName(), | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |             $this->type, | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |             $this->params | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         // automatically close opening commands to be able to parse valid strings. | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         if($command instanceof Mailcode_Commands_Command_Type_Opening) | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |             $string .= '{end}'; | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |         return $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     private function createCommand() : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $this->collection = Mailcode::create()->parseString($this->getCommandString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         if(!$this->collection->isValid()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $error = $this->collection->getFirstError(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             $this->makeError($error->getMessage(), $error->getCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |    /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     * Retrieves the command for the keyword. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     * @throws Mailcode_Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     * @return Mailcode_Commands_Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     public function getCommand() : Mailcode_Commands_Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $command = $this->collection->getFirstCommand(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         if($this->collection->isValid() && $command !== null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             return $command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         throw new Mailcode_Exception( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             'Cannot get invalid command', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             'The collection has no commands, meaning the generated command was invalid.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             self::ERROR_CANNOT_GET_INVALID_COMMAND | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 193 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |  |