99use PhpList \Core \Domain \Common \Model \ValidationContext ;
1010use PhpList \Core \Domain \Common \Validator \ValidatorInterface ;
1111use Symfony \Component \Validator \Exception \ValidatorException ;
12+ use Symfony \Contracts \Translation \TranslatorInterface ;
1213use Throwable ;
1314
1415class TemplateImageValidator implements ValidatorInterface
1516{
16- public function __construct (private readonly ClientInterface $ httpClient )
17- {
17+ public function __construct (
18+ private readonly ClientInterface $ httpClient ,
19+ private readonly TranslatorInterface $ translator ,
20+ ) {
1821 }
1922
2023 public function validate (mixed $ value , ValidationContext $ context = null ): void
2124 {
2225 if (!is_array ($ value )) {
23- throw new InvalidArgumentException ('Value must be an array of image URLs. ' );
26+ throw new InvalidArgumentException ($ this -> translator -> trans ( 'Value must be an array of image URLs. ' ) );
2427 }
2528
2629 $ checkFull = $ context ?->get('checkImages ' , false );
@@ -42,7 +45,7 @@ private function validateFullUrls(array $urls): array
4245
4346 foreach ($ urls as $ url ) {
4447 if (!preg_match ('#^https?://#i ' , $ url )) {
45- $ errors [] = sprintf ('Image "%s " is not a full URL. ' , $ url );
48+ $ errors [] = $ this -> translator -> trans ('Image "%url% " is not a full URL. ' , [ ' %url% ' => $ url] );
4649 }
4750 }
4851
@@ -61,10 +64,16 @@ private function validateExistence(array $urls): array
6164 try {
6265 $ response = $ this ->httpClient ->request ('HEAD ' , $ url );
6366 if ($ response ->getStatusCode () !== 200 ) {
64- $ errors [] = sprintf ('Image "%s" does not exist (HTTP %s) ' , $ url , $ response ->getStatusCode ());
67+ $ errors [] = $ this ->translator ->trans ('Image "%url%" does not exist (HTTP %code%) ' , [
68+ '%url% ' => $ url ,
69+ '%code% ' => $ response ->getStatusCode ()
70+ ]);
6571 }
6672 } catch (Throwable $ e ) {
67- $ errors [] = sprintf ('Image "%s" could not be validated: %s ' , $ url , $ e ->getMessage ());
73+ $ errors [] = $ this ->translator ->trans ('Image "%url%" could not be validated: %message% ' , [
74+ '%url% ' => $ url ,
75+ '%message% ' => $ e ->getMessage ()
76+ ]);
6877 }
6978 }
7079
0 commit comments