Skip to content

Commit

Permalink
add possibility to translate exception messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilian Ranguelov committed May 30, 2018
1 parent 532fe05 commit 366e124
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
30 changes: 26 additions & 4 deletions src/Gtin/NonNormalizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Real\Validator\Gtin;

class NonNormalizable extends \InvalidArgumentException
use Real\Validator\UserError;

class NonNormalizable extends \InvalidArgumentException implements UserError
{
# @formatter:off
public const CODE_LENGTH_14 = 1000;
Expand All @@ -17,21 +19,25 @@ class NonNormalizable extends \InvalidArgumentException
/** @var string */
private $value;

/** @var int */
private $reasonCode;

public function __construct(string $value, Specification $specification, \Throwable $previous = null)
{
$this->value = $value;
$this->reasonCode = $specification->reasonCode();

parent::__construct($this->reasonMessage($specification), $specification->reasonCode(), $previous);
parent::__construct($this->reasonMessage($this->reasonCode), $this->reasonCode, $previous);
}

public function value(): string
{
return $this->value;
}

private function reasonMessage(Specification $specification): string
private function reasonMessage(int $reasonCode): string
{
switch ($specification->reasonCode()) {
switch ($reasonCode) {
case self::CODE_LENGTH_14:
$message = 'Length is greater than 14 characters';
break;
Expand All @@ -54,4 +60,20 @@ private function reasonMessage(Specification $specification): string

return $message;
}

/**
* @inheritdoc
*/
public function messageKey(): string
{
return sprintf('GTIN %%gtin%% is not valid. %s', $this->getMessage());
}

/**
* @inheritdoc
*/
public function messageData(): array
{
return ['%gtin%' => $this->value];
}
}
17 changes: 17 additions & 0 deletions src/UserError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace Real\Validator;

interface UserError
{
/**
* Message key to be used by translation component
*/
public function messageKey(): string;

/**
* Message data to be used by translation component
*/
public function messageData(): array;
}
24 changes: 23 additions & 1 deletion tests/Gtin/NonNormalizableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use PHPUnit\Framework\TestCase;
use Real\Validator\Gtin;
use Real\Validator\UserError;

class NonNormalizableTest extends TestCase
{
Expand All @@ -30,13 +31,14 @@ public function testValue(string $value)
self::assertSame($value, $exception->value());
}

public function testExceptionExtendsTheBasicOne()
public function testInheritance()
{
/** @var Gtin\Specification $specification */
$specification = $this->createMock(Gtin\Specification::class);

$exception = new Gtin\NonNormalizable('string', $specification);
self::assertInstanceOf(\InvalidArgumentException::class, $exception);
self::assertInstanceOf(UserError::class, $exception);
}

public function testConstantValues()
Expand All @@ -48,4 +50,24 @@ public function testConstantValues()
self::assertSame(1004, Gtin\NonNormalizable::CODE_CHECKSUM);
self::assertSame(1005, Gtin\NonNormalizable::CODE_PREFIX);
}

public function testMessageKey()
{
/** @var Gtin\Specification $specification */
$specification = $this->createConfiguredMock(Gtin\Specification::class, ['reasonCode' => 42]);
$exception = new Gtin\NonNormalizable('123456789', $specification);

self::assertRegExp('/%gtin%/', $exception->messageKey());
}

public function testMessageData()
{
/** @var Gtin\Specification $specification */
$specification = $this->createConfiguredMock(Gtin\Specification::class, ['reasonCode' => 42]);
$exception = new Gtin\NonNormalizable('123456789', $specification);

$messageData = $exception->messageData();
self::assertArrayHasKey('%gtin%', $messageData);
self::assertSame('123456789', $messageData['%gtin%']);
}
}

0 comments on commit 366e124

Please sign in to comment.