Skip to content

Commit e7c3a43

Browse files
committed
Rename
1 parent 46c58f2 commit e7c3a43

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
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/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/Normalizer.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,57 @@
11
<?php
22
namespace PHPJava\Utilities;
33

4-
use PHPJava\Exceptions\ConverterException;
4+
use PHPJava\Exceptions\NormalizerException;
55
use PHPJava\Kernel\Types\_Array\Collection;
66
use PHPJava\Kernel\Types\Type;
77

88
class Normalizer
99
{
1010

1111
/**
12-
* @param array $arguments
13-
* @param array $acceptedArguments
12+
* @param array|Collection $values
13+
* @param array $normalizeTypes
1414
* @return array
15-
* @throws ConverterException
15+
* @throws NormalizerException
1616
*/
17-
public static function normalizeArguments(array $arguments, array $acceptedArguments): array
17+
public static function normalizeValues($values, array $normalizeTypes)
1818
{
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.');
2121
}
2222

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+
}
2428
/**
25-
* @var Type|Collection $argument
29+
* @var Type|Collection $value
2630
*/
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+
);
2940
continue;
3041
}
31-
$realType = $acceptedArguments[$key] ?? null;
32-
if ($realType === null) {
33-
throw new ConverterException('Broken arguments parser.');
34-
}
3542
if ($realType['type'] === 'class') {
3643
// TODO: implements up-cast and down-cast
3744
continue;
3845
}
3946
$initiateClass = TypeResolver::TYPES_MAP[$realType['type']];
40-
if ($argument instanceof $initiateClass) {
47+
if ($value instanceof $initiateClass) {
4148
continue;
4249
}
43-
$argument = new $initiateClass(Extractor::realValue($argument));
50+
$value = new $initiateClass(
51+
Extractor::realValue($value)
52+
);
4453
}
4554

46-
return $arguments;
55+
return $values;
4756
}
4857
}

0 commit comments

Comments
 (0)