Skip to content

Commit f68f0e5

Browse files
committed
Merge branch 'compact-types' into dev
# Conflicts: # src/Structures/TransformedType.php # src/Transformers/DtoTransformer.php # src/Transformers/EnumTransformer.php # src/Transformers/MyclabsEnumTransformer.php # src/Transformers/SpatieEnumTransformer.php # tests/Fakes/FakeTransformedType.php # tests/Fakes/FakeTypeScriptCollector.php
2 parents 9ce55c3 + 9771dca commit f68f0e5

16 files changed

+277
-8
lines changed

src/Collectors/Collector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
namespace Spatie\TypeScriptTransformer\Collectors;
44

55
use ReflectionClass;
6+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
67
use Spatie\TypeScriptTransformer\Structures\TransformedType;
78
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
89

910
abstract class Collector
1011
{
1112
protected TypeScriptTransformerConfig $config;
1213

14+
protected ConfigCompactor $compactor;
15+
1316
public function __construct(TypeScriptTransformerConfig $config)
1417
{
1518
$this->config = $config;
19+
$this->compactor = new ConfigCompactor($config);
1620
}
1721

1822
abstract public function getTransformedType(ReflectionClass $class): ?TransformedType;

src/Collectors/DefaultCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected function resolveAlreadyTransformedType(ClassTypeReflector $reflector):
4949
$reflector->getReflectionClass(),
5050
$reflector->getName(),
5151
$transpiler->execute($reflector->getType()),
52+
$this->compactor,
5253
$missingSymbols
5354
);
5455
}

src/Compactors/Compactor.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Spatie\TypeScriptTransformer\Compactors;
4+
5+
/**
6+
* Shortens namespace of a typescript identifier
7+
*/
8+
interface Compactor
9+
{
10+
public function removeSuffix(string $typeName): string;
11+
12+
public function removePrefix(string $namespace): string;
13+
}

src/Compactors/ConfigCompactor.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Spatie\TypeScriptTransformer\Compactors;
4+
5+
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
6+
7+
class ConfigCompactor implements Compactor
8+
{
9+
10+
protected ?array $prefixes = null;
11+
12+
protected ?array $suffixes = null;
13+
14+
protected TypeScriptTransformerConfig $config;
15+
16+
public function __construct(TypeScriptTransformerConfig $config) {
17+
$this->config = $config;
18+
}
19+
20+
/**
21+
* @return string[]
22+
*/
23+
protected function getPrefixes(): array {
24+
if ($this->prefixes === null) {
25+
$this->prefixes = array_map(
26+
function (string $prefix): string {
27+
if (str_ends_with($prefix, "\\")) {
28+
$prefix = rtrim($prefix, "\\");
29+
}
30+
return $prefix;
31+
},
32+
$this->config->getCompactorPrefixes()
33+
);
34+
}
35+
return $this->prefixes;
36+
}
37+
38+
/**
39+
* @return string[]
40+
*/
41+
protected function getSuffixes(): array {
42+
if ($this->suffixes === null) {
43+
$this->suffixes = $this->config->getCompactorSuffixes();
44+
}
45+
return $this->suffixes;
46+
}
47+
48+
public function removeSuffix(string $typeName): string {
49+
$matchingSuffix = '';
50+
foreach ($this->getSuffixes() as $suffix) {
51+
if (str_ends_with($typeName, $suffix)) {
52+
$matchingSuffix = $suffix;
53+
break;
54+
}
55+
}
56+
if ($matchingSuffix !== '') {
57+
$typeName = substr($typeName, 0, -strlen($matchingSuffix));
58+
}
59+
return $typeName;
60+
}
61+
62+
public function removePrefix(string $namespace): string {
63+
$matchingPrefix = '';
64+
foreach ($this->getPrefixes() as $prefix) {
65+
if (str_starts_with($namespace, $prefix)) {
66+
$matchingPrefix = $prefix;
67+
break;
68+
}
69+
}
70+
$substr = substr($namespace, strlen($matchingPrefix));
71+
return ltrim($substr, '\\');
72+
}
73+
74+
}

src/Compactors/IdentityCompactor.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Spatie\TypeScriptTransformer\Compactors;
4+
5+
class IdentityCompactor implements Compactor
6+
{
7+
8+
public function removeSuffix(string $typeName): string {
9+
return $typeName;
10+
}
11+
12+
public function removePrefix(string $namespace): string {
13+
return $namespace;
14+
}
15+
}

src/Structures/TransformedType.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Spatie\TypeScriptTransformer\Structures;
44

55
use ReflectionClass;
6+
use Spatie\TypeScriptTransformer\Compactors\Compactor;
67

78
class TransformedType
89
{
@@ -14,6 +15,8 @@ class TransformedType
1415

1516
public MissingSymbolsCollection $missingSymbols;
1617

18+
public Compactor $compactor;
19+
1720
public bool $isInline;
1821

1922
public string $keyword;
@@ -24,26 +27,29 @@ public static function create(
2427
ReflectionClass $class,
2528
string $name,
2629
TranspilationResult $transformed,
30+
Compactor $compactor,
2731
?MissingSymbolsCollection $missingSymbols = null,
2832
bool $inline = false,
2933
string $keyword = 'type',
3034
bool $trailingSemicolon = true,
3135
): self {
32-
return new self($class, $name, $transformed, $missingSymbols ?? new MissingSymbolsCollection(), $inline, $keyword, $trailingSemicolon);
36+
return new self($class, $compactor->removeSuffix($name), $transformed, $compactor, $missingSymbols ?? new MissingSymbolsCollection(), $inline, $keyword, $trailingSemicolon);
3337
}
3438

3539
public static function createInline(
3640
ReflectionClass $class,
3741
TranspilationResult $transformed,
42+
Compactor $compactor,
3843
?MissingSymbolsCollection $missingSymbols = null
3944
): self {
40-
return new self($class, null, $transformed, $missingSymbols ?? new MissingSymbolsCollection(), true);
45+
return new self($class, null, $transformed, $compactor, $missingSymbols ?? new MissingSymbolsCollection(), true);
4146
}
4247

4348
public function __construct(
4449
ReflectionClass $class,
4550
?string $name,
4651
TranspilationResult $transformed,
52+
Compactor $compactor,
4753
MissingSymbolsCollection $missingSymbols,
4854
bool $isInline,
4955
string $keyword = 'type',
@@ -53,6 +59,7 @@ public function __construct(
5359
$this->name = $name;
5460
$this->transformed = $transformed;
5561
$this->missingSymbols = $missingSymbols;
62+
$this->compactor = $compactor;
5663
$this->isInline = $isInline;
5764
$this->keyword = $keyword;
5865
$this->trailingSemicolon = $trailingSemicolon;
@@ -70,6 +77,12 @@ public function getNamespaceSegments(): array
7077
return [];
7178
}
7279

80+
$namespace = $this->compactor->removePrefix($namespace);
81+
82+
if ($namespace === '') {
83+
return [];
84+
}
85+
7386
return explode('\\', $namespace);
7487
}
7588

src/Transformers/DtoTransformer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ReflectionProperty;
77
use Spatie\TypeScriptTransformer\Attributes\Hidden;
88
use Spatie\TypeScriptTransformer\Attributes\Optional;
9+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
910
use Spatie\TypeScriptTransformer\Structures\MissingSymbolsCollection;
1011
use Spatie\TypeScriptTransformer\Structures\TransformedType;
1112
use Spatie\TypeScriptTransformer\Structures\TranspilationResult;
@@ -19,9 +20,12 @@ class DtoTransformer implements Transformer
1920

2021
protected TypeScriptTransformerConfig $config;
2122

23+
protected ConfigCompactor $compactor;
24+
2225
public function __construct(TypeScriptTransformerConfig $config)
2326
{
2427
$this->config = $config;
28+
$this->compactor = new ConfigCompactor($config);
2529
}
2630

2731
public function transform(ReflectionClass $class, string $name): ?TransformedType
@@ -52,6 +56,7 @@ public function transform(ReflectionClass $class, string $name): ?TransformedTyp
5256
),
5357
"{" . PHP_EOL . $type . "}"
5458
),
59+
$this->compactor,
5560
$missingSymbols
5661
);
5762
}

src/Transformers/EnumTransformer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
use ReflectionClass;
66
use ReflectionEnum;
77
use ReflectionEnumBackedCase;
8+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
89
use Spatie\TypeScriptTransformer\Structures\TransformedType;
910
use Spatie\TypeScriptTransformer\Structures\TranspilationResult;
1011
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
1112

1213
class EnumTransformer implements Transformer
1314
{
15+
protected ConfigCompactor $compactor;
16+
1417
public function __construct(protected TypeScriptTransformerConfig $config)
1518
{
19+
$this->compactor = new ConfigCompactor($config);
1620
}
1721

1822
public function transform(ReflectionClass $class, string $name): ?TransformedType
@@ -49,6 +53,7 @@ protected function toEnum(ReflectionEnum $enum, string $name): TransformedType
4953
TranspilationResult::noDeps(
5054
implode(', ', $options),
5155
),
56+
$this->compactor,
5257
keyword: 'enum'
5358
);
5459
}
@@ -65,7 +70,8 @@ protected function toType(ReflectionEnum $enum, string $name): TransformedType
6570
$name,
6671
TranspilationResult::noDeps(
6772
implode(' | ', $options)
68-
)
73+
),
74+
$this->compactor
6975
);
7076
}
7177

src/Transformers/MyclabsEnumTransformer.php

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

55
use MyCLabs\Enum\Enum;
66
use ReflectionClass;
7+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
78
use Spatie\TypeScriptTransformer\Structures\TransformedType;
89
use Spatie\TypeScriptTransformer\Structures\TranspilationResult;
910
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
1011

1112
class MyclabsEnumTransformer implements Transformer
1213
{
14+
protected ConfigCompactor $compactor;
15+
1316
public function __construct(protected TypeScriptTransformerConfig $config)
1417
{
18+
$this->compactor = new ConfigCompactor($config);
1519
}
1620

1721
public function transform(ReflectionClass $class, string $name): ?TransformedType
@@ -42,6 +46,7 @@ protected function toEnum(ReflectionClass $class, string $name): TransformedType
4246
TranspilationResult::noDeps(
4347
implode(', ', $options),
4448
),
49+
$this->compactor,
4550
keyword: 'enum'
4651
);
4752
}
@@ -61,7 +66,8 @@ protected function toType(ReflectionClass $class, string $name): TransformedType
6166
$name,
6267
TranspilationResult::noDeps(
6368
implode(' | ', $options)
64-
)
69+
),
70+
$this->compactor
6571
);
6672
}
6773
}

src/Transformers/SpatieEnumTransformer.php

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

55
use ReflectionClass;
66
use Spatie\Enum\Enum;
7+
use Spatie\TypeScriptTransformer\Compactors\ConfigCompactor;
78
use Spatie\TypeScriptTransformer\Structures\TransformedType;
89
use Spatie\TypeScriptTransformer\Structures\TranspilationResult;
910
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
1011

1112
class SpatieEnumTransformer implements Transformer
1213
{
14+
protected ConfigCompactor $compactor;
15+
1316
public function __construct(protected TypeScriptTransformerConfig $config)
1417
{
18+
$this->compactor = new ConfigCompactor($config);
1519
}
1620

1721
public function transform(ReflectionClass $class, string $name): ?TransformedType
@@ -42,6 +46,7 @@ protected function toEnum(ReflectionClass $class, string $name): TransformedType
4246
TranspilationResult::noDeps(
4347
implode(', ', $options),
4448
),
49+
$this->compactor,
4550
keyword: 'enum'
4651
);
4752
}
@@ -61,7 +66,8 @@ private function toType(ReflectionClass $class, string $name): TransformedType
6166
$name,
6267
TranspilationResult::noDeps(
6368
implode(' | ', $options)
64-
)
69+
),
70+
$this->compactor
6571
);
6672
}
6773
}

0 commit comments

Comments
 (0)