Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/Exceptions/ConverterException.php

This file was deleted.

6 changes: 6 additions & 0 deletions src/Exceptions/NormalizerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace PHPJava\Exceptions;

class NormalizerException extends \Exception
{
}
4 changes: 2 additions & 2 deletions src/Kernel/Mnemonics/_invokevirtual.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPJava\Utilities\AttributionResolver;
use PHPJava\Utilities\BinaryTool;
use PHPJava\Utilities\ClassResolver;
use PHPJava\Utilities\Converter;
use PHPJava\Utilities\Normalizer;
use PHPJava\Utilities\Formatter;
use PHPJava\Utilities\TypeResolver;

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

$arguments = Converter::normalizeArguments(
$arguments = Normalizer::normalizeValues(
$arguments,
$signature['arguments']
);
Expand Down
13 changes: 12 additions & 1 deletion src/Kernel/Types/_Array/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
use PHPJava\Utilities\Extractor;
use PHPJava\Utilities\TypeResolver;

class Collection implements \ArrayAccess
class Collection implements \ArrayAccess, \Countable, \IteratorAggregate
{
private $data;
private $position = 0;

public function __construct(array &$data, string $type = null)
{
Expand Down Expand Up @@ -54,4 +55,14 @@ public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}

public function count()
{
return count($this->data);
}

public function getIterator()
{
return new \ArrayIterator($this->data);
}
}
48 changes: 0 additions & 48 deletions src/Utilities/Converter.php

This file was deleted.

57 changes: 57 additions & 0 deletions src/Utilities/Normalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace PHPJava\Utilities;

use PHPJava\Exceptions\NormalizerException;
use PHPJava\Kernel\Types\_Array\Collection;
use PHPJava\Kernel\Types\Type;

class Normalizer
{

/**
* @param array|Collection $values
* @param array $normalizeTypes
* @return array|Collection
* @throws NormalizerException
*/
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 Type|Collection $value
*/
if ($value instanceof Collection) {
$value = static::normalizeValues(
$value,
array_fill(
0,
count($value),
$realType
)
);
continue;
}
if ($realType['type'] === 'class') {
// TODO: implements up-cast and down-cast
continue;
}
$initiateClass = TypeResolver::TYPES_MAP[$realType['type']];
if ($value instanceof $initiateClass) {
continue;
}
$value = new $initiateClass(
Extractor::realValue($value)
);
}

return $values;
}
}