Commit d2766057 authored by Ján Forgáč's avatar Ján Forgáč

OrderConverter - complex (with arrays)

parent 315c5d24
......@@ -29,17 +29,22 @@ class OrderConverter
}
private static function parseValue($nestedMethods, $object, $result = [])
private static function parseValue($nestedMethods, $object)
{
$methodValue = array_shift($nestedMethods);
$method = 'get' . ucfirst($methodValue);
$object = $object->$method();
$value = $object->$method();
if (!is_object($object)) {
$result[$methodValue] = $object;
if (is_object($value)) {
$result[$methodValue] = self::parseValue($nestedMethods, $value);
return $result;
} elseif (is_array($value)) {
while ($subValue = array_shift($value)) {
$result[][$methodValue] = self::parseValue($nestedMethods, $subValue);
}
return $result;
} else {
$result[$methodValue] = self::parseValue($nestedMethods, $object, $result);
$result[$methodValue] = $value;
return $result;
}
}
......
......@@ -44,4 +44,88 @@ class OrderConverterTest extends TestCase
], OrderConverter::orderToArray($root, $nestedMethods));
}
public function testParseValue3()
{
$nestedMethods = ['second.third.fourth', 'third.fourth'];
$root = new TestObjects\Root3();
$this->assertSame([
0 => [
'second' => [
'third' => [
'fourth' => true,
],
],
],
1 => [
'second' => [
'third' => [
'fourth' => true,
],
],
],
'third' => [
'fourth' => true,
],
], OrderConverter::orderToArray($root, $nestedMethods));
}
public function testParseValue4()
{
$nestedMethods = ['first3.second3.third.fourth', 'second.third.fourth', 'third.fourth'];
$root = new TestObjects\Root3();
$this->assertSame([
0 => [
'first3' => [
'second3' => [
0 => [
'third' => [
'fourth' => true,
],
],
1 => [
'third' => [
'fourth' => true,
],
],
],
],
],
1 => [
'first3' => [
'second3' => [
0 => [
'third' => [
'fourth' => true,
],
],
1 => [
'third' => [
'fourth' => true,
],
],
],
],
],
2 => [
'first3' => [
'second3' => [
0 => [
'third' => [
'fourth' => true,
],
],
1 => [
'third' => [
'fourth' => true,
],
],
],
],
],
'third' => [
'fourth' => true,
],
], OrderConverter::orderToArray($root, $nestedMethods));
}
}
......@@ -10,6 +10,7 @@ namespace Tests\Unit\TestObjects;
class Second
{
/** @var Third */
private $third;
public function __construct()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment