2020
2121class TypeResolver
2222{
23+ const IS_CLASS = 'class ' ;
24+ const IS_PRIMITIVE = 'primitive ' ;
25+
2326 const PHP_TYPE_MAP = [
2427 'integer ' => 'I ' ,
2528 'float ' => 'F ' ,
@@ -63,12 +66,10 @@ class TypeResolver
6366 const PHP_SCALAR_MAP = [
6467 // [ TypeClass, Instantiation ]
6568 'string ' => [_String::class, true ],
66- 'float ' => [_Float::class, true ],
67- 'double ' => [_Float::class, true ],
68- 'int ' => [_Int::class, true ],
69- 'integer ' => [_Int::class, true ],
70- 'bool ' => [_Boolean::class, true ],
71- 'boolean ' => [_Boolean::class, true ],
69+ 'float ' => [_Float::class, false ],
70+ 'double ' => [_Float::class, false ],
71+ 'integer ' => [_Int::class, false ],
72+ 'boolean ' => [_Boolean::class, false ],
7273 ];
7374
7475 const PHP_TO_JAVA_MAP = [
@@ -82,7 +83,7 @@ class TypeResolver
8283 * @param $signature
8384 * @throws TypeException
8485 */
85- public static function getMappedSignatureType ($ signature ): string
86+ public static function getMappedSignatureType (string $ signature ): string
8687 {
8788 if (isset (static ::SIGNATURE_MAP [$ signature ])) {
8889 return static ::SIGNATURE_MAP [$ signature ];
@@ -93,7 +94,7 @@ public static function getMappedSignatureType($signature): string
9394 /**
9495 * @param $type
9596 */
96- public static function resolve ($ type ): string
97+ public static function resolve (string $ type ): string
9798 {
9899 $ flipped = array_flip (static ::SIGNATURE_MAP );
99100 if (isset ($ flipped [$ type ])) {
@@ -106,6 +107,47 @@ public static function resolve($type): string
106107 return 'L ' . $ type ;
107108 }
108109
110+ public static function extractPrimitiveValueFromType (Type $ value )
111+ {
112+ $ extractedValue = (string ) $ value ->getValue ();
113+ if ($ value instanceof _Int || $ value instanceof _Long) {
114+ return (int ) $ extractedValue ;
115+ }
116+ if ($ value instanceof _Float || $ value instanceof _Double) {
117+ return (float ) $ extractedValue ;
118+ }
119+ if ($ value instanceof _Boolean) {
120+ return $ extractedValue === 'true ' ? true : false ;
121+ }
122+
123+ return $ extractedValue ;
124+ }
125+
126+ /**
127+ * @param array $signatureArray a formatted signature array
128+ * @throws TypeException
129+ * @return string
130+ */
131+ public static function getType (array $ signatureArray )
132+ {
133+ $ type = $ signatureArray ['type ' ];
134+ if ($ type === 'class ' ) {
135+ $ className = Runtime::PHP_PACKAGES_DIRECTORY . '\\' . str_replace ('/ ' , '\\' , $ signatureArray ['class_name ' ]);
136+ return [
137+ static ::IS_CLASS ,
138+ $ className ,
139+ ];
140+ }
141+ $ result = static ::TYPES_MAP [strtolower ($ type )] ?? null ;
142+ if ($ result === null ) {
143+ throw new TypeException ('Unknown type: ' . $ type );
144+ }
145+ return [
146+ static ::IS_PRIMITIVE ,
147+ $ result ,
148+ ];
149+ }
150+
109151 /**
110152 * @param $value
111153 * @return bool|mixed|string
@@ -123,17 +165,16 @@ public static function resolveFromPHPType($value)
123165 /**
124166 * @param $type
125167 */
126- public static function convertJavaTypeSimplyForPHP ($ type ): string
168+ public static function convertJavaTypeSimplyForPHP (string $ type ): string
127169 {
128170 return static ::AMBIGUOUS_TYPES_ON_PHP [$ type ] ?? $ type ;
129171 }
130172
131173 /**
132174 * @param $arguments
133- * @param string $defaultJavaArgumentType
134175 * @throws TypeException
135176 */
136- public static function convertPHPtoJava ($ arguments , $ defaultJavaArgumentType = 'java.lang.String ' ): array
177+ public static function convertPHPtoJava ($ arguments , string $ defaultJavaArgumentType = 'java.lang.String ' ): array
137178 {
138179 $ phpType = gettype ($ arguments );
139180 $ deepArray = 0 ;
@@ -245,7 +286,7 @@ public static function compare(string $a, string $b): bool
245286 * @throws TypeException
246287 * @throws \ReflectionException
247288 */
248- public static function getExtendedClasses ($ class ): array
289+ public static function getExtendedClasses (string $ class ): array
249290 {
250291 static $ loadedExtendedRoots = [];
251292 $ result = [];
0 commit comments