1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Exception ;
4+
5+ class ValidationException extends \Exception
6+ {
7+ public function __construct (string $ message , array $ parameters = [])
8+ {
9+ $ message = $ this ->formatMessage ($ message , $ parameters );
10+ parent ::__construct ($ message );
11+ }
12+
13+ private function formatMessage (string $ message , array $ parameters = []): string
14+ {
15+ foreach ($ parameters as $ parameter => $ value ) {
16+ $ message = str_replace ("{{ $ parameter }} " , $ this ->formatValue ($ value ), $ message );
17+ }
18+
19+ return $ message ;
20+ }
21+
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 \sprintf ('"%s" ' , $ value );
42+ }
43+
44+ if (\is_resource ($ value )) {
45+ return 'resource ' ;
46+ }
47+
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 \implode (', ' , $ values );
70+ }
71+ }
0 commit comments