Mistralys /
mailcode
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * File containing the {@see Mailcode_Parser_Statement} class. |
||
| 4 | * |
||
| 5 | * @package Mailcode |
||
| 6 | * @subpackage Parser |
||
| 7 | * @see Mailcode_Parser_Statement |
||
| 8 | */ |
||
| 9 | |||
| 10 | declare(strict_types=1); |
||
| 11 | |||
| 12 | namespace Mailcode; |
||
| 13 | |||
| 14 | use AppUtils\OperationResult; |
||
| 15 | use Mailcode\Parser\Statement\Tokenizer\EventHandler; |
||
| 16 | use Mailcode\Parser\Statement\Tokenizer\SpecialChars; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Mailcode statement parser: parses arbitrary statements |
||
| 20 | * to check for validation issues. |
||
| 21 | * |
||
| 22 | * @package Mailcode |
||
| 23 | * @subpackage Parser |
||
| 24 | * @author Sebastian Mordziol <[email protected]> |
||
| 25 | */ |
||
| 26 | class Mailcode_Parser_Statement |
||
| 27 | { |
||
| 28 | public const VALIDATION_EMPTY = 48801; |
||
| 29 | public const VALIDATION_UNQUOTED_STRING_LITERALS = 48802; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected string $statement; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var OperationResult |
||
| 38 | */ |
||
| 39 | protected OperationResult $result; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Mailcode_Parser_Statement_Tokenizer |
||
| 43 | */ |
||
| 44 | protected Mailcode_Parser_Statement_Tokenizer $tokenizer; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Mailcode_Parser_Statement_Info|NULL |
||
| 48 | */ |
||
| 49 | protected ?Mailcode_Parser_Statement_Info $info = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | protected bool $freeform = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Mailcode_Commands_Command|null |
||
| 58 | */ |
||
| 59 | private ?Mailcode_Commands_Command $sourceCommand = null; |
||
| 60 | |||
| 61 | public function __construct(string $statement, bool $freeform=false, ?Mailcode_Commands_Command $sourceCommand=null) |
||
| 62 | { |
||
| 63 | $this->sourceCommand = $sourceCommand; |
||
| 64 | $this->statement = $this->prepareStatement($statement); |
||
| 65 | $this->result = new OperationResult($this); |
||
| 66 | $this->tokenizer = new Mailcode_Parser_Statement_Tokenizer($this); |
||
| 67 | $this->freeform = $freeform; |
||
| 68 | |||
| 69 | $this->validate(); |
||
| 70 | } |
||
| 71 | |||
| 72 | private function prepareStatement(string $statement) : string |
||
| 73 | { |
||
| 74 | return str_replace( |
||
| 75 | array( |
||
| 76 | SpecialChars::PLACEHOLDER_BRACKET_OPEN, |
||
| 77 | SpecialChars::PLACEHOLDER_BRACKET_CLOSE |
||
| 78 | ), |
||
| 79 | array( |
||
| 80 | '\{', |
||
| 81 | '\}' |
||
| 82 | ), |
||
| 83 | $statement |
||
| 84 | ); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function getSourceCommand() : ?Mailcode_Commands_Command |
||
| 88 | { |
||
| 89 | return $this->sourceCommand; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function getStatementString() : string |
||
| 93 | { |
||
| 94 | return $this->statement; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function isValid() : bool |
||
| 98 | { |
||
| 99 | $this->validate(); |
||
| 100 | |||
| 101 | return $this->result->isValid(); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function getValidationResult() : OperationResult |
||
| 105 | { |
||
| 106 | return $this->result; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function getEventHandler() : EventHandler |
||
| 110 | { |
||
| 111 | return $this->tokenizer->getEventHandler(); |
||
| 112 | } |
||
| 113 | |||
| 114 | public function getInfo() : Mailcode_Parser_Statement_Info |
||
| 115 | { |
||
| 116 | if($this->info instanceof Mailcode_Parser_Statement_Info) |
||
| 117 | { |
||
| 118 | return $this->info; |
||
| 119 | } |
||
| 120 | |||
| 121 | $this->info = new Mailcode_Parser_Statement_Info($this->tokenizer); |
||
| 122 | |||
| 123 | return $this->info; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 124 | } |
||
| 125 | |||
| 126 | protected function validate() : void |
||
| 127 | { |
||
| 128 | if($this->freeform) |
||
| 129 | { |
||
| 130 | return; |
||
| 131 | } |
||
| 132 | |||
| 133 | if(!$this->tokenizer->hasTokens()) |
||
| 134 | { |
||
| 135 | $this->result->makeError( |
||
| 136 | t('Empty statement'), |
||
| 137 | self::VALIDATION_EMPTY |
||
| 138 | ); |
||
| 139 | |||
| 140 | return; |
||
| 141 | } |
||
| 142 | |||
| 143 | $unknown = $this->tokenizer->getFirstUnknown(); |
||
| 144 | |||
| 145 | if($unknown) |
||
| 146 | { |
||
| 147 | $this->result->makeError( |
||
| 148 | t('Unquoted string literal found:').' ('.htmlspecialchars($unknown->getMatchedText()).')', |
||
| 149 | self::VALIDATION_UNQUOTED_STRING_LITERALS |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | public function getNormalized() : string |
||
| 155 | { |
||
| 156 | return $this->tokenizer->getNormalized(); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Creates a copy of the parameters, with the exact |
||
| 161 | * same parameters and source command, if any. |
||
| 162 | * |
||
| 163 | * @return Mailcode_Parser_Statement |
||
| 164 | */ |
||
| 165 | public function copy() : Mailcode_Parser_Statement |
||
| 166 | { |
||
| 167 | return new Mailcode_Parser_Statement( |
||
| 168 | $this->getNormalized(), |
||
| 169 | $this->freeform, |
||
| 170 | $this->sourceCommand |
||
| 171 | ); |
||
| 172 | } |
||
| 173 | } |
||
| 174 |