diff --git a/module/Core/functions/functions.php b/module/Core/functions/functions.php index 6ae232be..c766b767 100644 --- a/module/Core/functions/functions.php +++ b/module/Core/functions/functions.php @@ -10,6 +10,9 @@ use Fig\Http\Message\StatusCodeInterface; use Laminas\InputFilter\InputFilter; use PUGX\Shortid\Factory as ShortIdFactory; +use function Functional\reduce_left; +use function is_array; +use function print_r; use function sprintf; const DEFAULT_DELETE_SHORT_URL_THRESHOLD = 15; @@ -75,3 +78,12 @@ function getOptionalBoolFromInputFilter(InputFilter $inputFilter, string $fieldN $value = $inputFilter->getValue($fieldName); return $value !== null ? (bool) $value : null; } + +function arrayToString(array $array): string +{ + return reduce_left($array, fn ($messages, string $name, $_, string $acc) => $acc . sprintf( + "\n '%s' => %s", + $name, + is_array($messages) ? print_r($messages, true) : $messages, + ), ''); +} diff --git a/module/Core/src/Exception/ValidationException.php b/module/Core/src/Exception/ValidationException.php index fc090279..195cfa42 100644 --- a/module/Core/src/Exception/ValidationException.php +++ b/module/Core/src/Exception/ValidationException.php @@ -11,9 +11,7 @@ use Mezzio\ProblemDetails\Exception\ProblemDetailsExceptionInterface; use Throwable; use function array_keys; -use function Functional\reduce_left; -use function is_array; -use function print_r; +use function Shlinkio\Shlink\Core\arrayToString; use function sprintf; use const PHP_EOL; @@ -60,19 +58,10 @@ class ValidationException extends InvalidArgumentException implements ProblemDet $this->getMessage(), $this->getFile(), $this->getLine(), - $this->invalidElementsToString(), + arrayToString($this->getInvalidElements()), PHP_EOL, PHP_EOL, $this->getTraceAsString(), ); } - - private function invalidElementsToString(): string - { - return reduce_left($this->getInvalidElements(), fn ($messages, string $name, $_, string $acc) => $acc . sprintf( - "\n '%s' => %s", - $name, - is_array($messages) ? print_r($messages, true) : $messages, - ), ''); - } } diff --git a/module/Core/test/Exception/ValidationExceptionTest.php b/module/Core/test/Exception/ValidationExceptionTest.php index 44a46c1f..fab70760 100644 --- a/module/Core/test/Exception/ValidationExceptionTest.php +++ b/module/Core/test/Exception/ValidationExceptionTest.php @@ -32,9 +32,9 @@ class ValidationExceptionTest extends TestCase ]; $barValue = print_r(['baz', 'foo'], true); $expectedStringRepresentation = << bar - 'something' => {$barValue} -EOT; + 'foo' => bar + 'something' => {$barValue} + EOT; $inputFilter = $this->prophesize(InputFilterInterface::class); $getMessages = $inputFilter->getMessages()->willReturn($invalidData);