Skip to content

Commit 4e53dff

Browse files
authored
Merge pull request #125 from php-java/rename-converter-to-normalizer
Rename from converter to normalizer
2 parents a720ffc + 10c589d commit 4e53dff

File tree

6 files changed

+77
-57
lines changed

6 files changed

+77
-57
lines changed

src/Exceptions/ConverterException.php

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace PHPJava\Exceptions;
3+
4+
class NormalizerException extends \Exception
5+
{
6+
}

src/Kernel/Mnemonics/_invokevirtual.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PHPJava\Utilities\AttributionResolver;
1111
use PHPJava\Utilities\BinaryTool;
1212
use PHPJava\Utilities\ClassResolver;
13-
use PHPJava\Utilities\Converter;
13+
use PHPJava\Utilities\Normalizer;
1414
use PHPJava\Utilities\Formatter;
1515
use PHPJava\Utilities\TypeResolver;
1616

@@ -37,7 +37,7 @@ public function execute(): void
3737
$arguments[$i] = $this->popFromOperandStack();
3838
}
3939

40-
$arguments = Converter::normalizeArguments(
40+
$arguments = Normalizer::normalizeValues(
4141
$arguments,
4242
$signature['arguments']
4343
);

src/Kernel/Types/_Array/Collection.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
use PHPJava\Utilities\Extractor;
77
use PHPJava\Utilities\TypeResolver;
88

9-
class Collection implements \ArrayAccess
9+
class Collection implements \ArrayAccess, \Countable, \IteratorAggregate
1010
{
1111
private $data;
12+
private $position = 0;
1213

1314
public function __construct(array &$data, string $type = null)
1415
{
@@ -54,4 +55,14 @@ public function offsetSet($offset, $value)
5455
{
5556
$this->data[$offset] = $value;
5657
}
58+
59+
public function count()
60+
{
61+
return count($this->data);
62+
}
63+
64+
public function getIterator()
65+
{
66+
return new \ArrayIterator($this->data);
67+
}
5768
}

src/Utilities/Converter.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/Utilities/Normalizer.php

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

0 commit comments

Comments
 (0)