Skip to content

Commit b84885d

Browse files
committed
Add translation possibilities
1 parent cf1204a commit b84885d

File tree

7 files changed

+192
-3
lines changed

7 files changed

+192
-3
lines changed

src/Data/Exception/ActionForbiddenException.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace DataKit\DataViews\Data\Exception;
44

55
use DataKit\DataViews\Data\DataSource;
6+
use DataKit\DataViews\Translation\Translator;
67
use Throwable;
78

89
/**
@@ -20,6 +21,15 @@ final class ActionForbiddenException extends DataSourceException {
2021
*/
2122
private DataSource $data_source;
2223

24+
/**
25+
* The ID of the DataSet.
26+
*
27+
* @since $ve$
28+
*
29+
* @var string
30+
*/
31+
private string $id;
32+
2333
/**
2434
* @inheritDoc
2535
*
@@ -47,7 +57,10 @@ public function __construct(
4757
* @return self The exception.
4858
*/
4959
public static function with_id( DataSource $data_source, string $id ): self {
50-
return new self( $data_source, sprintf( 'This action is forbidden for data set with id "%s".', $id ) );
60+
$exception = new self( $data_source );
61+
$exception->id = $id;
62+
63+
return $exception;
5164
}
5265

5366
/**
@@ -60,4 +73,17 @@ public static function with_id( DataSource $data_source, string $id ): self {
6073
public function data_source(): DataSource {
6174
return $this->data_source;
6275
}
76+
77+
/**
78+
* {@inheritDoc}
79+
*
80+
* @since $ver$
81+
*/
82+
public function translate( Translator $translator ): string {
83+
if ( ! isset( $this->id ) ) {
84+
return parent::translate( $translator );
85+
}
86+
87+
return $translator->translate( 'This action is forbidden for data set with id "%s".', $this->id );
88+
}
6389
}

src/Data/Exception/DataNotFoundException.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace DataKit\DataViews\Data\Exception;
44

55
use DataKit\DataViews\Data\DataSource;
6+
use DataKit\DataViews\Translation\Translator;
67
use Throwable;
78

89
/**
@@ -20,6 +21,15 @@ final class DataNotFoundException extends DataSourceException {
2021
*/
2122
private DataSource $data_source;
2223

24+
/**
25+
* The ID of the DataSet.
26+
*
27+
* @since $ve$
28+
*
29+
* @var string
30+
*/
31+
private string $id;
32+
2333
/**
2434
* @inheritDoc
2535
*
@@ -47,7 +57,10 @@ public function __construct(
4757
* @return self The exception.
4858
*/
4959
public static function with_id( DataSource $data_source, string $id ): self {
50-
return new self( $data_source, sprintf( 'Data set with id "%s" not found.', $id ) );
60+
$exception = new self( $data_source );
61+
$exception->id = $id;
62+
63+
return $exception;
5164
}
5265

5366
/**
@@ -60,4 +73,17 @@ public static function with_id( DataSource $data_source, string $id ): self {
6073
public function data_source(): DataSource {
6174
return $this->data_source;
6275
}
76+
77+
/**
78+
* {@inheritDoc}
79+
*
80+
* @since $ver
81+
*/
82+
public function translate( Translator $translator ): string {
83+
if ( ! isset( $this->id ) ) {
84+
return parent::translate( $translator );
85+
}
86+
87+
return $translator->translate( 'Data set with id "%s" not found.', $this->id );
88+
}
6389
}

src/DataViewException.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22

33
namespace DataKit\DataViews;
44

5+
use DataKit\DataViews\Translation\Translatable;
6+
use DataKit\DataViews\Translation\Translator;
57
use Exception;
68

79
/**
810
* Exception from the DataView namespace.
911
*
1012
* @since $ver$
1113
*/
12-
class DataViewException extends Exception {
14+
class DataViewException extends Exception implements Translatable {
15+
/**
16+
* {@inheritDoc}
17+
*
18+
* @since $ver$
19+
*/
20+
public function translate( Translator $translator ): string {
21+
return $translator->translate( $this->getMessage() );
22+
}
1323
}

src/Translation/Translatable.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace DataKit\DataViews\Translation;
4+
5+
/**
6+
* Marks a class to be translatable.
7+
*
8+
* @since $ver$
9+
*/
10+
interface Translatable {
11+
/**
12+
* Returns a messages through the provided translator.
13+
*
14+
* @since $ver$
15+
*
16+
* @param Translator $translator The translator.
17+
*
18+
* @return string The translation.
19+
*/
20+
public function translate( Translator $translator ): string;
21+
}

src/Translation/Translator.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace DataKit\DataViews\Translation;
4+
5+
/**
6+
* Represents a translator that can translate and format a message based on how `sprintf` would work.
7+
*
8+
* @since $ver$
9+
*/
10+
interface Translator {
11+
/**
12+
* Translates the message.
13+
*
14+
* @since $ver$
15+
*
16+
* @param string $message The message to translate.
17+
* @param mixed ...$values The context needed to complete the translation.
18+
*
19+
* @return string The translated message with the
20+
*/
21+
public function translate( string $message, ...$values ): string;
22+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace DataKit\DataViews\Tests\Data\Exception;
4+
5+
use DataKit\DataViews\Data\ArrayDataSource;
6+
use DataKit\DataViews\Data\Exception\ActionForbiddenException;
7+
use DataKit\DataViews\Translation\NoopTranslator;
8+
use DataKit\DataViews\Translation\Translator;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* Unit tests for {@see ActionForbiddenException}
13+
*
14+
* @since $ver$
15+
*/
16+
final class ActionForbiddenExceptionTest extends TestCase {
17+
/**
18+
* Test case for {@see ActionForbiddenException}.
19+
*
20+
* @since $ver$
21+
*/
22+
public function test_exception(): void {
23+
$translator = new class implements Translator {
24+
public function translate( string $message, ...$values ): string {
25+
return sprintf( $message, ...$values );
26+
}
27+
};
28+
29+
$data_source = new ArrayDataSource( 'test', [] );
30+
$custom_message = new ActionForbiddenException( $data_source, 'some message' );
31+
$with_id = ActionForbiddenException::with_id( $data_source, 'test-id' );
32+
33+
self::assertSame( $data_source, $custom_message->data_source() );
34+
self::assertSame( $data_source, $with_id->data_source() );
35+
36+
self::assertSame( 'some message', $custom_message->translate( $translator ) );
37+
self::assertSame(
38+
'This action is forbidden for data set with id "test-id".',
39+
$with_id->translate( $translator )
40+
);
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace DataKit\DataViews\Tests\Data\Exception;
4+
5+
use DataKit\DataViews\Data\ArrayDataSource;
6+
use DataKit\DataViews\Data\Exception\DataNotFoundException;
7+
use DataKit\DataViews\Translation\NoopTranslator;
8+
use DataKit\DataViews\Translation\Translator;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* Unit tests for {@see DataNotFoundException}
13+
*
14+
* @since $ver$
15+
*/
16+
final class DataNotFoundExceptionTest extends TestCase {
17+
/**
18+
* Test case for {@see DataNotFoundException}.
19+
*
20+
* @since $ver$
21+
*/
22+
public function test_exception(): void {
23+
$translator = new class implements Translator {
24+
public function translate( string $message, ...$values ): string {
25+
return sprintf( $message, ...$values );
26+
}
27+
};
28+
29+
$data_source = new ArrayDataSource( 'test', [] );
30+
$custom_message = new DataNotFoundException( $data_source, 'some message' );
31+
$with_id = DataNotFoundException::with_id( $data_source, 'test-id' );
32+
33+
self::assertSame( $data_source, $custom_message->data_source() );
34+
self::assertSame( $data_source, $with_id->data_source() );
35+
36+
self::assertSame( 'some message', $custom_message->translate( $translator ) );
37+
self::assertSame(
38+
'Data set with id "test-id" not found.',
39+
$with_id->translate( $translator )
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)