8000 Support multiple builtinType (union types) · symfony/symfony@d7655cd · GitHub
[go: up one dir, main page]

Skip to content

Commit d7655cd

Browse files
committed
Support multiple builtinType (union types)
1 parent 57e39b4 commit d7655cd

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/Symfony/Component/PropertyInfo/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* Added ability to have union types in `Type` class.
8+
49
5.2.0
510
-----
611

src/Symfony/Component/PropertyInfo/Tests/TypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\PropertyInfo\Type;
16+
use function PHPUnit\Framework\assertSame;
1617

1718
/**
1819
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -49,4 +50,15 @@ public function testInvalidType()
4950
$this->expectExceptionMessage('"foo" is not a valid PHP type.');
5051
new Type('foo');
5152
}
53+
54+
public function testUnionTypes()
55+
{
56+
$type = new Type(['string', 'int']);
57+
$this->assertSame(['string', 'int'], $type->getBuiltinType());
58+
59+
$type = new Type('array', false, null, true, new Type('string'), new Type(['object', 'string']));
60+
$this->assertSame('array', $type->getBuiltinType());
61+
$this->assertSame('string', $type->getCollectionKeyType()->getBuiltinType());
62+
$this->assertSame(['object', 'string'], $type->getCollectionValueType()->getBuiltinType());
63+
}
5264
}

src/Symfony/Component/PropertyInfo/Type.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,20 @@ class Type
5757
private $collectionValueType;
5858

5959
/**
60+
* @param string|array $builtinType
61+
*
6062
* @throws \InvalidArgumentException
6163
*/
62-
public function __construct(string $builtinType, bool $nullable = false, string $class = null, bool $collection = false, self $collectionKeyType = null, self $collectionValueType = null)
64+
public function __construct($builtinType, bool $nullable = false, string $class = null, bool $collection = false, self $collectionKeyType = null, self $collectionValueType = null)
6365
{
64-
if (!\in_array($builtinType, self::$builtinTypes)) {
65-
throw new \InvalidArgumentException(sprintf('"%s" is not a valid PHP type.', $builtinType));
66+
if (!\is_string($builtinType) && !\is_array($builtinType)) {
67+
throw new \TypeError(sprintf('__construct(): Argument #1 ($builtinType) must be of type string|array, "%s" given.', get_debug_type($builtinType)));
68+
}
69+
70+
foreach ((array) $builtinType as $type) {
71+
if (!\in_array($type, self::$builtinTypes)) {
72+
throw new \InvalidArgumentException(sprintf('"%s" is not a valid PHP type.', $type));
73+
}
6674
}
6775

6876
$this->builtinType = $builtinType;
@@ -77,8 +85,10 @@ public function __construct(string $builtinType, bool $nullable = false, string
7785
* Gets built-in type.
7886
*
7987
* Can be bool, int, float, string, array, object, resource, null, callback or iterable.
88+
*
89+
* @return string|array
8090
*/
81-
public function getBuiltinType(): string
91+
public function getBuiltinType()
8292
{
8393
return $this->builtinType;
8494
}

0 commit comments

Comments
 (0)
0