Skip to content

Commit 497cc6a

Browse files
committed
[graphql] Added ability to translate error messages.
1 parent abeaad7 commit 497cc6a

File tree

13 files changed

+164
-77
lines changed

13 files changed

+164
-77
lines changed

resources/lang/en/messages.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
// @phpcs:disable Generic.Files.LineLength.TooLong
4+
5+
return [
6+
'search_by' => [
7+
'errors' => [
8+
'too_many_properties' => 'Only one property allowed, found: `:properties`.',
9+
'too_many_operators' => 'Only one comparison operator allowed, found: `:operators`.',
10+
'unsupported_option' => 'Operator `:operator` cannot be used with `:option`.',
11+
'unknown_operator' => 'Operator `:operator` not found.',
12+
'empty_condition' => 'Search condition cannot be empty.',
13+
],
14+
],
15+
'sort_by' => [
16+
'errors' => [
17+
'unsupported_relation' => 'Relation of type `:relation` cannot be used for sort, only `:supported` supported.',
18+
'unsupported_builder' => 'Relation cannot be used with `:builder`.',
19+
'too_many_properties' => 'Only one property allowed, found: `:properties`.',
20+
'empty_clause' => 'Sort clause cannot be empty.',
21+
],
22+
],
23+
];

src/PackageTranslator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace LastDragon_ru\LaraASP\GraphQL;
4+
5+
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
6+
use LastDragon_ru\LaraASP\Core\Translator;
7+
8+
class PackageTranslator extends Translator {
9+
public function __construct(TranslatorContract $translator) {
10+
parent::__construct($translator, Package::Name);
11+
}
12+
}

src/Provider.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Provider extends ServiceProvider {
2424

2525
public function boot(Dispatcher $dispatcher): void {
2626
$this->bootConfig();
27+
$this->bootTranslations();
2728
$this->bootDirectives($dispatcher);
2829
}
2930

@@ -51,11 +52,12 @@ static function (): string {
5152

5253
protected function registerDirectives(): void {
5354
$this->app->bind(SearchByDirective::class, function (): SearchByDirective {
54-
$container = $this->app;
55-
$config = $container->make(Repository::class);
56-
$scalars = $config->get("{$this->getName()}.search_by.scalars");
57-
$aliases = $config->get("{$this->getName()}.search_by.aliases");
58-
$instance = new SearchByDirective($container, $scalars, $aliases);
55+
$container = $this->app;
56+
$translator = $container->make(PackageTranslator::class);
57+
$config = $container->make(Repository::class);
58+
$scalars = $config->get("{$this->getName()}.search_by.scalars");
59+
$aliases = $config->get("{$this->getName()}.search_by.aliases");
60+
$instance = new SearchByDirective($container, $translator, $scalars, $aliases);
5961

6062
return $instance;
6163
});

src/SearchBy/Directive.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
1010
use Illuminate\Database\Query\Builder as QueryBuilder;
1111
use Illuminate\Support\Collection;
12+
use LastDragon_ru\LaraASP\GraphQL\PackageTranslator;
1213
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\Between;
1314
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\Equal;
1415
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\GreaterThan;
@@ -42,8 +43,6 @@ class Directive extends BaseDirective implements ArgManipulator, ArgBuilderDirec
4243
*/
4344
protected ?array $operators = [];
4445

45-
protected Container $container;
46-
4746
/**
4847
* Determines operators available for each scalar type.
4948
*
@@ -106,10 +105,14 @@ class Directive extends BaseDirective implements ArgManipulator, ArgBuilderDirec
106105
* @param array<string, array<class-string<\LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Operator>>> $scalars
107106
* @param array<string,string> $aliases
108107
*/
109-
public function __construct(Container $container, array $scalars, array $aliases) {
110-
$this->container = $container;
111-
$this->scalars = array_merge($this->scalars, $scalars);
112-
$this->aliases = array_merge($this->aliases, $aliases);
108+
public function __construct(
109+
protected Container $container,
110+
protected PackageTranslator $translator,
111+
array $scalars,
112+
array $aliases,
113+
) {
114+
$this->scalars = array_merge($this->scalars, $scalars);
115+
$this->aliases = array_merge($this->aliases, $aliases);
113116
}
114117

115118
public static function definition(): string {
@@ -141,6 +144,7 @@ public function manipulateArgDefinition(
141144
*/
142145
public function handleBuilder($builder, $value): EloquentBuilder|QueryBuilder {
143146
return (new SearchBuilder(
147+
$this->translator,
144148
(new Collection($this->scalars))
145149
->add(Not::class)
146150
->flatten()

src/SearchBy/DirectiveTest.php

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

55
use Closure;
66
use Exception;
7+
use LastDragon_ru\LaraASP\GraphQL\PackageTranslator;
78
use LastDragon_ru\LaraASP\GraphQL\Testing\BuilderDataProvider;
89
use LastDragon_ru\LaraASP\GraphQL\Testing\TestCase;
910
use LastDragon_ru\LaraASP\Testing\Providers\ArrayDataProvider;
@@ -41,7 +42,7 @@ public function testHandleBuilder(bool|Exception $expected, Closure $builder, ar
4142
}
4243

4344
$builder = $builder($this);
44-
$directive = new Directive($this->app, [], []);
45+
$directive = new Directive($this->app, $this->app->make(PackageTranslator::class), [], []);
4546

4647
$this->assertNotNull($directive->handleBuilder($builder, $input));
4748
}

src/SearchBy/Operators/BaseOperator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Operator;
66

77
abstract class BaseOperator implements Operator {
8+
public function __construct() {
9+
// empty
10+
}
11+
812
/**
913
* @inheritdoc
1014
*/

src/SearchBy/Operators/Complex/Relation.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@
55
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
66
use Illuminate\Database\Query\Builder as QueryBuilder;
77
use LastDragon_ru\LaraASP\GraphQL\Helpers\ModelHelper;
8+
use LastDragon_ru\LaraASP\GraphQL\PackageTranslator;
89
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\OperatorNegationable;
910
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\BaseOperator;
1011
use LastDragon_ru\LaraASP\GraphQL\SearchBy\SearchBuilder;
1112
use LastDragon_ru\LaraASP\GraphQL\SearchBy\SearchLogicException;
1213

1314
use function is_array;
1415
use function reset;
15-
use function sprintf;
1616

1717
/**
1818
* @internal Must not be used directly.
1919
*/
2020
class Relation extends BaseOperator implements ComplexOperator, OperatorNegationable {
21+
public function __construct(
22+
protected PackageTranslator $translator,
23+
) {
24+
parent::__construct();
25+
}
26+
2127
public function getName(): string {
2228
return 'where';
2329
}
@@ -44,10 +50,12 @@ public function apply(
4450
): EloquentBuilder {
4551
// QueryBuilder?
4652
if ($builder instanceof QueryBuilder) {
47-
throw new SearchLogicException(sprintf(
48-
'Operator `%s` can not be used with `%s`.',
49-
$this->getName(),
50-
QueryBuilder::class,
53+
throw new SearchLogicException($this->translator->get(
54+
'search_by.errors.unsupported_option',
55+
[
56+
'operator' => $this->getName(),
57+
'option' => QueryBuilder::class,
58+
],
5159
));
5260
}
5361

src/SearchBy/Operators/Complex/RelationTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\Relations\HasOne;
1010
use Illuminate\Database\Query\Builder as QueryBuilder;
11+
use LastDragon_ru\LaraASP\GraphQL\PackageTranslator;
1112
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\Equal;
1213
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Not;
1314
use LastDragon_ru\LaraASP\GraphQL\SearchBy\SearchBuilder;
@@ -42,11 +43,14 @@ public function testApply(
4243
$this->expectExceptionObject($expected);
4344
}
4445

45-
$search = new SearchBuilder([
46-
$this->app->make(Not::class),
47-
$this->app->make(Equal::class),
48-
$this->app->make(Relation::class),
49-
]);
46+
$search = new SearchBuilder(
47+
$this->app->make(PackageTranslator::class),
48+
[
49+
$this->app->make(Not::class),
50+
$this->app->make(Equal::class),
51+
$this->app->make(Relation::class),
52+
],
53+
);
5054
$relation = $this->app->make(Relation::class);
5155
$builder = $builder($this);
5256
$builder = $relation->apply($search, $builder, $property, $conditions);
@@ -65,8 +69,8 @@ public function dataProviderApply(): array {
6569
return [
6670
'query builder not supported' => [
6771
new SearchLogicException(sprintf(
68-
'Operator `%s` can not be used with `%s`.',
69-
(new Relation())->getName(),
72+
'Operator `%s` cannot be used with `%s`.',
73+
'where',
7074
QueryBuilder::class,
7175
)),
7276
static function (self $test): QueryBuilder {

src/SearchBy/SearchBuilder.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
66
use Illuminate\Database\Query\Builder as QueryBuilder;
77
use InvalidArgumentException;
8+
use LastDragon_ru\LaraASP\GraphQL\PackageTranslator;
89
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\OperatorNegationable;
910
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\ComparisonOperator;
1011
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Complex\ComplexOperator;
@@ -16,7 +17,6 @@
1617
use function implode;
1718
use function key;
1819
use function reset;
19-
use function sprintf;
2020

2121
class SearchBuilder {
2222
/**
@@ -39,7 +39,10 @@ class SearchBuilder {
3939
* |\LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Logical\LogicalOperator
4040
* |\LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Complex\ComplexOperator> $operators
4141
*/
42-
public function __construct(array $operators) {
42+
public function __construct(
43+
protected PackageTranslator $translator,
44+
array $operators,
45+
) {
4346
foreach ($operators as $operator) {
4447
if ($operator instanceof ComparisonOperator) {
4548
$this->comparison[$operator->getName()] = $operator;
@@ -84,9 +87,11 @@ public function process(
8487

8588
// More than one property?
8689
if (count($input) > 1) {
87-
throw new SearchLogicException(sprintf(
88-
'Only one property allowed, found: `%s`.',
89-
implode('`, `', array_keys($input)),
90+
throw new SearchLogicException($this->translator->get(
91+
'search_by.errors.too_many_properties',
92+
[
93+
'properties' => implode('`, `', array_keys($input)),
94+
],
9095
));
9196
}
9297

@@ -216,16 +221,18 @@ public function processComparison(
216221

217222
// Empty?
218223
if (count($conditions) <= 0) {
219-
throw new SearchLogicException(
220-
'Search condition cannot be empty.',
221-
);
224+
throw new SearchLogicException($this->translator->get(
225+
'search_by.errors.empty_condition',
226+
));
222227
}
223228

224229
// More than one operator?
225230
if (count($conditions) > 1) {
226-
throw new SearchLogicException(sprintf(
227-
'Only one comparison operator allowed, found: `%s`.',
228-
implode('`, `', array_keys($conditions)),
231+
throw new SearchLogicException($this->translator->get(
232+
'search_by.errors.too_many_operators',
233+
[
234+
'operators' => implode('`, `', array_keys($conditions)),
235+
],
229236
));
230237
}
231238

@@ -236,18 +243,22 @@ public function processComparison(
236243

237244
// Found?
238245
if (!$operator) {
239-
throw new SearchLogicException(sprintf(
240-
'Operator `%s` not found.',
241-
$name,
246+
throw new SearchLogicException($this->translator->get(
247+
'search_by.errors.unknown_operator',
248+
[
249+
'operator' => $name,
250+
],
242251
));
243252
}
244253

245254
// Not allowed?
246255
if ($not && !($operator instanceof OperatorNegationable)) {
247-
throw new SearchLogicException(sprintf(
248-
'Operator `%s` cannot be used with `%s`.',
249-
$name,
250-
Not::Name,
256+
throw new SearchLogicException($this->translator->get(
257+
'search_by.errors.unsupported_option',
258+
[
259+
'operator' => $name,
260+
'option' => Not::Name,
261+
],
251262
));
252263
}
253264

0 commit comments

Comments
 (0)