shlink/module/Core/src/Exception/ValidationException.php

67 lines
1.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Exception;
use Throwable;
use Zend\InputFilter\InputFilterInterface;
2019-08-08 17:20:37 +03:00
use function Functional\reduce_left;
use function is_array;
use function print_r;
use function sprintf;
use const PHP_EOL;
class ValidationException extends RuntimeException
{
/** @var array */
private $invalidElements;
public function __construct(
string $message = '',
array $invalidElements = [],
int $code = 0,
?Throwable $previous = null
) {
$this->invalidElements = $invalidElements;
parent::__construct($message, $code, $previous);
}
public static function fromInputFilter(InputFilterInterface $inputFilter, ?Throwable $prev = null): self
{
return static::fromArray($inputFilter->getMessages(), $prev);
}
private static function fromArray(array $invalidData, ?Throwable $prev = null): self
{
return new self(
sprintf(
'Provided data is not valid. These are the messages:%s%s%s',
PHP_EOL,
self::formMessagesToString($invalidData),
PHP_EOL
),
$invalidData,
-1,
$prev
);
}
2019-08-08 17:20:37 +03:00
private static function formMessagesToString(array $messages = []): string
{
2019-08-08 17:20:37 +03:00
return reduce_left($messages, function ($messageSet, $name, $_, string $acc) {
return $acc . sprintf(
"\n '%s' => %s",
$name,
is_array($messageSet) ? print_r($messageSet, true) : $messageSet
);
2019-08-08 17:20:37 +03:00
}, '');
}
public function getInvalidElements(): array
{
return $this->invalidElements;
}
}