10000 [PropertyInfo] Allow nested collections by jderusse · Pull Request #28012 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyInfo] Allow nested collections #28012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2018
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 10000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Allow multidimensional collection in property info
  • Loading branch information
jderusse committed Jul 25, 2018
commit ce49036790e91b6be626cf8a6243ccb75e87125d
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public function typesProvider()
array('bal', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime')), null, null),
array('parent', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy')), null, null),
array('collection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime'))), null, null),
array('nestedCollection', array(new T 10000 ype(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING, false)))), null, null),
array('mixedCollection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, null, null)), null, null),
array('a', array(new Type(Type::BUILTIN_TYPE_INT)), 'A.', null),
array('b', array(new Type(Type::BUILTIN_TYPE_OBJECT, true, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy')), 'B.', null),
array('c', array(new Type(Type::BUILTIN_TYPE_BOOL, true)), null, null),
Expand Down Expand Up @@ -126,6 +128,8 @@ public function typesWithCustomPrefixesProvider()
array('bal', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime')), null, null),
array('parent', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy')), null, null),
array('collection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime'))), null, null),
array('nestedCollection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING, false)))), null, null),
array('mixedCollection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, null, null)), null, null),
array('a', null, 'A.', null),
array('b', null, 'B.', null),
array('c', array(new Type(Type::BUILTIN_TYPE_BOOL, true)), null, null),
Expand Down Expand Up @@ -164,6 +168,8 @@ public function typesWithNoPrefixesProvider()
array('bal', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime')), null, null),
array('parent', array(new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy')), null, null),
array('collection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_OBJECT, false, 'DateTime'))), null, null),
array('nestedCollection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING, false)))), null, null),
array('mixedCollection', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, null, null)), null, null),
array('a', null, 'A.', null),
array('b', null, 'B.', null),
array('c', null, null, null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function testGetProperties()
'bal',
'parent',
'collection',
'nestedCollection',
'mixedCollection',
'B',
'Guid',
'g',
Expand Down Expand Up @@ -77,6 +79,8 @@ public function testGetPropertiesWithCustomPrefixes()
'bal',
'parent',
'collection',
'nestedCollection',
'mixedCollection',
'B',
'Guid',
'g',
Expand Down Expand Up @@ -108,6 +112,8 @@ public function testGetPropertiesWithNoPrefixes()
'bal',
'parent',
'collection',
'nestedCollection',
'mixedCollection',
'B',
'Guid',
'g',
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/PropertyInfo/Tests/Fixtures/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ class Dummy extends ParentDummy
* @Groups({"a", "b"})
*/
public $collection;
/**
* @var string[][]
*/
public $nestedCollection;
/**
* @var mixed[]
*/
public $mixedCollection;

/**
* @var ParentDummy
Expand Down
24 changes: 11 additions & 13 deletions src/Symfony/Component/PropertyInfo/Util/PhpDocTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,28 @@ private function createType($docType, $nullable)
{
// Cannot guess
if (!$docType || 'mixed' === $docType) {
return;
return null;
}

if ($collection = '[]' === substr($docType, -2)) {
$docType = substr($docType, 0, -2);
}

$docType = $this->normalizeType($docType);
list($phpType, $class) = $this->getPhpTypeAndClass($docType);

$array = 'array' === $docType;

if ($collection || $array) {
if ($array || 'mixed' === $docType) {
if ('[]' === substr($docType, -2)) {
if ('mixed[]' === $docType) {
$collectionKeyType = null;
$collectionValueType = null;
} else {
$collectionKeyType = new Type(Type::BUILTIN_TYPE_INT);
$collectionValueType = new Type($phpType, $nullable, $class);
$collectionValueType = $this->createType(substr($docType, 0, -2), $nullable);
}

return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, $collectionKeyType, $collectionValueType);
}

$docType = $this->normalizeType($docType);
list($phpType, $class) = $this->getPhpTypeAndClass($docType);

if ('array' === $docType) {
return new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, null, null);
}

return new Type($phpType, $nullable, $class);
}

Expand Down
0