Skip to content

Commit 4c4c310

Browse files
authored
Generate full API Platform IRI as TS type literal (#53)
1 parent 2b993c5 commit 4c4c310

File tree

6 files changed

+61
-6
lines changed

6 files changed

+61
-6
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"webmozart/assert": "^1.10",
2525
"jfcherng/php-diff": "^6.10",
2626
"jawira/case-converter": "^3.4",
27-
"phpstan/phpdoc-parser": "^1.6"
27+
"phpstan/phpdoc-parser": "^1.6",
28+
"doctrine/inflector": "^2.0"
2829
},
2930
"license": "GPL-3.0-or-later",
3031
"autoload": {

src/Bridge/ApiPlatform/ApiPlatformInputTypeResolver.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616

1717
class ApiPlatformInputTypeResolver implements UnknownTypeResolverInterface
1818
{
19+
private ApiPlatformIriGenerator $apiPlatformIriGenerator;
20+
1921
public function __construct(
2022
/** @var array<string, string> */
2123
private array $classMap = [],
2224
// https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html
2325
private bool $useTypeScriptTemplateLiteral = false,
26+
private bool $useApiPlatformIriGenerator = false,
2427
) {
28+
$this->apiPlatformIriGenerator = new ApiPlatformIriGenerator();
2529
}
2630

2731
public function supports(PhpUnknownType $type, DtoType|null $dto, DtoList $dtoList): bool
@@ -62,7 +66,11 @@ public function resolve(PhpUnknownType $type, DtoType|null $dto, DtoList $dtoLis
6266
}
6367

6468
if ($this->useTypeScriptTemplateLiteral) {
65-
return '`/api/${string}`';
69+
if ($this->useApiPlatformIriGenerator) {
70+
$pluralizedTypeName = $this->apiPlatformIriGenerator->generate($type);
71+
return sprintf('`/api/%s/${string}`', $pluralizedTypeName);
72+
}
73+
return'`/api/${string}`';
6674
}
6775

6876
return PhpBaseType::string();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Riverwaysoft\DtoConverter\Bridge\ApiPlatform;
6+
7+
use Doctrine\Inflector\Inflector;
8+
use Doctrine\Inflector\InflectorFactory;
9+
use Jawira\CaseConverter\Convert;
10+
use Riverwaysoft\DtoConverter\Dto\PhpType\PhpUnknownType;
11+
12+
class ApiPlatformIriGenerator
13+
{
14+
private Inflector $inflector;
15+
16+
public function __construct()
17+
{
18+
$this->inflector = InflectorFactory::create()->build();
19+
}
20+
21+
public function generate(PhpUnknownType $type): string
22+
{
23+
$pluralized = $this->inflector->pluralize($type->getName());
24+
return (new Convert($pluralized))->toSnake();
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Riverwaysoft\DtoConverter\Bridge\ApiPlatform;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Riverwaysoft\DtoConverter\Dto\PhpType\PhpUnknownType;
9+
10+
class ApiPlatformIriGeneratorTest extends TestCase
11+
{
12+
public function testGeneration(): void
13+
{
14+
$generator = new ApiPlatformIriGenerator();
15+
16+
$this->assertEquals('food_categories', $generator->generate(new PhpUnknownType('FoodCategory')));
17+
$this->assertEquals('users', $generator->generate(new PhpUnknownType('User')));
18+
$this->assertEquals('food_analyze_rule_categories', $generator->generate(new PhpUnknownType('FoodAnalyzeRuleCategory')));
19+
}
20+
}

tests/TypeScriptGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ class UserCreateInput
327327
new ApiPlatformInputTypeResolver([
328328
'LocationEmbeddable' => '{ lat: string; lan: string }',
329329
'Money' => '{ currency: string; amount: number }',
330-
], true),
330+
], true, true),
331331
new ClassNameTypeResolver(),
332332
],
333333
new OutputFilesProcessor([

tests/__snapshots__/TypeScriptGeneratorTest__testApiPlatformInput__2.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ export type ProfileOutput = {
1616
};
1717

1818
export type UserCreateInput = {
19-
profile: `/api/${string}`;
19+
profile: `/api/profiles/${string}`;
2020
promotedAt: string | null;
2121
userTheme: ColorEnum;
22-
industriesUnion: `/api/${string}`[] | null;
23-
industriesNullable: `/api/${string}`[] | null;
22+
industriesUnion: `/api/industries/${string}`[] | null;
23+
industriesNullable: `/api/industries/${string}`[] | null;
2424
money: { currency: string; amount: number };
2525
gender: { value: GenderEnum };
2626
location: { lat: string; lan: string };

0 commit comments

Comments
 (0)