Skip to content

Commit

Permalink
Merge pull request #3 from rstgroup/scalar-typed-array
Browse files Browse the repository at this point in the history
Scalar and Class typed arrays
  • Loading branch information
snapshotpl authored Jul 19, 2018
2 parents 0757e5f + 3e96001 commit 9395342
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Builder/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ private function buildParameter(ReflectionParameter $parameter, $data, Reflectio
$node = $parser->parse(new TokenIterator((new Lexer())->tokenize($constructor->getDocComment())));
foreach ($node->getParamTagValues() as $node) {
if ($node->parameterName === '$' . $parameter->getName()) {
$type = $node->type->type;
$typeName = $node->type->type->name;
if ($this->isScalar($typeName)) {
continue;
}
$list = [];

$parser = (new BetterReflection())->phpParser();
Expand All @@ -115,8 +118,9 @@ private function buildParameter(ReflectionParameter $parameter, $data, Reflectio

foreach($data as $objectConstructorData) {
$list[] = $this->build(
$this->getFullClassName($type->name, $namespaces, $constructor->getDeclaringClass()),
$objectConstructorData);
$this->getFullClassName($typeName, $namespaces, $constructor->getDeclaringClass()),
$objectConstructorData
);
}

return $list;
Expand All @@ -127,6 +131,19 @@ private function buildParameter(ReflectionParameter $parameter, $data, Reflectio
return $data;
}

private function isScalar(string $value): bool
{
$scalars = [
'string',
'int',
'float',
'double',
'mixed',
];

return in_array($value, $scalars);
}

/**
* @param Stmt[] $nodes
*/
Expand Down
19 changes: 19 additions & 0 deletions test/ListOfObjectsWithScalarTypedArrayAndObjectListConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace RstGroup\ObjectBuilder\Test;

class ListOfObjectsWithScalarTypedArrayAndObjectListConstructor
{
public $list1;
public $list2;

/**
* @param string[] $list1
* @param SimpleScalarConstructor[] $list2
*/
public function __construct(array $list1, array $list2)
{
$this->list1 = $list1;
$this->list2 = $list2;
}
}
19 changes: 19 additions & 0 deletions test/ListOfObjectsWithScalarTypedArrayConstructor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace RstGroup\ObjectBuilder\Test;

class ListOfObjectsWithScalarTypedArrayConstructor
{
public $list1;
public $list2;

/**
* @param string[] $list1
* @param mixed[] $list2
*/
public function __construct(array $list1, array $list2)
{
$this->list1 = $list1;
$this->list2 = $list2;
}
}
57 changes: 57 additions & 0 deletions test/unit/Builder/ReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use RstGroup\ObjectBuilder\Builder\Reflection;
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithoutUseButWithFQNTypedArrayConstructor;
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithoutUseStmtConstructor;
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithScalarTypedArrayAndObjectListConstructor;
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithScalarTypedArrayConstructor;
use RstGroup\ObjectBuilder\Test\ListOfObjectsWithUseStmtConstructor;
use RstGroup\ObjectBuilder\Test\Object\SomeObject;
use RstGroup\ObjectBuilder\Test\Object\SomeSecondObject;
Expand Down Expand Up @@ -148,6 +150,61 @@ public function iCanBuildObjectWithObjectCollectionWithoutUseButWithFQNTypedArra
}
}

/** @test */
public function iCanBuildObjectWithScalarCollectionTypedArrayInConstructor()
{
$data = [
'list1' => ['str', 'str'],
'list2' => ['str', 'str'],
];
$class = ListOfObjectsWithScalarTypedArrayConstructor::class;

/** @var ListOfObjectsWithScalarTypedArrayConstructor $object */
$object = static::$builder->build($class, $data);

$this->assertInstanceOf(ListOfObjectsWithScalarTypedArrayConstructor::class, $object);
$this->assertCount(2, $object->list1);
$this->assertCount(2, $object->list2);
foreach($object->list1 as $element) {
$this->assertSame('str', $element);
}
foreach($object->list2 as $element) {
$this->assertSame('str', $element);
}
}

/** @test */
public function iCanBuildObjectWithBothScalarAndObjectCollectionTypedArrayInConstructor()
{
$data = [
'list1' => ['str', 'str'],
'list2' => [
[
'someString' => 'some string1',
'someInt' => 1,
],
[
'someString' => 'some string2',
'someInt' => 2,
],
],
];
$class = ListOfObjectsWithScalarTypedArrayAndObjectListConstructor::class;

/** @var ListOfObjectsWithScalarTypedArrayAndObjectListConstructor $object */
$object = static::$builder->build($class, $data);

$this->assertInstanceOf(ListOfObjectsWithScalarTypedArrayAndObjectListConstructor::class, $object);
$this->assertCount(2, $object->list1);
$this->assertCount(2, $object->list2);
foreach($object->list1 as $element) {
$this->assertSame('str', $element);
}
foreach($object->list2 as $element) {
$this->assertInstanceOf(SimpleScalarConstructor::class, $element);
}
}

/** @test */
public function iCanBuildAdvancedObjectHierarchy()
{
Expand Down

0 comments on commit 9395342

Please sign in to comment.