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

Skip to content

Commit ec3a8e5

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

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-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/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('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