Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
10 / 11 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ImportStatement | |
90.91% |
10 / 11 |
|
80.00% |
4 / 5 |
7.04 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toTypescript | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| toJavascript | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| providesDefinitions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| needsDefinitions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\TypescriptCodeBuilder\Dto; |
| 3 | |
| 4 | use Apie\TypescriptCodeBuilder\Lists\JavascriptIdentifierList; |
| 5 | use Apie\TypescriptCodeBuilder\TypescriptFileExpressionInterface; |
| 6 | |
| 7 | class ImportStatement implements TypescriptFileExpressionInterface |
| 8 | { |
| 9 | public function __construct( |
| 10 | public string $source, |
| 11 | public JavascriptIdentifierList $imports, |
| 12 | public bool $typeOnly = false, |
| 13 | ) { |
| 14 | } |
| 15 | |
| 16 | public function toTypescript(): string |
| 17 | { |
| 18 | $imports = implode(', ', array_map( |
| 19 | static fn (\Apie\TypescriptCodeBuilder\ValueObjects\JavascriptIdentifier $import): string => $import->toNative(), |
| 20 | $this->imports->toArray(), |
| 21 | )); |
| 22 | $type = $this->typeOnly ? ' type' : ''; |
| 23 | $source = json_encode($this->source, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES); |
| 24 | return 'import' . $type . ' { ' . $imports . ' } from ' . $source . ';'; |
| 25 | } |
| 26 | |
| 27 | public function toJavascript(): string |
| 28 | { |
| 29 | return $this->typeOnly ? '' : $this->toTypescript(); |
| 30 | } |
| 31 | |
| 32 | public function providesDefinitions(): JavascriptIdentifierList |
| 33 | { |
| 34 | return $this->imports; |
| 35 | } |
| 36 | |
| 37 | public function needsDefinitions(): JavascriptIdentifierList |
| 38 | { |
| 39 | return new JavascriptIdentifierList(); |
| 40 | } |
| 41 | } |