Skip to content

Commit 6b6b0df

Browse files
committed
Change Translator to replace placeholders by key
- Add ReplaceParameters trait to replace parameters on a message - Replace DataView exceptions with translation keys
1 parent b84885d commit 6b6b0df

12 files changed

+87
-31
lines changed

src/Data/CachedDataSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private function get_cache_key( ...$arguments ): string {
7676
return md5( json_encode( $arguments, JSON_THROW_ON_ERROR ) );
7777
} catch ( JsonException $e ) {
7878
throw new InvalidArgumentException(
79-
'The cache key could not be generated based on the provide arguments',
79+
'The cache key could not be generated based on the provided arguments.',
8080
$e->getCode(),
8181
$e,
8282
);

src/Data/CsvDataSource.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Closure;
88
use DataKit\DataViews\Data\DataMatcher\ArrayDataMatcher;
99
use DataKit\DataViews\Data\Exception\DataNotFoundException;
10+
use DataKit\DataViews\Data\Exception\DataSourceNotFoundException;
1011
use DataKit\DataViews\DataView\Sort;
11-
use InvalidArgumentException;
1212
use Iterator;
1313
use LimitIterator;
1414
use SplFileObject;
@@ -34,6 +34,8 @@ final class CsvDataSource extends BaseDataSource {
3434
* @since $ver$
3535
*
3636
* @param string $file_path The file path to the CSV file.
37+
*
38+
* @throws DataSourceNotFoundException When file is not readable.
3739
*/
3840
public function __construct(
3941
string $file_path,
@@ -44,7 +46,7 @@ public function __construct(
4446
if (
4547
! file_exists( $file_path )
4648
|| ! is_readable( $file_path ) ) {
47-
throw new InvalidArgumentException( 'The CSV data source file is not found or readable.' );
49+
throw new DataSourceNotFoundException();
4850
}
4951

5052
$this->file = new SplFileObject( $file_path, 'rb' );
@@ -101,7 +103,6 @@ public function get_data_by_id( string $id ): array {
101103
throw DataNotFoundException::with_id( $this, $id );
102104
}
103105

104-
105106
/**
106107
* Cleans CSV data.
107108
*

src/Data/Exception/ActionForbiddenException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class ActionForbiddenException extends DataSourceException {
3737
*/
3838
public function __construct(
3939
DataSource $data_source,
40-
$message = 'Action is forbidden.',
40+
$message = 'datakit.action.forbidden',
4141
$code = 403,
4242
Throwable $previous = null
4343
) {
@@ -84,6 +84,6 @@ public function translate( Translator $translator ): string {
8484
return parent::translate( $translator );
8585
}
8686

87-
return $translator->translate( 'This action is forbidden for data set with id "%s".', $this->id );
87+
return $translator->translate( 'datakit.action.forbidden.with_id', [ 'id' => $this->id ] );
8888
}
8989
}

src/Data/Exception/DataNotFoundException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class DataNotFoundException extends DataSourceException {
3737
*/
3838
public function __construct(
3939
DataSource $data_source,
40-
$message = 'Dataset for id not found.',
40+
$message = 'datakit.data.not_found',
4141
$code = 404,
4242
Throwable $previous = null
4343
) {
@@ -84,6 +84,6 @@ public function translate( Translator $translator ): string {
8484
return parent::translate( $translator );
8585
}
8686

87-
return $translator->translate( 'Data set with id "%s" not found.', $this->id );
87+
return $translator->translate( 'datakit.data.not_found.with_id', [ 'id' => $this->id ] );
8888
}
8989
}

src/Data/Exception/DataSourceNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class DataSourceNotFoundException extends DataSourceException {
1717
*
1818
* @phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found
1919
*/
20-
public function __construct( $message = 'Data source not found.', $code = 404, Throwable $previous = null ) {
20+
public function __construct( $message = 'datakit.data_source.not_found', $code = 404, Throwable $previous = null ) {
2121
parent::__construct( $message, $code, $previous );
2222
}
2323
}

src/DataView/DataViewNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class DataViewNotFoundException extends DataViewException {
1919
* @param string $message The message.
2020
* @param Exception|null $previous The previous exception.
2121
*/
22-
public function __construct( $message = 'The DataView was not found.', Exception $previous = null ) {
22+
public function __construct( $message = 'datakit.dataview.not_found', Exception $previous = null ) {
2323
parent::__construct( $message, 404, $previous );
2424
}
2525
}

src/Translation/ReplaceParameters.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace DataKit\DataViews\Translation;
4+
5+
/**
6+
* Helper trait to replace parameters on a string.
7+
*
8+
* @since $ver$
9+
*/
10+
trait ReplaceParameters {
11+
/**
12+
* Replaces any parameters found in square brackets with the value.
13+
*
14+
* @since $ver$
15+
*
16+
* @param string $message The message.
17+
* @param array $parameters The parameters to replace.
18+
*
19+
* @return string The message with replaced parameters.
20+
*/
21+
protected function replace_parameters( string $message, array $parameters = [] ): string {
22+
$values = array_values( $parameters );
23+
$keys = array_map( static fn( string $key ): string => '[' . $key . ']', array_keys( $parameters ) );
24+
25+
return strtr( $message, array_combine( $keys, $values ) );
26+
}
27+
}

src/Translation/Translator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace DataKit\DataViews\Translation;
44

55
/**
6-
* Represents a translator that can translate and format a message based on how `sprintf` would work.
6+
* Represents a translator that can translate a message and replace placeholders.
77
*
88
* @since $ver$
99
*/
@@ -13,10 +13,10 @@ interface Translator {
1313
*
1414
* @since $ver$
1515
*
16-
* @param string $message The message to translate.
17-
* @param mixed ...$values The context needed to complete the translation.
16+
* @param string $message The message to translate.
17+
* @param array $parameters An array of parameters for the message.
1818
*
1919
* @return string The translated message with the
2020
*/
21-
public function translate( string $message, ...$values ): string;
21+
public function translate( string $message, array $parameters = [] ): string;
2222
}

tests/Data/CsvDataSourceTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use DataKit\DataViews\Data\CsvDataSource;
66
use DataKit\DataViews\Data\Exception\DataNotFoundException;
7+
use DataKit\DataViews\Data\Exception\DataSourceNotFoundException;
78
use DataKit\DataViews\DataView\Filter;
89
use DataKit\DataViews\DataView\Filters;
910
use DataKit\DataViews\DataView\Search;
1011
use DataKit\DataViews\DataView\Sort;
11-
use InvalidArgumentException;
1212
use PHPUnit\Framework\TestCase;
1313

1414
/**
@@ -34,14 +34,13 @@ protected function setUp(): void {
3434
$this->data_source = new CsvDataSource( __DIR__ . '/../assets/oscar-example-data.csv' );
3535
}
3636

37-
3837
/**
3938
* Test case for missing or unreadable path.
4039
*
4140
* @since $ver$
4241
*/
4342
public function test_invalid_path(): void {
44-
$this->expectException( InvalidArgumentException::class );
43+
$this->expectException( DataSourceNotFoundException::class );
4544
new CsvDataSource( 'invalid-path' );
4645
}
4746

tests/Data/Exception/ActionForbiddenExceptionTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use DataKit\DataViews\Data\ArrayDataSource;
66
use DataKit\DataViews\Data\Exception\ActionForbiddenException;
7-
use DataKit\DataViews\Translation\NoopTranslator;
7+
use DataKit\DataViews\Translation\ReplaceParameters;
88
use DataKit\DataViews\Translation\Translator;
99
use PHPUnit\Framework\TestCase;
1010

@@ -21,8 +21,10 @@ final class ActionForbiddenExceptionTest extends TestCase {
2121
*/
2222
public function test_exception(): void {
2323
$translator = new class implements Translator {
24-
public function translate( string $message, ...$values ): string {
25-
return sprintf( $message, ...$values );
24+
use ReplaceParameters;
25+
26+
public function translate( string $message, array $parameters = [] ): string {
27+
return $this->replace_parameters( $message, $parameters );
2628
}
2729
};
2830

@@ -34,9 +36,6 @@ public function translate( string $message, ...$values ): string {
3436
self::assertSame( $data_source, $with_id->data_source() );
3537

3638
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-
);
39+
self::assertSame( 'datakit.action.forbidden.with_id', $with_id->translate( $translator ) );
4140
}
4241
}

0 commit comments

Comments
 (0)