Simplified invalidElements to be a plain list of keys when a ValidationException is cast into a problem details error

This commit is contained in:
Alejandro Celaya 2019-11-29 18:55:27 +01:00
parent 60d3c09da5
commit 3cf1657d54
2 changed files with 9 additions and 2 deletions

View File

@ -10,6 +10,7 @@ use Zend\InputFilter\InputFilterInterface;
use Zend\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
use Zend\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
use function array_keys;
use function Functional\reduce_left;
use function is_array;
use function print_r;
@ -24,6 +25,9 @@ class ValidationException extends InvalidArgumentException implements ProblemDet
private const TITLE = 'Invalid data';
private const TYPE = 'INVALID_ARGUMENT';
/** @var array */
private $invalidElements;
public static function fromInputFilter(InputFilterInterface $inputFilter, ?Throwable $prev = null): self
{
return static::fromArray($inputFilter->getMessages(), $prev);
@ -38,14 +42,15 @@ class ValidationException extends InvalidArgumentException implements ProblemDet
$e->title = self::TITLE;
$e->type = self::TYPE;
$e->status = StatusCodeInterface::STATUS_BAD_REQUEST;
$e->additional = ['invalidElements' => $invalidData];
$e->invalidElements = $invalidData;
$e->additional = ['invalidElements' => array_keys($invalidData)];
return $e;
}
public function getInvalidElements(): array
{
return $this->additional['invalidElements'];
return $this->invalidElements;
}
public function __toString(): string

View File

@ -12,6 +12,7 @@ use Shlinkio\Shlink\Core\Exception\ValidationException;
use Throwable;
use Zend\InputFilter\InputFilterInterface;
use function array_keys;
use function print_r;
class ValidationExceptionTest extends TestCase
@ -38,6 +39,7 @@ EOT;
$e = ValidationException::fromInputFilter($inputFilter->reveal());
$this->assertEquals($invalidData, $e->getInvalidElements());
$this->assertEquals(['invalidElements' => array_keys($invalidData)], $e->getAdditionalData());
$this->assertEquals('Provided data is not valid', $e->getMessage());
$this->assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getCode());
$this->assertEquals($prev, $e->getPrevious());