| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * File containing the {@see \Mailcode\Traits\Commands\Validation\BreakAtTrait} trait. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @see \Mailcode\Traits\Commands\Validation\BreakAtTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @subpackage Validation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @package Mailcode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Mailcode\Traits\Commands\Validation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Mailcode\Commands\CommandException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Mailcode\Commands\ParamsException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Mailcode\Interfaces\Commands\Validation\BreakAtInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Mailcode\Mailcode_Commands_Keywords; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Mailcode\Mailcode_Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Mailcode\Mailcode_Parser_Statement_Tokenizer_Process_StringLiterals; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_Number; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_Variable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Mailcode\Mailcode_Variables_Variable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use function AppUtils\parseVariable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use function Mailcode\t; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * Command validation drop-in: checks for the presence | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * of the `break-at` parameter in the command statement. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @package Mailcode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @subpackage Validation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * @author Olaf Böcker <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * @see BreakAtInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | trait BreakAtTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     private bool $breakAtEnabled = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private ?Mailcode_Parser_Statement_Tokenizer_Token $breakAtToken = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected function validateSyntax_check_break_at(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->breakAtToken = $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             ->requireParams() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ->getInfo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->getTokenByParamName(BreakAtInterface::PARAMETER_NAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if($this->breakAtToken === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $this->breakAtEnabled = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if (!$this->breakAtToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Number && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             !$this->breakAtToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $this->validationResult->makeError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 t('Invalid break-at value:') . ' ' . t('Expected a number or variable.'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 BreakAtInterface::VALIDATION_BREAK_AT_CODE_WRONG_TYPE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->breakAtEnabled = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     public function isBreakAtEnabled(): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         return $this->breakAtEnabled; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function getBreakAtToken(): ?Mailcode_Parser_Statement_Tokenizer_Token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $this->breakAtToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return Mailcode_Variables_Variable|int|NULL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function getBreakAt() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $token = $this->getBreakAtToken(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if($token === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             return $token->getVariable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Number) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             return (int)$token->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             return (int)$token->getText(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param Mailcode_Variables_Variable|number|mixed|NULL $breakAt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @throws CommandException {@see BreakAtInterface::ERROR_INVALID_BREAK_AT_VALUE} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @throws ParamsException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     public function setBreakAt($breakAt) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $info = $this->requireParams()->getInfo(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         if(isset($this->breakAtToken)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $info->removeToken($this->breakAtToken); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->breakAtEnabled = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $this->breakAtToken = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if($breakAt === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $token = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         if(is_numeric($breakAt)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $token = $info->addNumber((string)(int)$breakAt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         else if($breakAt instanceof Mailcode_Variables_Variable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $token = $info->addVariable($breakAt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         if($token !== null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             $info->setParamName($token, BreakAtInterface::PARAMETER_NAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             $this->breakAtEnabled = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $this->breakAtToken = $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         throw new CommandException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             'Invalid break-at value', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 'Expected a number or variable, got: [%s].', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 parseVariable($breakAt)->enableType()->toString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             BreakAtInterface::ERROR_INVALID_BREAK_AT_VALUE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 153 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |  |