|
1 | 1 | <?php |
2 | 2 | namespace PHPJava\Utilities; |
3 | 3 |
|
4 | | -use PHPJava\Exceptions\ConverterException; |
| 4 | +use PHPJava\Exceptions\NormalizerException; |
5 | 5 | use PHPJava\Kernel\Types\_Array\Collection; |
6 | 6 | use PHPJava\Kernel\Types\Type; |
7 | 7 |
|
8 | 8 | class Normalizer |
9 | 9 | { |
10 | 10 |
|
11 | 11 | /** |
12 | | - * @param array $arguments |
13 | | - * @param array $acceptedArguments |
| 12 | + * @param array|Collection $values |
| 13 | + * @param array $normalizeTypes |
14 | 14 | * @return array |
15 | | - * @throws ConverterException |
| 15 | + * @throws NormalizerException |
16 | 16 | */ |
17 | | - public static function normalizeArguments(array $arguments, array $acceptedArguments): array |
| 17 | + public static function normalizeValues($values, array $normalizeTypes) |
18 | 18 | { |
19 | | - if (count($arguments) !== count($acceptedArguments)) { |
20 | | - throw new ConverterException('Does not match arguments.'); |
| 19 | + if (count($values) !== count($normalizeTypes)) { |
| 20 | + throw new NormalizerException('Does not match arguments.'); |
21 | 21 | } |
22 | 22 |
|
23 | | - foreach ($arguments as $key => &$argument) { |
| 23 | + foreach ($values as $key => &$value) { |
| 24 | + $realType = $normalizeTypes[$key] ?? null; |
| 25 | + if ($realType === null) { |
| 26 | + throw new NormalizerException('Broken arguments parser.'); |
| 27 | + } |
24 | 28 | /** |
25 | | - * @var Type|Collection $argument |
| 29 | + * @var Type|Collection $value |
26 | 30 | */ |
27 | | - if ($argument instanceof Collection) { |
28 | | - // TODO: convert an array contents. |
| 31 | + if ($value instanceof Collection) { |
| 32 | + $value = static::normalizeValues( |
| 33 | + $value, |
| 34 | + array_fill( |
| 35 | + 0, |
| 36 | + count($value), |
| 37 | + $realType |
| 38 | + ) |
| 39 | + ); |
29 | 40 | continue; |
30 | 41 | } |
31 | | - $realType = $acceptedArguments[$key] ?? null; |
32 | | - if ($realType === null) { |
33 | | - throw new ConverterException('Broken arguments parser.'); |
34 | | - } |
35 | 42 | if ($realType['type'] === 'class') { |
36 | 43 | // TODO: implements up-cast and down-cast |
37 | 44 | continue; |
38 | 45 | } |
39 | 46 | $initiateClass = TypeResolver::TYPES_MAP[$realType['type']]; |
40 | | - if ($argument instanceof $initiateClass) { |
| 47 | + if ($value instanceof $initiateClass) { |
41 | 48 | continue; |
42 | 49 | } |
43 | | - $argument = new $initiateClass(Extractor::realValue($argument)); |
| 50 | + $value = new $initiateClass( |
| 51 | + Extractor::realValue($value) |
| 52 | + ); |
44 | 53 | } |
45 | 54 |
|
46 | | - return $arguments; |
| 55 | + return $values; |
47 | 56 | } |
48 | 57 | } |
0 commit comments