-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathDescriptor.php
More file actions
96 lines (85 loc) Β· 2.8 KB
/
Descriptor.php
File metadata and controls
96 lines (85 loc) Β· 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
namespace PHPJava\Compiler\Builder\Signatures;
use PHPJava\Kernel\Resolvers\TypeResolver;
use PHPJava\Kernel\Types\Byte_;
use PHPJava\Kernel\Types\Char_;
use PHPJava\Kernel\Types\Double_;
use PHPJava\Kernel\Types\Float_;
use PHPJava\Kernel\Types\Int_;
use PHPJava\Kernel\Types\Long_;
use PHPJava\Kernel\Types\Short_;
use PHPJava\Kernel\Types\Void_;
use PHPJava\Utilities\Formatter;
class Descriptor implements DescriptorInterface
{
protected $return;
protected $arguments = [];
public static function factory()
{
return new static();
}
public function addArgument(string $type, int $dimensionsOfArray = 0): self
{
$this->arguments[] = [$type, $dimensionsOfArray];
return $this;
}
public function setReturn(string $type): self
{
$this->return = $type;
return $this;
}
public function make(): string
{
$arguments = implode(
array_map(
static function (array $argument) {
[$type, $dimensionsOfArray] = $argument;
$string = str_repeat('[', $dimensionsOfArray);
$type = Formatter::convertPHPPrimitiveTypeToJavaType(
ltrim($type, '\\')
);
switch ($type) {
case Char_::class:
case Byte_::class:
case Double_::class:
case Float_::class:
case Long_::class:
case Short_::class:
case Int_::class:
case Void_::class:
$string .= TypeResolver::resolveSignatureByType($type);
break;
default:
$path = Formatter::convertPHPNamespacesToJava(
$type,
'/'
);
$string .= 'L' . $path . ';';
break;
}
return $string;
},
$this->arguments
)
);
if ($this->return === null) {
return $arguments;
}
$returnSignature = Formatter::convertPrimitiveValueToJavaSignature(
$this->return
);
if ($returnSignature === null) {
$returnSignature = TypeResolver::resolve(
Formatter::convertPHPNamespacesToJava(
$this->return,
'/'
)
);
if ($returnSignature[0] === 'L') {
$returnSignature .= ';';
}
}
return '(' . $arguments . ')' . $returnSignature;
}
}