| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /****************************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2016 Richard Klees <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * This software is licensed under The MIT License. You should have received  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * a copy of the licence along with the code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Lechimp\Dicto\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Lechimp\Dicto\Rules\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Lechimp\Dicto\Rules\Invoke; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Lechimp\Dicto\Rules\DependOn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Lechimp\Dicto\Rules\ContainText; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Lechimp\Dicto\Rules\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Lechimp\Dicto\Rules\Ruleset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Lechimp\Dicto\Variables\Variable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Runtime for one rule definition. A rule definition starts with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Dicto::startDefinition() and ends with Dicto::endDefinition(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * This class provides the functionality that is accessed via the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * Dicto class during the definition. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | class RT { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @var array   $name => $definition | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     private $vars; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * ToDo: I think, this is not necessary and current_var is sufficient. | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @var string|null | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     private $current_var_name; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @var Variable|null | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     private $current_var; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @var Rule[] | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     private $rules; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @var array|null | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     private $config; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @var array<string,Schema> | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     private $known_schemas; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 283 |  |     public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 283 |  |         $this->vars = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 283 |  |         $this->current_var_name = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 283 |  |         $this->current_var = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 283 |  |         $this->rules = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 283 |  |         $this->config = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // TODO: This needs to go somewhere else and must be more dynamic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 283 |  |         $d = new DependOn(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 283 |  |         $i = new Invoke(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 283 |  |         $c = new ContainText(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         // TODO: There need to be checks on the name then as well. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 283 |  |         $this->known_schemas = array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 283 |  |             ( $d->name() => $d | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 283 |  |             , $i->name() => $i | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 283 |  |             , $c->name() => $c | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 283 |  |             ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 | 283 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * Set a configuration to be used with the rules. | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param   array|null  $config | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * @return  null|array | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 277 |  |     public function configuration(array $config = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 277 |  |         if ($config === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 277 |  |             return $this->config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 3 |  |         if ($this->config !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             throw new \RuntimeException("Already set configuration."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  |         $this->config = $config; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * Get the rule set that was currently created. | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * @return  Ruleset | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 277 |  |     public function ruleset() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 277 |  |         $this->maybe_save_current_var(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 277 |  |         return new Ruleset($this->vars, $this->rules); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * Define a new variable or reference an already defined variable to define | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * a rule. | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * @throws  \RuntimeException   if previous variable declaration was not finished | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * @return  NewVar|RuleVar | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 278 |  |     public function variable($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 278 |  |         $this->maybe_save_current_var(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 278 |  |         if (!array_key_exists($name, $this->vars)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 278 |  |             $this->current_var_name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 278 |  |             return new Fluid\NewVar($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 147 |  |             $this->throw_on_missing_var($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 147 |  |             return new Fluid\RuleVar($this, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |      * Define a only-rule. | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * @return  Fluid\Only | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 36 |  |     public function only() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 36 |  |         $this->maybe_save_current_var(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 36 |  |         return new Fluid\Only($this); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      * Save the currently defined variable, if there is any. | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * @throws  \RuntimeException   if previous variable declaration was not finished | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 281 |  |     protected function maybe_save_current_var() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 281 |  |         if ($this->current_var_name !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 276 |  |             if ($this->current_var === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 1 |  |                 throw new \RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 1 |  |                         "The declaration of ".$this->current_var_name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |                         " was not finished."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 275 |  |             $this->vars[$this->current_var_name] = $this->current_var; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 275 |  |             $this->current_var_name = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 275 |  |             $this->current_var = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 275 |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 153 | 281 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |      * Get the name of the current var. | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |      * @return  string | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 276 |  |     public function get_current_var_name() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 276 |  |         assert('is_string($this->current_var_name)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 276 |  |         return $this->current_var_name; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |      * Get the name of the current var. | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |      * @return  Variables\Variable | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 82 |  |     public function get_current_var() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 82 |  |         assert('$this->current_var !== null'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 82 |  |         return $this->current_var; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |      * Get an already defined variable. | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |      * @param   string  $name | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |      * @return  Variables\Variable | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 257 |  |     public function get_var($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 257 |  |         assert('array_key_exists($name, $this->vars)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 257 |  |         return $this->vars[$name]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 184 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |      * Announce what the current variable is atm. | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |      * @param   Variable  $var | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 276 |  |     public function current_var_is(Variable $var) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 276 |  |         assert('$this->current_var_name !== null'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 276 |  |         $this->current_var = $var; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 194 | 276 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |      * Throws a RuntimeException on missing variable $var. | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 258 |  |     public function throw_on_missing_var($var) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 258 |  |         if (!array_key_exists($var, $this->vars)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 | 1 |  |             throw new \RuntimeException("Missing variable $var"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 203 | 257 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 205 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 206 |  |  |      * Add a rule to the set. | 
            
                                                                        
                            
            
                                    
            
            
                | 207 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 182 |  |     public function add_rule(Rule $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 182 |  |         $this->rules[] = $rule; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 210 | 182 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 212 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 213 |  |  |      * Try to get a rule schema by name. | 
            
                                                                        
                            
            
                                    
            
            
                | 214 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 215 |  |  |      * @param   string  $name | 
            
                                                                        
                            
            
                                    
            
            
                | 216 |  |  |      * @return  Schema|null | 
            
                                                                        
                            
            
                                    
            
            
                | 217 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 | 183 |  |     public function get_schema($name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 | 183 |  |         assert('is_string($name)'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 183 |  |         if (array_key_exists($name, $this->known_schemas)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 | 183 |  |             return $this->known_schemas[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         return null; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 224 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 225 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 226 |  |  |  |