public function testSettingTemplates() { $x = new ValidationException(); $x->configure('bar'); $x->setTemplate('foo'); $this->assertEquals('foo', $x->getTemplate()); }
private function overwriteExceptionParams(ValidationException $exception) { $params = []; foreach ($exception->getParams() as $key => $value) { if (in_array($key, ['template', 'translator'])) { continue; } $params[$key] = $this->baseKey; } $exception->configure($this->comparedKey, $params); return $exception; }
/** * @dataProvider providerForInvalidFactorDividend */ public function testInvalidDividentShouldThrowComponentException($dividend, $input) { $this->setExpectedException('Respect\\Validation\\Exceptions\\ComponentException', 'Dividend ' . ValidationException::stringify($dividend) . ' must be an integer'); // It is enough to simply create a new Factor to trigger the dividend // exceptions in __construct. new Factor($dividend); }
public function __construct($dividend) { if (!is_numeric($dividend) || (int) $dividend != $dividend) { $message = 'Dividend %s must be an integer'; throw new ComponentException(sprintf($message, ValidationException::stringify($dividend))); } $this->dividend = (int) $dividend; }
public function reportError($input, array $extraParams = array()) { $exception = $this->createException(); $name = $this->name ?: ValidationException::stringify($input); $params = array_merge(get_class_vars(__CLASS__), get_object_vars($this), $extraParams, compact('input')); $exception->configure($name, $params); if (!is_null($this->template)) { $exception->setTemplate($this->template); } return $exception; }
public static function format($template, array $vars = []) { return preg_replace_callback('/{{(\\w+)}}/', function ($match) use($vars) { if (!isset($vars[$match[1]])) { return $match[0]; } $value = $vars[$match[1]]; if ('name' == $match[1]) { return $value; } return ValidationException::stringify($value); }, $template); }
/** * @return bool */ private function isRelated($name, ValidationException $exception) { return $exception->getId() === $name || $exception->getName() === $name; }
public function setParam($name, $value) { parent::setParam($name, $value); if ('translator' === $name) { foreach ($this->getRelated(true) as $related) { $related->setParam($name, $value); } } return $this; }