-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathNormalizer.php
More file actions
122 lines (112 loc) Β· 3.66 KB
/
Normalizer.php
File metadata and controls
122 lines (112 loc) Β· 3.66 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace PHPJava\Kernel\Filters;
use PHPJava\Core\JavaClass;
use PHPJava\Core\JavaClassInterface;
use PHPJava\Exceptions\NormalizerException;
use PHPJava\Kernel\Resolvers\TypeResolver;
use PHPJava\Kernel\Structures\FieldInfo;
use PHPJava\Kernel\Types\Array_\Collection;
use PHPJava\Kernel\Types\PrimitiveValueInterface;
use PHPJava\Kernel\Types\Type;
use PHPJava\Utilities\Formatter;
class Normalizer
{
/**
* @param array|Collection $values
* @throws NormalizerException
* @return array|Collection
*/
public static function normalizeValues($values, array $normalizeTypes)
{
if (count($values) !== count($normalizeTypes)) {
throw new NormalizerException('Does not match arguments.');
}
foreach ($values as $key => &$value) {
$realType = $normalizeTypes[$key] ?? null;
if ($realType === null) {
throw new NormalizerException('Broken arguments parser.');
}
/**
* @var Collection|Type $value
*/
if ($value instanceof Collection) {
$value = static::normalizeValues(
$value,
array_fill(
0,
count($value),
$realType
)
);
continue;
}
if (!TypeResolver::isPrimitive($realType['type'])) {
// TODO: implements up-cast and down-cast
continue;
}
$initiateClass = $realType['type'];
if ($value instanceof $initiateClass) {
continue;
}
/**
* @var Type $initiateClass
*/
$value = $initiateClass::get(
static::getPrimitiveValue($value)
);
}
return $values;
}
public static function normalizeFields(array $fields, JavaClassInterface $fromJavaClass = null): array
{
$newFields = [];
foreach ($fields as $name => $value) {
/**
* @var FieldInfo $value
*/
$cp = $value->getConstantPool();
$descriptor = Formatter::parseSignature($cp[$value->getDescriptorIndex()]->getString());
[$type, $class, $dimensionsOfArray] = TypeResolver::getType($descriptor[0]);
switch ($type) {
case TypeResolver::IS_PRIMITIVE:
$newFields[$name] = $class::get();
break;
case TypeResolver::IS_CLASS:
$newFields[$name] = JavaClass::load('java.lang.Object');
break;
case TypeResolver::IS_ARRAY:
$newFields[$name] = null;
break;
default:
throw new NormalizerException('Failed to normalize fields.');
}
}
return $newFields;
}
/**
* @throws \PHPJava\Exceptions\TypeException
* @return Type
*/
public static function normalizeReturnValue($value, array $signatureArray)
{
/**
* @var Type $typeClass
*/
[$type, $typeClass] = TypeResolver::getType($signatureArray);
return $type === TypeResolver::IS_PRIMITIVE
? $typeClass::get($value)
: $value;
}
/**
* @param $value
* @return bool|float|int|string
*/
public static function getPrimitiveValue($value)
{
if ($value instanceof PrimitiveValueInterface) {
return TypeResolver::extractPrimitiveValueFromType($value);
}
return $value;
}
}