| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * 2017 Romain CANON <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is part of the TYPO3 FormZ project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * It is free software; you can redistribute it and/or modify it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * under the terms of the GNU General Public License, either | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * version 3 of the License, or any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, see: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * http://www.gnu.org/licenses/gpl-3.0.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Romm\Formz\Condition\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Romm\Formz\Condition\Parser\ConditionParserFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Romm\Formz\Condition\Parser\Node\ConditionNode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Romm\Formz\Condition\Parser\Node\NodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Romm\Formz\Condition\Parser\Tree\ConditionTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Romm\Formz\Condition\Parser\Tree\EmptyConditionTree; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Romm\Formz\Form\Definition\Condition\ActivationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Romm\Formz\Form\Definition\Field\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Romm\Formz\Form\Definition\Field\Validation\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Romm\Formz\Form\FormObject\FormObject; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | class ConditionProcessor | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @var FormObject | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     private $formObject; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @var ConditionTree[] | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     private $fieldsTrees = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @var ConditionTree[] | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     private $conditionTrees = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     private $javaScriptFiles = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @param FormObject $formObject | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function __construct(FormObject $formObject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->attachFormObject($formObject); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * Returns the condition tree for a given field instance, giving access to | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * CSS, JavaScript and PHP transpiled results. | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @param Field $field | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @return ConditionTree | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function getActivationConditionTreeForField(Field $field) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $key = $field->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if (false === array_key_exists($key, $this->fieldsTrees)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $this->fieldsTrees[$key] = $field->hasActivation() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 ? $this->getConditionTree($field->getActivation()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 : EmptyConditionTree::get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($field->hasActivation()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $this->fieldsTrees[$key]->injectDependencies($this, $field->getActivation()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return $this->fieldsTrees[$key]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * Returns the condition tree for a given validator instance, giving access | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * to CSS, JavaScript and PHP transpiled results. | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @param Validator $validator | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * @return ConditionTree | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function getActivationConditionTreeForValidator(Validator $validator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $key = $validator->getParentField()->getName() . '->' . $validator->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if (false === array_key_exists($key, $this->conditionTrees)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $this->conditionTrees[$key] = $validator->hasActivation() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 ? $this->getConditionTree($validator->getActivation()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 : EmptyConditionTree::get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if ($validator->hasActivation()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $this->conditionTrees[$key]->injectDependencies($this, $validator->getActivation()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return $this->conditionTrees[$key]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * Function that will calculate all trees from fields and their validators. | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * This is useful to be able to store this instance in cache. | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     public function calculateAllTrees() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $fields = $this->formObject->getDefinition()->getFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         foreach ($fields as $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $this->getActivationConditionTreeForField($field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             foreach ($field->getValidators() as $validator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $this->getActivationConditionTreeForValidator($validator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |      * @param ActivationInterface $activation | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |      * @return ConditionTree | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     protected function getConditionTree(ActivationInterface $activation) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $tree = $this->getNewConditionTreeFromActivation($activation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $tree->alongNodes(function (NodeInterface $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             $this->attachNodeJavaScriptFiles($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         return $tree; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * @param ActivationInterface $activation | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      * @return ConditionTree | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     protected function getNewConditionTreeFromActivation(ActivationInterface $activation) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return ConditionParserFactory::get() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             ->parse($activation); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |      * @param NodeInterface $node | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     protected function attachNodeJavaScriptFiles(NodeInterface $node) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         if ($node instanceof ConditionNode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             $files = $node->getCondition()->getJavaScriptFiles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             foreach ($files as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                 if (false === in_array($file, $this->javaScriptFiles)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                     $this->javaScriptFiles[] = $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |      * @param FormObject $formObject | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     public function attachFormObject(FormObject $formObject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $this->formObject = $formObject; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |      * @return FormObject | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     public function getFormObject() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         return $this->formObject; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     public function getJavaScriptFiles() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         return $this->javaScriptFiles; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 184 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     public function __sleep() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         return ['fieldsTrees', 'conditionTrees', 'javaScriptFiles']; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 193 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |  |