| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @category  Acsi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package   Acsi\ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright 2012 Bram Gerritsen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @version   SVN: $Id$ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace StrokerForm\Renderer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Zend\Form\Element\MultiCheckbox; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Zend\Form\Element\Radio; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Zend\Form\ElementInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Zend\Form\FieldsetInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Zend\Form\FormInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Zend\InputFilter\InputFilterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Zend\InputFilter\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Zend\Validator\Csrf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Zend\Validator\NotEmpty; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Zend\Validator\ValidatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Zend\View\Renderer\PhpRenderer as View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | abstract class AbstractValidateRenderer extends AbstractRenderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Executed before the ZF2 view helper renders the element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param string                          $formAlias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param \Zend\View\Renderer\PhpRenderer $view | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param \Zend\Form\FormInterface        $form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param array                           $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return FormInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function preRenderForm($formAlias, View $view, FormInterface $form = null, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->setOptions($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if ($form === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $form = $this->getFormManager()->get($formAlias); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $inputFilter = $form->getInputFilter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         /** @var $element \Zend\Form\Element */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $validators = $this->extractValidatorsForForm($form, $inputFilter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         foreach ($validators as $validator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $element = $validator['element']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             foreach ($validator['validators'] as $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 if ($val['instance'] instanceof Csrf) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $this->addValidationAttributesForElement($formAlias, $element, $val['instance']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Iterate through all the elements and retrieve their validators | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param FieldsetInterface    $formOrFieldset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param InputFilterInterface $inputFilter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function extractValidatorsForForm(FieldsetInterface $formOrFieldset, InputFilterInterface $inputFilter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $foundValidators = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         foreach ($formOrFieldset->getElements() as $element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $validators = $this->getValidatorsForElement($inputFilter, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if (count($validators) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $foundValidators[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     'element'    => $element, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     'validators' => $validators | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         foreach ($formOrFieldset->getFieldsets() as $key => $fieldset) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             if ($inputFilter->has($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $foundValidators = array_merge($foundValidators, $this->extractValidatorsForForm($fieldset, $inputFilter->get($key))); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return $foundValidators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * Get all validators for a given element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param InputFilterInterface $inputFilter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param ElementInterface     $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function getValidatorsForElement(InputFilterInterface $inputFilter, ElementInterface $element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if ($element->getOption('strokerform-exclude')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         // Check if we are dealing with a fieldset element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (preg_match('/^.*\[(.*)\]$/', $element->getName(), $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $elementName = $matches[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $elementName = $element->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         if (!$inputFilter->has($elementName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $input = $inputFilter->get($elementName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         if (!$input instanceof InputInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->injectNotEmptyValidator($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return $input->getValidatorChain()->getValidators(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * When the input is required we need to inject the NotEmpty validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     protected function injectNotEmptyValidator(InputInterface $input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         if (!$input->isRequired()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $chain = $input->getValidatorChain(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         // Check if NotEmpty validator is already in chain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $validators = $chain->getValidators(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         foreach ($validators as $validator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             if ($validator['instance'] instanceof NotEmpty) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         // Make sure NotEmpty validator is added when input is required | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $chain->prependValidator(new NotEmpty()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * Excecuted before the ZF2 view helper renders the element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param  ElementInterface $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     public function preRenderInputField(ElementInterface $element) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @param  string             $formAlias | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param  ElementInterface   $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @param  ValidatorInterface $validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     abstract protected function addValidationAttributesForElement($formAlias, ElementInterface $element, ValidatorInterface $validator = null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * Get the name of the form element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * @param  ElementInterface $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 177 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |     protected function getElementName(ElementInterface $element) | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |         $elementName = $element->getName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |         if ($element instanceof MultiCheckbox && !$element instanceof Radio) { | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |             $elementName .= '[]'; | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |         return $elementName; | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * Get the classname of the zend validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * @param  ValidatorInterface $validator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     protected function getValidatorClassName(ValidatorInterface $validator = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         $namespaces = explode('\\', get_class($validator)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         return end($namespaces); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 200 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |  | 
            
                        
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.