-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from innocenzi/feat/record-type
feat: add support for record types
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Spatie\TypeScriptTransformer\Attributes; | ||
|
||
use Attribute; | ||
use phpDocumentor\Reflection\Type; | ||
use Spatie\TypeScriptTransformer\Types\RecordType; | ||
|
||
#[Attribute] | ||
class RecordTypeScriptType implements TypeScriptTransformableAttribute | ||
{ | ||
private string $keyType; | ||
private string | array $valueType; | ||
private bool $array; | ||
|
||
public function __construct(string $keyType, string | array $valueType, ?bool $array = false) | ||
{ | ||
$this->keyType = $keyType; | ||
$this->valueType = $valueType; | ||
$this->array = $array; | ||
} | ||
|
||
public function getType(): Type | ||
{ | ||
return new RecordType($this->keyType, $this->valueType, $this->array); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Spatie\TypeScriptTransformer\Types; | ||
|
||
use phpDocumentor\Reflection\Type; | ||
use phpDocumentor\Reflection\TypeResolver; | ||
use phpDocumentor\Reflection\Types\Array_; | ||
|
||
/** @psalm-immutable */ | ||
class RecordType implements Type | ||
{ | ||
private Type $keyType; | ||
private Type $valueType; | ||
|
||
public function __construct(string $keyType, string | array $valueType, ?bool $array = false) | ||
{ | ||
$this->keyType = (new TypeResolver())->resolve($keyType); | ||
|
||
if ($array) { | ||
$this->valueType = new Array_((new TypeResolver())->resolve($valueType)); | ||
} else { | ||
$this->valueType = is_array($valueType) | ||
? StructType::fromArray($valueType) | ||
: (new TypeResolver())->resolve($valueType); | ||
} | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return 'record'; | ||
} | ||
|
||
public function getKeyType(): Type | ||
{ | ||
return $this->keyType; | ||
} | ||
|
||
public function getValueType(): Type | ||
{ | ||
return $this->valueType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
use phpDocumentor\Reflection\Fqsen; | ||
use phpDocumentor\Reflection\Types\Array_; | ||
use phpDocumentor\Reflection\Types\Integer; | ||
use phpDocumentor\Reflection\Types\Object_; | ||
use phpDocumentor\Reflection\Types\String_; | ||
use function PHPUnit\Framework\assertEquals; | ||
use function PHPUnit\Framework\assertInstanceOf; | ||
use Spatie\TypeScriptTransformer\Tests\FakeClasses\BackedEnumWithoutAnnotation; | ||
use Spatie\TypeScriptTransformer\Tests\FakeClasses\Enum\RegularEnum; | ||
|
||
use Spatie\TypeScriptTransformer\Types\RecordType; | ||
use Spatie\TypeScriptTransformer\Types\StructType; | ||
|
||
it('creates a scalar key and an object value', function () { | ||
$record = new RecordType('string', RegularEnum::class); | ||
|
||
assertInstanceOf(RecordType::class, $record); | ||
assertEquals(new String_(), $record->getKeyType()); | ||
assertEquals(new Object_(new Fqsen('\\'.RegularEnum::class)), $record->getValueType()); | ||
}); | ||
|
||
it('creates a scalar key and an struct value', function () { | ||
$record = new RecordType('string', [ | ||
'enum' => RegularEnum::class, | ||
'array' => 'int[]', | ||
]); | ||
|
||
assertInstanceOf(RecordType::class, $record); | ||
assertEquals(new String_(), $record->getKeyType()); | ||
|
||
assertInstanceOf(StructType::class, $record->getValueType()); | ||
assertEquals([ | ||
'enum' => new Object_(new Fqsen('\\'.RegularEnum::class)), | ||
'array' => new Array_(new Integer()), | ||
], $record->getValueType()->getTypes()); | ||
}); | ||
|
||
it('creates a scalar key and an array value', function () { | ||
$record = new RecordType(RegularEnum::class, BackedEnumWithoutAnnotation::class, array: true); | ||
|
||
assertInstanceOf(RecordType::class, $record); | ||
assertEquals(new Object_(new Fqsen('\\'.RegularEnum::class)), $record->getKeyType()); | ||
assertEquals(new Array_(new Object_(new Fqsen('\\'.BackedEnumWithoutAnnotation::class))), $record->getValueType()); | ||
}); |