Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 97fa94d

Browse files
committed
chore: move trait to exception
1 parent ea55416 commit 97fa94d

2 files changed

Lines changed: 48 additions & 60 deletions

File tree

src/Exception/Util/FormatValueTrait.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/Exception/ValidationException.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
namespace ProgrammatorDev\YetAnotherPhpValidator\Exception;
44

5-
use ProgrammatorDev\YetAnotherPhpValidator\Exception\Util\FormatValueTrait;
6-
75
class ValidationException extends \Exception
86
{
9-
use FormatValueTrait;
10-
117
public function __construct(string $message, array $parameters = [])
128
{
139
$message = $this->formatMessage($message, $parameters);
@@ -23,5 +19,53 @@ private function formatMessage(string $message, array $parameters = []): string
2319
return $message;
2420
}
2521

22+
private function formatValue(mixed $value): string
23+
{
24+
if ($value instanceof \DateTimeInterface) {
25+
return $value->format('Y-m-d H:i:s');
26+
}
27+
28+
if (\is_object($value)) {
29+
if ($value instanceof \Stringable) {
30+
return $value->__toString();
31+
}
32+
33+
return 'object';
34+
}
35+
36+
if (\is_array($value)) {
37+
return $this->formatValues($value);
38+
}
39+
40+
if (\is_string($value)) {
41+
return $value;
42+
}
43+
44+
if (\is_resource($value)) {
45+
return 'resource';
46+
}
2647

48+
if ($value === null) {
49+
return 'null';
50+
}
51+
52+
if ($value === false) {
53+
return 'false';
54+
}
55+
56+
if ($value === true) {
57+
return 'true';
58+
}
59+
60+
return (string) $value;
61+
}
62+
63+
private function formatValues(array $values): string
64+
{
65+
foreach ($values as $key => $value) {
66+
$values[$key] = $this->formatValue($value);
67+
}
68+
69+
return \sprintf('[%s]', \implode(', ', $values));
70+
}
2771
}

0 commit comments

Comments
 (0)