| @@ -2,7 +2,6 @@ | ||
| 2 | 2 | namespace Comfort\Validator; | 
| 3 | 3 | |
| 4 | 4 | use Comfort\Comfort; | 
| 5 | -use Comfort\Error; | |
| 6 | 5 | use Comfort\Exception\ValidationException; | 
| 7 | 6 | use Comfort\ValidationError; | 
| 8 | 7 | |
| @@ -87,7 +87,7 @@ discard block | ||
| 87 | 87 | } | 
| 88 | 88 | |
| 89 | 89 | return $value; | 
| 90 | -        } catch (ValidationException $validationException) { | |
| 90 | +        }catch (ValidationException $validationException) { | |
| 91 | 91 |              if ($this->toBool) { | 
| 92 | 92 | return false; | 
| 93 | 93 | } | 
| @@ -110,7 +110,7 @@ discard block | ||
| 110 | 110 |      { | 
| 111 | 111 | $this->optional = false; | 
| 112 | 112 | |
| 113 | -        $this->add(function ($value, $nameKey) { | |
| 113 | +        $this->add(function($value, $nameKey) { | |
| 114 | 114 |              if (is_null($value)) { | 
| 115 | 115 |                  $this->createError('required', $value, $nameKey); | 
| 116 | 116 | } | 
| @@ -154,7 +154,7 @@ discard block | ||
| 154 | 154 | */ | 
| 155 | 155 | public function anyOf(array $vals) | 
| 156 | 156 |      { | 
| 157 | -        $this->add(function ($value, $nameKey) use ($vals) { | |
| 157 | +        $this->add(function($value, $nameKey) use ($vals) { | |
| 158 | 158 |              if (!in_array($value, $vals)) { | 
| 159 | 159 |                  return $this->createError('anyof', $value, $nameKey); | 
| 160 | 160 | } | 
| @@ -197,7 +197,7 @@ discard block | ||
| 197 | 197 | */ | 
| 198 | 198 | public function errorMessages(array $errorMessages) | 
| 199 | 199 |      { | 
| 200 | -        $errorMessages = array_map(function ($errorMessage) { | |
| 200 | +        $errorMessages = array_map(function($errorMessage) { | |
| 201 | 201 |              if (is_string($errorMessage)) { | 
| 202 | 202 | $errorMessage = ['message' => $errorMessage]; | 
| 203 | 203 | } | 
| @@ -230,7 +230,7 @@ discard block | ||
| 230 | 230 | |
| 231 | 231 | $errorHandler = $this->errorHandlers[$key]; | 
| 232 | 232 |          if (!array_key_exists('message_formatter', $errorHandler)) { | 
| 233 | -            $messageFormatter = function ($template, $value, $validationValue = null) { | |
| 233 | +            $messageFormatter = function($template, $value, $validationValue = null) { | |
| 234 | 234 | return sprintf($template, $value, $validationValue); | 
| 235 | 235 | }; | 
| 236 | 236 |          } else { | 
| @@ -17,7 +17,7 @@ discard block | ||
| 17 | 17 | |
| 18 | 18 | $this->toBool(false); | 
| 19 | 19 | |
| 20 | -        $this->add(function ($value, $nameKey) { | |
| 20 | +        $this->add(function($value, $nameKey) { | |
| 21 | 21 |              if (!is_array($value)) { | 
| 22 | 22 |                  return $this->createError('array.not_array', $value, $nameKey); | 
| 23 | 23 | } | 
| @@ -53,7 +53,7 @@ discard block | ||
| 53 | 53 | */ | 
| 54 | 54 | public function keys(array $definition) | 
| 55 | 55 |      { | 
| 56 | -        return $this->add(function (&$value) use ($definition) { | |
| 56 | +        return $this->add(function(&$value) use ($definition) { | |
| 57 | 57 | /** | 
| 58 | 58 | * @var string $key | 
| 59 | 59 | * @var AbstractValidator $validator | 
| @@ -87,7 +87,7 @@ discard block | ||
| 87 | 87 | */ | 
| 88 | 88 | public function min($min) | 
| 89 | 89 |      { | 
| 90 | -        return $this->add(function ($value, $nameKey) use ($min) { | |
| 90 | +        return $this->add(function($value, $nameKey) use ($min) { | |
| 91 | 91 |              if (count($value) < $min) { | 
| 92 | 92 |                  return $this->createError('array.min', $value, $nameKey); | 
| 93 | 93 | } | 
| @@ -102,7 +102,7 @@ discard block | ||
| 102 | 102 | */ | 
| 103 | 103 | public function max($max) | 
| 104 | 104 |      { | 
| 105 | -        return $this->add(function ($value, $nameKey) use ($max) { | |
| 105 | +        return $this->add(function($value, $nameKey) use ($max) { | |
| 106 | 106 |              if (count($value) > $max) { | 
| 107 | 107 |                  return $this->createError('array.max', $value, $nameKey); | 
| 108 | 108 | } | 
| @@ -117,7 +117,7 @@ discard block | ||
| 117 | 117 | */ | 
| 118 | 118 | public function length($length) | 
| 119 | 119 |      { | 
| 120 | -        return $this->add(function ($value, $nameKey) use ($length) { | |
| 120 | +        return $this->add(function($value, $nameKey) use ($length) { | |
| 121 | 121 |              if (count($value) != $length) { | 
| 122 | 122 |                  return $this->createError('array.length', $value, $nameKey); | 
| 123 | 123 | } | 
| @@ -131,7 +131,7 @@ discard block | ||
| 131 | 131 | */ | 
| 132 | 132 | public function unique() | 
| 133 | 133 |      { | 
| 134 | -        return $this->add(function ($value, $nameKey) { | |
| 134 | +        return $this->add(function($value, $nameKey) { | |
| 135 | 135 |              if (md5(serialize($value)) != md5(serialize(array_unique($value)))) { | 
| 136 | 136 |                  return $this->createError('array.unique', $value, $nameKey); | 
| 137 | 137 | } | 
| @@ -147,7 +147,7 @@ discard block | ||
| 147 | 147 | */ | 
| 148 | 148 | public function items(AbstractValidator $definition) | 
| 149 | 149 |      { | 
| 150 | -        return $this->add(function ($value, $nameKey) use ($definition) { | |
| 150 | +        return $this->add(function($value, $nameKey) use ($definition) { | |
| 151 | 151 |              foreach ($value as $key => $val) { | 
| 152 | 152 | $resp = $definition($val, $nameKey); | 
| 153 | 153 |                  if ($resp instanceof ValidationError) { | 
| @@ -11,7 +11,7 @@ discard block | ||
| 11 | 11 | */ | 
| 12 | 12 | public function format($format) | 
| 13 | 13 |      { | 
| 14 | -        return $this->add(function ($value, $namekey) use ($format) { | |
| 14 | +        return $this->add(function($value, $namekey) use ($format) { | |
| 15 | 15 | $date = \DateTime::createFromFormat($format, $value); | 
| 16 | 16 |              if (false === $date) { | 
| 17 | 17 |                  return $this->createError('date.format', $value, $namekey); | 
| @@ -27,7 +27,7 @@ discard block | ||
| 27 | 27 | */ | 
| 28 | 28 | public function min($date) | 
| 29 | 29 |      { | 
| 30 | -        return $this->add(function ($value, $nameKey) use ($date) { | |
| 30 | +        return $this->add(function($value, $nameKey) use ($date) { | |
| 31 | 31 | $minDate = strtotime($date); | 
| 32 | 32 | $curDate = strtotime($value); | 
| 33 | 33 | |
| @@ -45,7 +45,7 @@ discard block | ||
| 45 | 45 | */ | 
| 46 | 46 | public function max($date) | 
| 47 | 47 |      { | 
| 48 | -        return $this->add(function ($value, $nameKey) use ($date) { | |
| 48 | +        return $this->add(function($value, $nameKey) use ($date) { | |
| 49 | 49 | $maxDate = strtotime($date); | 
| 50 | 50 | $curDate = strtotime($value); | 
| 51 | 51 | |
| @@ -62,10 +62,10 @@ discard block | ||
| 62 | 62 | */ | 
| 63 | 63 | public function timestamp() | 
| 64 | 64 |      { | 
| 65 | -        return $this->add(function ($value, $nameKey) { | |
| 65 | +        return $this->add(function($value, $nameKey) { | |
| 66 | 66 |              try { | 
| 67 | 67 |                  new \DateTime('@' . $value); | 
| 68 | -            } catch (\Exception $e) { | |
| 68 | +            }catch (\Exception $e) { | |
| 69 | 69 |                  return $this->createError('date.timestamp', $value, $nameKey); | 
| 70 | 70 | } | 
| 71 | 71 | }); | 
| @@ -42,7 +42,7 @@ discard block | ||
| 42 | 42 | ] | 
| 43 | 43 | ]; | 
| 44 | 44 | |
| 45 | -        $this->add(function ($value, $nameKey) { | |
| 45 | +        $this->add(function($value, $nameKey) { | |
| 46 | 46 |              if (is_null($value)) { | 
| 47 | 47 | return; | 
| 48 | 48 | } | 
| @@ -61,7 +61,7 @@ discard block | ||
| 61 | 61 | */ | 
| 62 | 62 | public function token() | 
| 63 | 63 |      { | 
| 64 | -        return $this->add(function ($value, $nameKey) { | |
| 64 | +        return $this->add(function($value, $nameKey) { | |
| 65 | 65 |              preg_match('/[a-zA-Z0-9_]+/', $value, $matches); | 
| 66 | 66 |              if (!(isset($matches[0]) && ($matches[0] == $value))) { | 
| 67 | 67 |                  $this->createError('string.token', $value, $nameKey); | 
| @@ -78,7 +78,7 @@ discard block | ||
| 78 | 78 | */ | 
| 79 | 79 | public function min($min) | 
| 80 | 80 |      { | 
| 81 | -        return $this->add(function ($value, $nameKey) use ($min) { | |
| 81 | +        return $this->add(function($value, $nameKey) use ($min) { | |
| 82 | 82 |              if (strlen($value) < $min) { | 
| 83 | 83 |                  $this->createError('string.min', $value, $nameKey, $min); | 
| 84 | 84 | } | 
| @@ -93,7 +93,7 @@ discard block | ||
| 93 | 93 | */ | 
| 94 | 94 | public function max($max) | 
| 95 | 95 |      { | 
| 96 | -        return $this->add(function ($value, $nameKey) use ($max) { | |
| 96 | +        return $this->add(function($value, $nameKey) use ($max) { | |
| 97 | 97 |              if (strlen($value) > $max) { | 
| 98 | 98 |                  return $this->createError('string.max', $value, $nameKey, $max); | 
| 99 | 99 | } | 
| @@ -108,7 +108,7 @@ discard block | ||
| 108 | 108 | */ | 
| 109 | 109 | public function matches($regex) | 
| 110 | 110 |      { | 
| 111 | -        return $this->add(function ($value, $nameKey) use ($regex) { | |
| 111 | +        return $this->add(function($value, $nameKey) use ($regex) { | |
| 112 | 112 |              if (!preg_match($regex, $value)) { | 
| 113 | 113 |                  return $this->createError('string.matches', $value, $nameKey, $regex); | 
| 114 | 114 | } | 
| @@ -122,7 +122,7 @@ discard block | ||
| 122 | 122 | */ | 
| 123 | 123 | public function length($length) | 
| 124 | 124 |      { | 
| 125 | -        return $this->add(function ($value, $nameKey) use ($length) { | |
| 125 | +        return $this->add(function($value, $nameKey) use ($length) { | |
| 126 | 126 |              if (strlen($value) != $length) { | 
| 127 | 127 |                  return $this->createError('string.length', $value, $nameKey, $length); | 
| 128 | 128 | } | 
| @@ -136,7 +136,7 @@ discard block | ||
| 136 | 136 | */ | 
| 137 | 137 | public function alphanum() | 
| 138 | 138 |      { | 
| 139 | -        return $this->add(function ($value, $nameKey) { | |
| 139 | +        return $this->add(function($value, $nameKey) { | |
| 140 | 140 |              if (!ctype_alnum($value)) { | 
| 141 | 141 |                  return $this->createError('string.alphanum', $value, $nameKey); | 
| 142 | 142 | } | 
| @@ -150,7 +150,7 @@ discard block | ||
| 150 | 150 | */ | 
| 151 | 151 | public function alpha() | 
| 152 | 152 |      { | 
| 153 | -        return $this->add(function ($value, $nameKey) { | |
| 153 | +        return $this->add(function($value, $nameKey) { | |
| 154 | 154 |              if (!ctype_alpha($value)) { | 
| 155 | 155 |                  return $this->createError('string.alpha', $value, $nameKey); | 
| 156 | 156 | } | 
| @@ -164,7 +164,7 @@ discard block | ||
| 164 | 164 | */ | 
| 165 | 165 | public function email() | 
| 166 | 166 |      { | 
| 167 | -        return $this->add(function ($value, $nameKey) { | |
| 167 | +        return $this->add(function($value, $nameKey) { | |
| 168 | 168 |              if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { | 
| 169 | 169 |                  return $this->createError('string.email', $value, $nameKey); | 
| 170 | 170 | } | 
| @@ -178,7 +178,7 @@ discard block | ||
| 178 | 178 | */ | 
| 179 | 179 | public function ip() | 
| 180 | 180 |      { | 
| 181 | -        return $this->add(function ($value, $nameKey) { | |
| 181 | +        return $this->add(function($value, $nameKey) { | |
| 182 | 182 |              if (!filter_var($value, FILTER_VALIDATE_IP)) { | 
| 183 | 183 |                  return $this->createError('string.ip', $value, $nameKey); | 
| 184 | 184 | } | 
| @@ -192,7 +192,7 @@ discard block | ||
| 192 | 192 | */ | 
| 193 | 193 | public function uri() | 
| 194 | 194 |      { | 
| 195 | -        return $this->add(function ($value, $nameKey) { | |
| 195 | +        return $this->add(function($value, $nameKey) { | |
| 196 | 196 |              if (!filter_var($value, FILTER_VALIDATE_URL)) { | 
| 197 | 197 |                  return $this->createError('string.uri', $value, $nameKey); | 
| 198 | 198 | } | 
| @@ -209,7 +209,7 @@ discard block | ||
| 209 | 209 | */ | 
| 210 | 210 | public function replace($pattern, $replacement) | 
| 211 | 211 |      { | 
| 212 | -        return $this->add(function ($value, $nameKey) use ($pattern, $replacement) { | |
| 212 | +        return $this->add(function($value, $nameKey) use ($pattern, $replacement) { | |
| 213 | 213 | return preg_replace($pattern, $replacement, $value); | 
| 214 | 214 | }); | 
| 215 | 215 | } | 
| @@ -16,7 +16,7 @@ discard block | ||
| 16 | 16 | |
| 17 | 17 | $this->toBool(false); | 
| 18 | 18 | |
| 19 | -        $this->add(function ($value, $nameKey) { | |
| 19 | +        $this->add(function($value, $nameKey) { | |
| 20 | 20 | json_decode($value); | 
| 21 | 21 |              if (json_last_error() != JSON_ERROR_NONE) { | 
| 22 | 22 |                  return $this->createError('json.invalid', $value, $nameKey); | 
| @@ -32,7 +32,7 @@ discard block | ||
| 32 | 32 | */ | 
| 33 | 33 | public function keys(array $definition) | 
| 34 | 34 |      { | 
| 35 | -        return $this->add(function ($value, $nameKey) use ($definition) { | |
| 35 | +        return $this->add(function($value, $nameKey) use ($definition) { | |
| 36 | 36 | $decodedValue = json_decode($value, true); | 
| 37 | 37 | $validation = $this->array()->keys($definition); | 
| 38 | 38 | return $validation($decodedValue); | 
| @@ -47,7 +47,7 @@ discard block | ||
| 47 | 47 | */ | 
| 48 | 48 | public function items(AbstractValidator $definition) | 
| 49 | 49 |      { | 
| 50 | -        return $this->add(function ($value, $nameKey) use ($definition) { | |
| 50 | +        return $this->add(function($value, $nameKey) use ($definition) { | |
| 51 | 51 | $decodedValue = json_decode($value, true); | 
| 52 | 52 | $validation = $this->array()->items($definition); | 
| 53 | 53 | return $validation($decodedValue); | 
| @@ -50,7 +50,7 @@ discard block | ||
| 50 | 50 | */ | 
| 51 | 51 | public function min($min) | 
| 52 | 52 |      { | 
| 53 | -        return $this->add(function ($value, $nameKey) use ($min) { | |
| 53 | +        return $this->add(function($value, $nameKey) use ($min) { | |
| 54 | 54 |              if ($value < $min) { | 
| 55 | 55 |                  return $this->createError('number.min', $value, $nameKey); | 
| 56 | 56 | } | 
| @@ -65,7 +65,7 @@ discard block | ||
| 65 | 65 | */ | 
| 66 | 66 | public function max($max) | 
| 67 | 67 |      { | 
| 68 | -        return $this->add(function ($value, $nameKey) use ($max) { | |
| 68 | +        return $this->add(function($value, $nameKey) use ($max) { | |
| 69 | 69 |              if ($value > $max) { | 
| 70 | 70 |                  return $this->createError('number.max', $value, $nameKey); | 
| 71 | 71 | } | 
| @@ -81,7 +81,7 @@ discard block | ||
| 81 | 81 | */ | 
| 82 | 82 | public function precision($precision) | 
| 83 | 83 |      { | 
| 84 | -        return $this->add(function ($value, $nameKey) use ($precision) { | |
| 84 | +        return $this->add(function($value, $nameKey) use ($precision) { | |
| 85 | 85 |              if (strlen(substr($value, strpos($value, '.') + 1)) != $precision) { | 
| 86 | 86 |                  return $this->createError('number.precision', $value, $nameKey); | 
| 87 | 87 | } | 
| @@ -95,7 +95,7 @@ discard block | ||
| 95 | 95 | */ | 
| 96 | 96 | public function positive() | 
| 97 | 97 |      { | 
| 98 | -        return $this->add(function ($value, $nameKey) { | |
| 98 | +        return $this->add(function($value, $nameKey) { | |
| 99 | 99 |              if ($value < 0) { | 
| 100 | 100 |                  return $this->createError('number.positive', $value, $nameKey); | 
| 101 | 101 | } | 
| @@ -107,7 +107,7 @@ discard block | ||
| 107 | 107 | */ | 
| 108 | 108 | public function negative() | 
| 109 | 109 |      { | 
| 110 | -        return $this->add(function ($value, $nameKey) { | |
| 110 | +        return $this->add(function($value, $nameKey) { | |
| 111 | 111 |              if ($value > 0) { | 
| 112 | 112 |                  return $this->createError('number.negative', $value, $nameKey); | 
| 113 | 113 | } | 
| @@ -121,7 +121,7 @@ discard block | ||
| 121 | 121 | */ | 
| 122 | 122 | public function isInt() | 
| 123 | 123 |      { | 
| 124 | -        return $this->add(function ($value, $nameKey) { | |
| 124 | +        return $this->add(function($value, $nameKey) { | |
| 125 | 125 |              if (!is_int($value)) { | 
| 126 | 126 |                  return $this->createError('number.is_int', $value, $nameKey); | 
| 127 | 127 | } | 
| @@ -135,7 +135,7 @@ discard block | ||
| 135 | 135 | */ | 
| 136 | 136 | public function isFloat() | 
| 137 | 137 |      { | 
| 138 | -        return $this->add(function ($value, $nameKey) { | |
| 138 | +        return $this->add(function($value, $nameKey) { | |
| 139 | 139 |              if (!is_float($value)) { | 
| 140 | 140 |                  return $this->createError('number.is_float', $value, $nameKey); | 
| 141 | 141 | } | 
| @@ -149,7 +149,7 @@ discard block | ||
| 149 | 149 | */ | 
| 150 | 150 | public function isNumber() | 
| 151 | 151 |      { | 
| 152 | -        return $this->add(function ($value, $nameKey) { | |
| 152 | +        return $this->add(function($value, $nameKey) { | |
| 153 | 153 |              if (!is_numeric($value)) { | 
| 154 | 154 |                  return $this->createError('number.is_numeric', $value, $nameKey); | 
| 155 | 155 | } | 
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | */ | 
| 12 | 12 | public function anyOf(array $vals) | 
| 13 | 13 |      { | 
| 14 | -        $this->add(function ($value, $nameKey) use ($vals) { | |
| 14 | +        $this->add(function($value, $nameKey) use ($vals) { | |
| 15 | 15 |              if (!in_array($value, $vals)) { | 
| 16 | 16 |                  return $this->createError('anyof', $value, $nameKey); | 
| 17 | 17 | } | 
| @@ -33,7 +33,7 @@ discard block | ||
| 33 | 33 | */ | 
| 34 | 34 | private function orMethod($conditions) | 
| 35 | 35 |      { | 
| 36 | -        return $this->add(function ($value, $nameKey) use ($conditions) { | |
| 36 | +        return $this->add(function($value, $nameKey) use ($conditions) { | |
| 37 | 37 | $errors = []; | 
| 38 | 38 |              foreach ($conditions as $condition) { | 
| 39 | 39 |                  if (!$condition instanceof AbstractValidator) { | 
| @@ -58,7 +58,7 @@ discard block | ||
| 58 | 58 | */ | 
| 59 | 59 | private function ifThenElse($conditions) | 
| 60 | 60 |      { | 
| 61 | -        return $this->add(function ($value, $nameKey) use ($conditions) { | |
| 61 | +        return $this->add(function($value, $nameKey) use ($conditions) { | |
| 62 | 62 | |
| 63 | 63 |              foreach ($conditions as $condition) { | 
| 64 | 64 |                  if (!isset($condition['is'])) { |