| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Respect/Validation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Alexandre Gomes Gaigalas <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the "LICENSE.md" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Respect\Validation\Exceptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function in_array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use function is_numeric; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function Respect\Stringifier\stringify; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class ValidationException extends InvalidArgumentException implements ExceptionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const MODE_DEFAULT = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     const MODE_NEGATIVE = 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     const STANDARD = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public static $defaultTemplates = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         self::MODE_DEFAULT => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             self::STANDARD => 'Data validation failed for %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         self::MODE_NEGATIVE => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             self::STANDARD => 'Data validation failed for %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $id = 'validation'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected $mode = self::MODE_DEFAULT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $name = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $template = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected $params = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $customTemplate = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 224 |  |     public static function format($template, array $vars = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 224 |  |         return preg_replace_callback( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 224 |  |             '/{{(\w+)}}/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 224 |  |             function ($match) use ($vars) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 222 |  |                 if (!isset($vars[$match[1]])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |                     return $match[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 222 |  |                 $value = $vars[$match[1]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 222 |  |                 if ('name' == $match[1] && is_string($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 218 |  |                     return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 46 |  |                 return ValidationException::stringify($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 224 |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 224 |  |             $template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 206 |  |     public static function stringify($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 206 |  |         return stringify($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |     public function __toString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |         return $this->getMainMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 166 |  |     public function chooseTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 166 |  |         return key(static::$defaultTemplates[$this->mode]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 219 |  |     public function configure($name, array $params = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 219 |  |         $this->setName($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 219 |  |         $this->setId($this->guessId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 219 |  |         $this->setParams($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 219 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 221 |  |     public function getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 221 |  |         return $this->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 15 |  |     protected function hasName(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 15 |  |         $name = $this->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 15 |  |         if (is_numeric($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 3 |  |             return (bool) (float) $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 15 |  |         return !in_array($name, ['`FALSE`', '`NULL`', '`{ }`', '""', '']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 9 |  |     public function getId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 9 |  |         return $this->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 221 |  |     public function getMainMessage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 221 |  |         $vars = $this->getParams(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 221 |  |         $vars['name'] = $this->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 221 |  |         $template = $this->getTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 221 |  |         if (isset($vars['translator']) && is_callable($vars['translator'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 3 |  |             $template = call_user_func($vars['translator'], $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 221 |  |         return static::format($template, $vars); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 144 |  |     public function getParam($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 144 |  |         return $this->hasParam($name) ? $this->params[$name] : false; | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 221 |  |     public function getParams() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 221 |  |         return $this->params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 221 |  |     public function getTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 221 |  |         if (!empty($this->template)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 170 |  |             return $this->template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 221 |  |         return $this->template = $this->buildTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 144 |  |     public function hasParam($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 144 |  |         return isset($this->params[$name]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 219 |  |     public function setId($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 219 |  |         $this->id = $id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 219 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 219 |  |     public function setName($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 219 |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 219 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 85 |  |     public function setMode($mode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 85 |  |         $this->mode = $mode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 85 |  |         $this->template = $this->buildTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 85 |  |         $this->buildMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 85 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 3 |  |     public function setParam($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 3 |  |         $this->params[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 3 |  |         $this->buildMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 3 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 219 |  |     public function setParams(array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 219 |  |         foreach ($params as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 217 |  |             $this->params[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 219 |  |         $this->buildMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 219 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 117 |  |     public function hasCustomTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 117 |  |         return true === $this->customTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 9 |  |     public function setTemplate($template) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 9 |  |         $this->customTemplate = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 9 |  |         $this->template = $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 9 |  |         $this->buildMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 | 9 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 | 219 |  |     private function buildMessage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 219 |  |         $this->message = $this->getMainMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 | 219 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 221 |  |     protected function buildTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 221 |  |         $templateKey = $this->chooseTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 | 221 |  |         return static::$defaultTemplates[$this->mode][$templateKey]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 | 219 |  |     public function guessId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 | 219 |  |         if (!empty($this->id) && $this->id != 'validation') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 | 10 |  |             return $this->id; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 | 219 |  |         $pieces = explode('\\', get_called_class()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 | 219 |  |         $exceptionClassShortName = end($pieces); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 | 219 |  |         $ruleClassShortName = str_replace('Exception', '', $exceptionClassShortName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 | 219 |  |         $ruleName = lcfirst($ruleClassShortName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 | 219 |  |         return $ruleName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 229 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 230 |  |  |  |