Skip to content

Commit 1c3c589

Browse files
authored
Merge pull request #4 from jmsariron/master
Better handle nullables for frontend devs
2 parents 2e99fd0 + 83bea4a commit 1c3c589

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

ParseTypeScript/Parser.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ public function __construct(string $filePath)
7575

7676
$type = $this->parsePhpDocForProperty($property);
7777

78-
$isNull = '';
78+
$isNull = false;
7979
if (preg_match('/nullable=true/i', $property->getDocComment(), $matches)) {
80-
$isNull = '?';
80+
$isNull = true;
8181
}
8282

8383
if (empty($isNull) && is_null($property->getType()) !== true && $property->getType()->allowsNull()) {
84-
$isNull = '?';
84+
$isNull = true;
8585
}
8686

87-
$this->currentInterface->properties[] = new TypeScriptProperty($property->getName() . $isNull, $type);
87+
$this->currentInterface->properties[] = new TypeScriptProperty($property->getName(), $type, $isNull);
8888
}
8989

9090
$this->output[] = $this->currentInterface;

ParseTypeScript/TypeScriptBaseInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __toString()
4242
}
4343
}
4444

45-
$pieces[] = " " . $property->name . ": " . $property->type . ";";
45+
$pieces[] = ' ' . (string) $property . ';';
4646
}
4747

4848
$result = "";

ParseTypeScript/TypeScriptProperty.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@ class TypeScriptProperty
2323
*/
2424
public $type;
2525

26-
public function __construct(string $name, string $type = 'unknown')
26+
/**
27+
* @var bool
28+
*/
29+
public $isNullable;
30+
31+
public function __construct(string $name, string $type = 'unknown', bool $isNullable = false)
2732
{
2833
$this->name = $name;
2934
$this->type = $type;
35+
$this->isNullable = $isNullable;
3036
}
3137

3238
public function __toString()
3339
{
34-
return $this->name . ': ' . $this->type;
40+
return $this->name . '?: ' . $this->type . ($this->isNullable ? ' | null' : '') ;
3541
}
3642

3743
}

0 commit comments

Comments
 (0)