Skip to content

Commit b456c0d

Browse files
committed
AttributeTypeValidator
1 parent c07be31 commit b456c0d

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

resources/translations/messages.en.xlf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,14 @@
436436
<source>General import error: %error%</source>
437437
<target>General import error: %error%</target>
438438
</trans-unit>
439-
439+
<trans-unit id="subscription.value_must_be_string">
440+
<source>Value must be a string.</source>
441+
<target>Value must be a string.</target>
442+
</trans-unit>
443+
<trans-unit id="subscription.invalid_attribute_type">
444+
<source>Invalid attribute type: "%type%". Valid types are: %valid_types%</source>
445+
<target>Invalid attribute type: "%type%". Valid types are: %valid_types%</target>
446+
</trans-unit>
440447

441448
</body>
442449
</file>

src/Domain/Messaging/Exception/ImapConnectionException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ class ImapConnectionException extends RuntimeException
1212
public function __construct(?Throwable $previous = null)
1313
{
1414
parent::__construct('Cannot connect to IMAP server', 0, $previous);
15-
1615
}
1716
}

src/Domain/Messaging/Exception/OpenMboxFileException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ class OpenMboxFileException extends RuntimeException
1212
public function __construct(?Throwable $previous = null)
1313
{
1414
parent::__construct('Cannot open mbox file', 0, $previous);
15-
1615
}
1716
}

src/Domain/Subscription/Service/SubscriberCsvImporter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public function importFromCsv(UploadedFile $file, SubscriberImportOptions $optio
8787
$this->entityManager->flush();
8888
}
8989
} catch (Throwable $e) {
90-
$stats['errors'][] = $this->translator->trans('Error processing %email%: %error%',
90+
$stats['errors'][] = $this->translator->trans(
91+
'Error processing %email%: %error%',
9192
['%email%' => $dto->email, '%error%' => $e->getMessage()]
9293
);
9394
$stats['skipped']++;

src/Domain/Subscription/Validator/AttributeTypeValidator.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
use PhpList\Core\Domain\Common\Model\ValidationContext;
99
use PhpList\Core\Domain\Common\Validator\ValidatorInterface;
1010
use Symfony\Component\Validator\Exception\ValidatorException;
11+
use Symfony\Contracts\Translation\TranslatorInterface;
1112

1213
class AttributeTypeValidator implements ValidatorInterface
1314
{
15+
public function __construct(private readonly TranslatorInterface $translator)
16+
{
17+
}
18+
1419
private const VALID_TYPES = [
1520
'textline',
1621
'checkbox',
@@ -25,15 +30,17 @@ class AttributeTypeValidator implements ValidatorInterface
2530
public function validate(mixed $value, ValidationContext $context = null): void
2631
{
2732
if (!is_string($value)) {
28-
throw new InvalidArgumentException('Value must be a string.');
33+
throw new InvalidArgumentException($this->translator->trans('Value must be a string.'));
2934
}
3035

3136
$errors = [];
3237
if (!in_array($value, self::VALID_TYPES, true)) {
33-
$errors[] = sprintf(
34-
'Invalid attribute type: "%s". Valid types are: %s',
35-
$value,
36-
implode(', ', self::VALID_TYPES)
38+
$errors[] = $this->translator->trans(
39+
'Invalid attribute type: "%type%". Valid types are: %valid_types%',
40+
[
41+
'%type%' => $value,
42+
'%valid_types%' => implode(', ', self::VALID_TYPES),
43+
]
3744
);
3845
}
3946

tests/Unit/Domain/Subscription/Validator/AttributeTypeValidatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use InvalidArgumentException;
88
use PhpList\Core\Domain\Subscription\Validator\AttributeTypeValidator;
99
use PHPUnit\Framework\TestCase;
10+
use Symfony\Component\Translation\Translator;
1011
use Symfony\Component\Validator\Exception\ValidatorException;
1112

1213
class AttributeTypeValidatorTest extends TestCase
@@ -15,7 +16,7 @@ class AttributeTypeValidatorTest extends TestCase
1516

1617
protected function setUp(): void
1718
{
18-
$this->validator = new AttributeTypeValidator();
19+
$this->validator = new AttributeTypeValidator(new Translator('en'));
1920
}
2021

2122
public function testValidatesValidType(): void

0 commit comments

Comments
 (0)