Skip to content

Commit 87cd810

Browse files
authored
Merge pull request #46 from sunrise-php/release/v3.18.0
* Improved localization capabilities.
2 parents ad6cc3c + 485bc96 commit 87cd810

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/Exception/InvalidValueException.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
namespace Sunrise\Hydrator\Exception;
1515

16+
use RuntimeException;
1617
use Sunrise\Hydrator\Dictionary\ErrorCode;
1718
use Sunrise\Hydrator\Dictionary\ErrorMessage;
18-
use RuntimeException;
19+
use Sunrise\Hydrator\Dictionary\TranslationDomain;
1920
use Symfony\Component\Validator\ConstraintViolation;
2021
use Symfony\Component\Validator\ConstraintViolationInterface;
2122

@@ -49,6 +50,11 @@ class InvalidValueException extends RuntimeException implements ExceptionInterfa
4950
*/
5051
private $invalidValue;
5152

53+
/**
54+
* @see TranslationDomain
55+
*/
56+
private string $translationDomain;
57+
5258
/**
5359
* @param list<array-key> $propertyPath
5460
* @param array<string, int|float|string> $messagePlaceholders
@@ -60,7 +66,8 @@ public function __construct(
6066
array $propertyPath,
6167
string $messageTemplate,
6268
array $messagePlaceholders,
63-
$invalidValue = null
69+
$invalidValue = null,
70+
string $translationDomain = TranslationDomain::HYDRATOR
6471
) {
6572
parent::__construct($message);
6673

@@ -69,6 +76,7 @@ public function __construct(
6976
$this->messageTemplate = $messageTemplate;
7077
$this->messagePlaceholders = $messagePlaceholders;
7178
$this->invalidValue = $invalidValue;
79+
$this->translationDomain = $translationDomain;
7280
}
7381

7482
final public function getPropertyPath(): string
@@ -112,6 +120,14 @@ final public function getInvalidValue()
112120
return $this->invalidValue;
113121
}
114122

123+
/**
124+
* @since 3.18.0
125+
*/
126+
final public function getTranslationDomain(): string
127+
{
128+
return $this->translationDomain;
129+
}
130+
115131
/**
116132
* @since 3.9.0
117133
*/

tests/HydratorTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Sunrise\Hydrator\Dictionary\BuiltinType;
2323
use Sunrise\Hydrator\Dictionary\ContextKey;
2424
use Sunrise\Hydrator\Dictionary\ErrorCode;
25+
use Sunrise\Hydrator\Dictionary\TranslationDomain;
2526
use Sunrise\Hydrator\Exception\InvalidDataException;
2627
use Sunrise\Hydrator\Exception\InvalidObjectException;
2728
use Sunrise\Hydrator\Hydrator;
@@ -46,6 +47,7 @@ class HydratorTest extends TestCase
4647
private array $invalidValueExceptionMessage = [];
4748
private array $invalidValueExceptionPropertyPath = [];
4849
private array $invalidValueExceptionErrorCode = [];
50+
private array $invalidValueExceptionTranslationDomain = [];
4951

5052
public function testIssue25(): void
5153
{
@@ -180,6 +182,7 @@ public function testHydrateBooleanPropertyWithEmptyValue(array $data): void
180182
$this->assertInvalidValueExceptionMessage(0, 'This value must not be empty.');
181183
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_NOT_BE_EMPTY);
182184
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
185+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
183186
$this->createHydrator()->hydrate($object, $data);
184187
}
185188

@@ -197,6 +200,7 @@ public function testHydrateBooleanPropertyWithInvalidValue(array $data): void
197200
$this->assertInvalidValueExceptionMessage(0, 'This value must be of type boolean.');
198201
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_BE_BOOLEAN);
199202
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
203+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
200204
$this->createHydrator()->hydrate($object, $data);
201205
}
202206

@@ -213,6 +217,7 @@ public function testHydrateBooleanPropertyWithoutValue(): void
213217
$this->assertInvalidValueExceptionMessage(0, 'This value must be provided.');
214218
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_BE_PROVIDED);
215219
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
220+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
216221
$this->createHydrator()->hydrate($object, []);
217222
}
218223

@@ -282,6 +287,7 @@ public function testHydrateIntegerPropertyWithEmptyValue(array $data): void
282287
$this->assertInvalidValueExceptionMessage(0, 'This value must not be empty.');
283288
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_NOT_BE_EMPTY);
284289
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
290+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
285291
$this->createHydrator()->hydrate($object, $data);
286292
}
287293

@@ -299,6 +305,7 @@ public function testHydrateIntegerPropertyWithInvalidValue(array $data): void
299305
$this->assertInvalidValueExceptionMessage(0, 'This value must be of type integer.');
300306
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_BE_INTEGER);
301307
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
308+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
302309
$this->createHydrator()->hydrate($object, $data);
303310
}
304311

@@ -315,6 +322,7 @@ public function testHydrateIntegerPropertyWithoutValue(): void
315322
$this->assertInvalidValueExceptionMessage(0, 'This value must be provided.');
316323
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_BE_PROVIDED);
317324
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
325+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
318326
$this->createHydrator()->hydrate($object, []);
319327
}
320328

@@ -384,6 +392,7 @@ public function testHydrateNumericPropertyWithEmptyValue(array $data): void
384392
$this->assertInvalidValueExceptionMessage(0, 'This value must not be empty.');
385393
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_NOT_BE_EMPTY);
386394
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
395+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
387396
$this->createHydrator()->hydrate($object, $data);
388397
}
389398

@@ -401,6 +410,7 @@ public function testHydrateNumericPropertyWithInvalidValue(array $data): void
401410
$this->assertInvalidValueExceptionMessage(0, 'This value must be of type number.');
402411
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_BE_NUMBER);
403412
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
413+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
404414
$this->createHydrator()->hydrate($object, $data);
405415
}
406416

@@ -417,6 +427,7 @@ public function testHydrateNumericPropertyWithoutValue(): void
417427
$this->assertInvalidValueExceptionMessage(0, 'This value must be provided.');
418428
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_BE_PROVIDED);
419429
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
430+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
420431
$this->createHydrator()->hydrate($object, []);
421432
}
422433

@@ -484,6 +495,7 @@ public function testHydrateStringPropertyWithNull(array $data): void
484495
$this->assertInvalidValueExceptionMessage(0, 'This value must not be empty.');
485496
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_NOT_BE_EMPTY);
486497
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
498+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
487499
$this->createHydrator()->hydrate($object, $data);
488500
}
489501

@@ -501,6 +513,7 @@ public function testHydrateStringPropertyWithInvalidValue(array $data): void
501513
$this->assertInvalidValueExceptionMessage(0, 'This value must be of type string.');
502514
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_BE_STRING);
503515
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
516+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
504517
$this->createHydrator()->hydrate($object, $data);
505518
}
506519

@@ -517,6 +530,7 @@ public function testHydrateStringPropertyWithoutValue(): void
517530
$this->assertInvalidValueExceptionMessage(0, 'This value must be provided.');
518531
$this->assertInvalidValueExceptionErrorCode(0, ErrorCode::MUST_BE_PROVIDED);
519532
$this->assertInvalidValueExceptionPropertyPath(0, 'value');
533+
$this->assertInvalidValueExceptionTranslationDomain(0, TranslationDomain::HYDRATOR);
520534
$this->createHydrator()->hydrate($object, []);
521535
}
522536

@@ -3180,6 +3194,13 @@ private function assertInvalidValueExceptionErrorCode(int $exceptionIndex, strin
31803194
$this->invalidValueExceptionErrorCode[] = [$exceptionIndex, $expectedErrorCode];
31813195
}
31823196

3197+
private function assertInvalidValueExceptionTranslationDomain(
3198+
int $exceptionIndex,
3199+
string $expectedTranslationDomain
3200+
): void {
3201+
$this->invalidValueExceptionTranslationDomain[] = [$exceptionIndex, $expectedTranslationDomain];
3202+
}
3203+
31833204
protected function runTest(): void
31843205
{
31853206
$invalidDataExceptionHandled = false;
@@ -3232,6 +3253,18 @@ protected function runTest(): void
32323253
);
32333254
}
32343255

3256+
foreach ($this->invalidValueExceptionTranslationDomain as [
3257+
$index,
3258+
$invalidValueExceptionTranslationDomain,
3259+
]) {
3260+
$invalidDataExceptionHandled = true;
3261+
$this->assertArrayHasKey($index, $invalidDataException->getExceptions());
3262+
$this->assertSame(
3263+
$invalidValueExceptionTranslationDomain,
3264+
$invalidDataException->getExceptions()[$index]->getTranslationDomain(),
3265+
);
3266+
}
3267+
32353268
if (!$invalidDataExceptionHandled) {
32363269
throw $invalidDataException;
32373270
}
@@ -3240,6 +3273,7 @@ protected function runTest(): void
32403273
$this->invalidValueExceptionMessage = [];
32413274
$this->invalidValueExceptionPropertyPath = [];
32423275
$this->invalidValueExceptionErrorCode = [];
3276+
$this->invalidValueExceptionTranslationDomain = [];
32433277

32443278
if ($invalidDataExceptionHandled) {
32453279
$this->assertTrue(true);

0 commit comments

Comments
 (0)