2018-01-07 21:51:25 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2018-01-07 21:51:25 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shlinkio\Shlink\Core\Exception;
|
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
use Fig\Http\Message\StatusCodeInterface;
|
2020-01-01 23:11:53 +03:00
|
|
|
use Laminas\InputFilter\InputFilterInterface;
|
|
|
|
use Mezzio\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
|
|
|
|
use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
|
2018-10-28 10:24:06 +03:00
|
|
|
use Throwable;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2019-11-29 20:55:27 +03:00
|
|
|
use function array_keys;
|
2021-01-10 22:28:52 +03:00
|
|
|
use function Shlinkio\Shlink\Core\arrayToString;
|
2018-10-28 10:24:06 +03:00
|
|
|
use function sprintf;
|
2018-01-07 21:51:25 +03:00
|
|
|
|
2019-08-01 20:49:54 +03:00
|
|
|
use const PHP_EOL;
|
|
|
|
|
2019-11-26 22:58:38 +03:00
|
|
|
class ValidationException extends InvalidArgumentException implements ProblemDetailsExceptionInterface
|
2018-01-07 21:51:25 +03:00
|
|
|
{
|
2019-11-25 01:45:40 +03:00
|
|
|
use CommonProblemDetailsExceptionTrait;
|
|
|
|
|
|
|
|
private const TITLE = 'Invalid data';
|
2019-11-27 00:12:52 +03:00
|
|
|
private const TYPE = 'INVALID_ARGUMENT';
|
2019-11-25 01:45:40 +03:00
|
|
|
|
2019-12-30 00:27:00 +03:00
|
|
|
private array $invalidElements;
|
2019-11-29 20:55:27 +03:00
|
|
|
|
2019-08-01 20:49:54 +03:00
|
|
|
public static function fromInputFilter(InputFilterInterface $inputFilter, ?Throwable $prev = null): self
|
2018-01-07 21:51:25 +03:00
|
|
|
{
|
|
|
|
return static::fromArray($inputFilter->getMessages(), $prev);
|
|
|
|
}
|
|
|
|
|
2019-11-21 22:05:06 +03:00
|
|
|
public static function fromArray(array $invalidData, ?Throwable $prev = null): self
|
2018-01-07 21:51:25 +03:00
|
|
|
{
|
2019-11-25 01:45:40 +03:00
|
|
|
$status = StatusCodeInterface::STATUS_BAD_REQUEST;
|
2019-11-27 22:18:36 +03:00
|
|
|
$e = new self('Provided data is not valid', $status, $prev);
|
2019-11-25 01:45:40 +03:00
|
|
|
|
|
|
|
$e->detail = $e->getMessage();
|
|
|
|
$e->title = self::TITLE;
|
|
|
|
$e->type = self::TYPE;
|
|
|
|
$e->status = StatusCodeInterface::STATUS_BAD_REQUEST;
|
2019-11-29 20:55:27 +03:00
|
|
|
$e->invalidElements = $invalidData;
|
|
|
|
$e->additional = ['invalidElements' => array_keys($invalidData)];
|
2019-11-25 01:45:40 +03:00
|
|
|
|
2022-01-09 13:28:32 +03:00
|
|
|
// TODO Expose reasons for the validation to fail
|
|
|
|
// $e->additional = ['invalidElements' => array_keys($invalidData), 'reasons' => $invalidData];
|
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
return $e;
|
2019-11-21 22:05:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getInvalidElements(): array
|
|
|
|
{
|
2019-11-29 20:55:27 +03:00
|
|
|
return $this->invalidElements;
|
2019-11-21 22:05:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
return sprintf(
|
2021-01-11 17:20:26 +03:00
|
|
|
'%s %s in %s:%s%s%s%sStack trace:%s%s',
|
2019-11-21 22:05:06 +03:00
|
|
|
__CLASS__,
|
|
|
|
$this->getMessage(),
|
|
|
|
$this->getFile(),
|
|
|
|
$this->getLine(),
|
2021-01-11 17:20:26 +03:00
|
|
|
PHP_EOL,
|
2021-01-10 22:28:52 +03:00
|
|
|
arrayToString($this->getInvalidElements()),
|
2019-11-21 22:05:06 +03:00
|
|
|
PHP_EOL,
|
|
|
|
PHP_EOL,
|
2020-01-01 22:48:31 +03:00
|
|
|
$this->getTraceAsString(),
|
2018-01-07 21:51:25 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|