| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Todd Burry <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright 2009-2017 Vanilla Forums Inc. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace Garden\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * An class for collecting validation errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Validation { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $errors = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $mainMessage = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $mainStatus = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var bool Whether or not fields should be translated. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $translateFieldNames = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Add an error. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $field The name and path of the field to add or an empty string if this is a global error. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param string $error The message code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param int|array $options An array of additional information to add to the error entry or a numeric error code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 74 |  |     public function addError($field, $error, $options = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 74 |  |         if (empty($error)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             throw new \InvalidArgumentException('The error code cannot be empty.', 500); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 74 |  |         $fieldKey = $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 74 |  |         $row = ['field' => null, 'code' => null, 'path' => null, 'index' => null]; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         // Split the field out into a path, field, and possible index. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 74 |  |         if (($pos = strrpos($field, '.')) !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 5 |  |             $row['path'] = substr($field, 0, $pos); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 5 |  |             $field = substr($field, $pos + 1); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 74 |  |         if (preg_match('`^([^[]+)\[(\d+)\]$`', $field, $m)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  |             $row['index'] = (int)$m[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  |             $field = $m[1]; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 74 |  |         $row['field'] = $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 74 |  |         $row['code'] = $error; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $row = array_filter($row, function ($v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 74 |  |             return $v !== null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 74 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 74 |  |         if (is_array($options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 71 |  |             $row += $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 4 |  |         } elseif (is_int($options)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |             $row['status'] = $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 74 |  |         $this->errors[$fieldKey][] = $row; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 74 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Get or set the error status code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * The status code is an http response code and should be of the 4xx variety. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return int Returns the current status code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 67 |  |     public function getStatus() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 67 |  |         if ($status = $this->getMainStatus()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |             return $status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 66 |  |         if ($this->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |             return 200; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         // There was no status so loop through the errors and look for the highest one. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 65 |  |         $maxStatus = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 65 |  |         foreach ($this->getRawErrors() as $error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 65 |  |             if (isset($error['status']) && $error['status'] > $maxStatus) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 62 |  |                 $maxStatus = $error['status']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 65 |  |         return $maxStatus?: 400; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Get the message for this exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return string Returns the exception message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 68 |  |     public function getMessage() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 68 |  |         if ($message = $this->getMainMessage()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |             return $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 67 |  |         $sentence = $this->translate('%s.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         // Generate the message by concatenating all of the errors together. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 67 |  |         $messages = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 67 |  |         foreach ($this->getRawErrors() as $error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 67 |  |             $message = $this->getErrorMessage($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 67 |  |             if (preg_match('`\PP$`u', $message)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |                 $message = sprintf($sentence, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 67 |  |             $messages[] = $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 67 |  |         return implode(' ', $messages); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * Gets all of the errors as a flat array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * The errors are internally stored indexed by field. This method flattens them for final error returns. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return array Returns all of the errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 4 |  |     public function getErrors() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 4 |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 4 |  |         foreach ($this->getRawErrors() as $error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 4 |  |             $result[] = $this->formatError($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 4 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * Get the errors for a specific field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param string $field The full path to the field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @return array Returns an array of errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 6 |  |     public function getFieldErrors($field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 6 |  |         if (empty($this->errors[$field])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 6 |  |             $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 6 |  |             foreach ($this->errors[$field] as $error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 6 |  |                 $result[] = $this->formatError($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 6 |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * Gets all of the errors as a flat array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * The errors are internally stored indexed by field. This method flattens them for final error returns. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @return \Traversable Returns all of the errors. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 168 | 82 |  |     protected function getRawErrors() { | 
            
                                                                        
                            
            
                                    
            
            
                | 169 | 82 |  |         foreach ($this->errors as $errors) { | 
            
                                                                        
                            
            
                                    
            
            
                | 170 | 71 |  |             foreach ($errors as $error) { | 
            
                                                                        
                            
            
                                    
            
            
                | 171 | 71 |  |                 yield $error; | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 174 | 82 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * Check whether or not the validation is free of errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * @return bool Returns true if there are no errors, false otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 118 |  |     public function isValid() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 118 |  |         return empty($this->errors); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      * Check whether or not a particular field is has errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      * @param string $field The name of the field to check for validity. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * @return bool Returns true if the field has no errors, false otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 | 76 |  |     public function isValidField($field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 76 |  |         $result = empty($this->errors[$field]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 | 76 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * Get the error count, optionally for a particular field. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * @param string $field The name of a field or an empty string for all errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @return int Returns the error count. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 48 |  |     public function getErrorCount($field = '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 48 |  |         if (empty($field)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 | 17 |  |             return iterator_count($this->getRawErrors()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 31 |  |         } elseif (empty($this->errors[$field])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 31 |  |             return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |             return count($this->errors[$field]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |      * Get the error message for an error row. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |      * @param array $error The error row. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |      * @return string Returns a formatted/translated error message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 | 69 |  |     private function getErrorMessage(array $error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 | 69 |  |         if (isset($error['messageCode'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 63 |  |             $messageCode = $error['messageCode']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 | 7 |  |         } elseif (isset($error['message'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |             return $error['message']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 | 7 |  |             $messageCode = $error['code']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |         // Massage the field name for better formatting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 | 69 |  |         if (!$this->getTranslateFieldNames()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 | 68 |  |             $field = (!empty($error['path']) ? ($error['path'][0] !== '[' ?: 'item').$error['path'].'.' : '').$error['field']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 | 68 |  |             $field = $field ?: (isset($error['index']) ? 'item' : 'value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 | 68 |  |             if (isset($error['index'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 | 3 |  |                 $field .= '['.$error['index'].']'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 | 68 |  |             $error['field'] = '@'.$field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 | 2 |  |         } elseif (isset($error['index'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |             if (empty($error['field'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |                 $error['field'] = '@'.$this->formatMessage('item {index}', $error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |                 $error['field'] = '@'.$this->formatMessage('{field} at position {index}', $error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 | 2 |  |         } elseif (empty($error['field'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 | 1 |  |             $error['field'] = 'value'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 | 69 |  |         $msg = $this->formatMessage($messageCode, $error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 | 69 |  |         return $msg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |      * Expand and translate a message format against an array of values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |      * @param string $format The message format. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |      * @param array $context The context arguments to apply to the message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |      * @return string Returns a formatted string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 | 69 |  |     private function formatMessage($format, $context = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 | 69 |  |         $format = $this->translate($format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 | 69 |  |         $msg = preg_replace_callback('`({[^{}]+})`', function ($m) use ($context) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 | 66 |  |             $args = array_filter(array_map('trim', explode(',', trim($m[1], '{}')))); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 | 66 |  |             $field = array_shift($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 | 66 |  |             return $this->formatField(isset($context[$field]) ? $context[$field] : null, $args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 | 69 |  |         }, $format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 | 69 |  |         return $msg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |      * Translate an argument being placed in an error message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |      * @param mixed $value The argument to translate. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |      * @param array $args Formatting arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |      * @return string Returns the translated string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 | 66 |  |     private function formatField($value, array $args = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 | 66 |  |         if ($value === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 | 1 |  |             $r = $this->translate('null'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 | 66 |  |         } elseif ($value === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |             $r = $this->translate('true'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 | 66 |  |         } elseif ($value === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |             $r = $this->translate('false'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 | 66 |  |         } elseif (is_string($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 | 65 |  |             $r = $this->translate($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 | 9 |  |         } elseif (is_numeric($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 | 5 |  |             $r = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 | 4 |  |         } elseif (is_array($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 | 4 |  |             $argArray = array_map([$this, 'formatField'], $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 | 4 |  |             $r = implode(', ', $argArray); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |         } elseif ($value instanceof \DateTimeInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |             $r = $value->format('c'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |             $r = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 | 66 |  |         $format = array_shift($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |         switch ($format) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 | 66 |  |             case 'plural': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 | 6 |  |                 $singular = array_shift($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 | 6 |  |                 $plural = array_shift($args) ?: $singular.'s'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 | 6 |  |                 $count = is_array($value) ? count($value) : $value; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 | 6 |  |                 $r = $count == 1 ? $singular : $plural; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 | 6 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 | 66 |  |         return (string)$r; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |      * Translate a string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |      * This method doesn't do any translation itself, but is meant for subclasses wanting to add translation ability to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |      * this class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |      * @param string $str The string to translate. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |      * @return string Returns the translated string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 | 70 |  |     public function translate($str) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 | 70 |  |         if (substr($str, 0, 1) === '@') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |             // This is a literal string that bypasses translation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 | 63 |  |             return substr($str, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 | 69 |  |             return $str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |      * Merge another validation object with this one. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |      * @param Validation $validation The validation object to merge. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |      * @param string $name The path to merge to. Use this parameter when the validation object is meant to be a subset of this one. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 | 1 |  |     public function merge(Validation $validation, $name = '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 | 1 |  |         $paths = $validation->errors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 | 1 |  |         foreach ($paths as $path => $errors) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 | 1 |  |             foreach ($errors as $error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 | 1 |  |                 if (!empty($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 | 1 |  |                     $fullPath = "{$name}.{$path}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 | 1 |  |                     $this->addError($fullPath, $error['code'], $error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |      * Get the main error message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |      * If set, this message will be returned as the error message. Otherwise the message will be set from individual | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |      * errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |      * @return string Returns the main message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 | 68 |  |     public function getMainMessage() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 | 68 |  |         return $this->mainMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |      * Set the main error message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |      * @param string $message The new message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 | 1 |  |     public function setMainMessage($message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 | 1 |  |         $this->mainMessage = $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |      * Get the main status. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |      * @return int Returns an HTTP response code or zero to indicate it should be calculated. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 | 67 |  |     public function getMainStatus() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 | 67 |  |         return $this->mainStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |      * Set the main status. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |      * @param int $status An HTTP response code or zero. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 | 1 |  |     public function setMainStatus($status) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 | 1 |  |         $this->mainStatus = $status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |      * Whether or not fields should be translated. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |      * @return bool Returns **true** if field names are translated or **false** otherwise. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 | 69 |  |     public function getTranslateFieldNames() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 | 69 |  |         return $this->translateFieldNames; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |      * Set whether or not fields should be translated. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |      * @param bool $translate Whether or not fields should be translated. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 | 2 |  |     public function setTranslateFieldNames($translate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 | 2 |  |         $this->translateFieldNames = $translate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |      * @param $error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 | 10 |  |     private function formatError($error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 | 10 |  |         $row = array_intersect_key( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |             $error, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 | 10 |  |             ['field' => 1, 'path' => 1, 'index' => 1, 'code' => 1] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 | 10 |  |         $row['message'] = $this->getErrorMessage($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 | 10 |  |         return $row; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 422 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 423 |  |  |  | 
            
                        
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.