2010-12-02 22:04:10 +01:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2010-12-02 22:04:10 +01:00
|
|
|
namespace Respect\Validation\Exceptions;
|
|
|
|
|
2018-02-04 00:59:18 +01:00
|
|
|
use InvalidArgumentException;
|
2019-04-30 00:31:04 +02:00
|
|
|
use Respect\Validation\Message\Formatter;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2019-02-17 19:41:55 +01:00
|
|
|
use function key;
|
2010-12-02 22:04:10 +01:00
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
/**
|
|
|
|
* Default exception class for rule validations.
|
|
|
|
*
|
2023-02-13 05:59:06 +01:00
|
|
|
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
2018-05-24 07:47:09 +02:00
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
*/
|
2018-05-24 08:25:57 +02:00
|
|
|
class ValidationException extends InvalidArgumentException implements Exception
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2018-05-23 10:07:41 +02:00
|
|
|
public const MODE_DEFAULT = 'default';
|
|
|
|
public const MODE_NEGATIVE = 'negative';
|
|
|
|
public const STANDARD = 'standard';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contains the default templates for exception message.
|
|
|
|
*
|
2019-02-07 21:22:10 +01:00
|
|
|
* @var string[][]
|
2018-05-23 10:07:41 +02:00
|
|
|
*/
|
2019-05-12 01:30:36 +02:00
|
|
|
protected $defaultTemplates = [
|
2015-10-18 03:44:47 +02:00
|
|
|
self::MODE_DEFAULT => [
|
2018-03-07 11:03:08 +01:00
|
|
|
self::STANDARD => '{{name}} must be valid',
|
2015-10-18 03:44:47 +02:00
|
|
|
],
|
|
|
|
self::MODE_NEGATIVE => [
|
2018-03-07 11:03:08 +01:00
|
|
|
self::STANDARD => '{{name}} must not be valid',
|
2015-10-18 03:44:47 +02:00
|
|
|
],
|
|
|
|
];
|
2015-08-26 19:30:45 +02:00
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
/**
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $input;
|
2011-02-06 23:21:15 +01:00
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $id;
|
2011-02-07 11:52:18 +01:00
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $mode = self::MODE_DEFAULT;
|
2011-02-05 18:32:21 +01:00
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
/**
|
2019-02-07 21:22:10 +01:00
|
|
|
* @var mixed[]
|
2018-05-24 07:47:09 +02:00
|
|
|
*/
|
|
|
|
private $params = [];
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
/**
|
2019-04-30 00:31:04 +02:00
|
|
|
* @var Formatter
|
2018-05-24 07:47:09 +02:00
|
|
|
*/
|
2019-04-30 00:31:04 +02:00
|
|
|
private $formatter;
|
2011-02-05 18:32:21 +01:00
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $template;
|
2011-02-08 23:10:58 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
* @param mixed[] $params
|
|
|
|
*/
|
2019-04-30 00:31:04 +02:00
|
|
|
public function __construct($input, string $id, array $params, Formatter $formatter)
|
2018-01-01 14:59:31 +01:00
|
|
|
{
|
2018-05-24 07:47:09 +02:00
|
|
|
$this->input = $input;
|
|
|
|
$this->id = $id;
|
|
|
|
$this->params = $params;
|
2019-04-30 00:31:04 +02:00
|
|
|
$this->formatter = $formatter;
|
2018-05-24 07:47:09 +02:00
|
|
|
$this->template = $this->chooseTemplate();
|
2018-08-22 18:30:48 +02:00
|
|
|
|
|
|
|
parent::__construct($this->createMessage());
|
2018-01-01 14:59:31 +01:00
|
|
|
}
|
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
public function getId(): string
|
2011-02-02 03:28:02 +01:00
|
|
|
{
|
2011-02-06 23:21:15 +01:00
|
|
|
return $this->id;
|
2011-02-02 03:28:02 +01:00
|
|
|
}
|
2011-02-18 04:08:46 +01:00
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
2018-05-24 07:47:09 +02:00
|
|
|
public function getParams(): array
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2018-05-24 07:47:09 +02:00
|
|
|
return $this->params;
|
2011-01-31 01:17:06 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
|
|
|
public function getParam(string $name)
|
2011-01-31 01:17:06 +01:00
|
|
|
{
|
2018-05-24 07:47:09 +02:00
|
|
|
return $this->params[$name] ?? null;
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2023-02-18 22:09:26 +01:00
|
|
|
public function setParam(string $name, mixed $value): void
|
|
|
|
{
|
|
|
|
$this->params[$name] = $value;
|
|
|
|
}
|
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
public function updateMode(string $mode): void
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2018-05-24 07:47:09 +02:00
|
|
|
$this->mode = $mode;
|
|
|
|
$this->message = $this->createMessage();
|
2010-12-02 22:04:10 +01:00
|
|
|
}
|
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
public function updateTemplate(string $template): void
|
2011-01-31 01:17:06 +01:00
|
|
|
{
|
2018-05-24 07:47:09 +02:00
|
|
|
$this->template = $template;
|
|
|
|
$this->message = $this->createMessage();
|
2011-02-20 17:59:53 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:22:10 +01:00
|
|
|
/**
|
|
|
|
* @param mixed[] $params
|
|
|
|
*/
|
2018-05-24 07:47:09 +02:00
|
|
|
public function updateParams(array $params): void
|
2011-02-20 17:59:53 +01:00
|
|
|
{
|
2018-05-24 07:47:09 +02:00
|
|
|
$this->params = $params;
|
|
|
|
$this->message = $this->createMessage();
|
2011-02-07 18:21:58 +01:00
|
|
|
}
|
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
public function hasCustomTemplate(): bool
|
2010-12-02 22:04:10 +01:00
|
|
|
{
|
2019-05-12 01:30:36 +02:00
|
|
|
return isset($this->defaultTemplates[$this->mode][$this->template]) === false;
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
protected function chooseTemplate(): string
|
2011-04-10 01:34:21 +02:00
|
|
|
{
|
2019-05-12 01:30:36 +02:00
|
|
|
return (string) key($this->defaultTemplates[$this->mode]);
|
2011-04-10 01:34:21 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 07:47:09 +02:00
|
|
|
private function createMessage(): string
|
2011-02-20 19:03:09 +01:00
|
|
|
{
|
2019-04-30 00:31:04 +02:00
|
|
|
return $this->formatter->format(
|
|
|
|
$this->defaultTemplates[$this->mode][$this->template] ?? $this->template,
|
|
|
|
$this->input,
|
|
|
|
$this->params
|
2018-05-24 07:47:09 +02:00
|
|
|
);
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
2020-08-27 15:29:46 +02:00
|
|
|
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return $this->getMessage();
|
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|