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;
|
2019-08-08 17:20:37 +03:00
|
|
|
use function Functional\reduce_left;
|
2018-10-28 10:24:06 +03:00
|
|
|
use function is_array;
|
|
|
|
use function print_r;
|
|
|
|
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
|
|
|
|
|
|
|
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(
|
|
|
|
'%s %s in %s:%s%s%sStack trace:%s%s',
|
|
|
|
__CLASS__,
|
|
|
|
$this->getMessage(),
|
|
|
|
$this->getFile(),
|
|
|
|
$this->getLine(),
|
|
|
|
$this->invalidElementsToString(),
|
|
|
|
PHP_EOL,
|
|
|
|
PHP_EOL,
|
2020-01-01 22:48:31 +03:00
|
|
|
$this->getTraceAsString(),
|
2018-01-07 21:51:25 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-21 22:05:06 +03:00
|
|
|
private function invalidElementsToString(): string
|
2018-01-07 21:51:25 +03:00
|
|
|
{
|
2019-12-30 01:16:55 +03:00
|
|
|
return reduce_left($this->getInvalidElements(), fn ($messages, string $name, $_, string $acc) => $acc . sprintf(
|
|
|
|
"\n '%s' => %s",
|
|
|
|
$name,
|
2020-01-01 22:48:31 +03:00
|
|
|
is_array($messages) ? print_r($messages, true) : $messages,
|
2019-12-30 01:16:55 +03:00
|
|
|
), '');
|
2018-01-07 21:51:25 +03:00
|
|
|
}
|
|
|
|
}
|