| 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\Validation\Validator\Internal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Romm\Formz\Condition\Exceptions\InvalidConditionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Romm\Formz\Form\Definition\Field\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Romm\Formz\Form\Definition\FormDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use TYPO3\CMS\Extbase\Error\Error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | class FormDefinitionValidator extends AbstractValidator | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @var FormDefinition | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     protected $definition; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param FormDefinition $definition | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public function isValid($definition) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->definition = $definition; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $this->validateConditionList(); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $this->validateFieldsConditions(); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * Validates each condition of the list, at the root of the form definition. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected function validateConditionList() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         foreach ($this->definition->getConditionList() as $conditionName => $condition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $condition->validateConditionConfiguration($this->definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             } catch (InvalidConditionException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $property = "conditionList.{$conditionName}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 $this->addPropertyError($property, $exception->getMessage(), $exception->getCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * Loops on each field, and validates every condition. | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected function validateFieldsConditions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         foreach ($this->definition->getFields() as $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $this->validateFieldsValidatorsConditions($field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             if (false === $field->hasActivation()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             foreach ($field->getActivation()->getConditions() as $conditionName => $condition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     $condition->validateConditionConfiguration($this->definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 } catch (InvalidConditionException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     $property = "fields.{$field->getName()}.activation.conditions.{$conditionName}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     $this->addPropertyError($property, $exception->getMessage(), $exception->getCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * Loops on each field validator, and validates every condition. | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param Field $field | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     protected function validateFieldsValidatorsConditions(Field $field) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         foreach ($field->getValidators() as $validator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             if (false === $validator->hasActivation()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             foreach ($validator->getActivation()->getConditions() as $conditionName => $condition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                     $condition->validateConditionConfiguration($this->definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 } catch (InvalidConditionException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     $property = "fields.{$field->getName()}.validation.{$validator->getName()}.activation.conditions.{$conditionName}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     $this->addPropertyError($property, $exception->getMessage(), $exception->getCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * @param string $property | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * @param string $message | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @param int    $code | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     protected function addPropertyError($property, $message, $code) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $error = new Error($message, $code); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->result->forProperty($property)->addError($error); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |  |