-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUnionType.php
110 lines (91 loc) · 3.65 KB
/
UnionType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;
/**
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
* @author Baptiste Leduc <baptiste.leduc@gmail.com>
*
* @template T of Type
*
* @implements CompositeTypeInterface<T>
*/
class UnionType extends Type implements CompositeTypeInterface
{
/**
* @var list<T>
*/
private readonly array $types;
/**
* @param list<T> $types
*/
public function __construct(Type ...$types)
{
if (\count($types) < 2) {
throw new InvalidArgumentException(\sprintf('"%s" expects at least 2 types.', self::class));
}
foreach ($types as $type) {
if ($type instanceof self) {
throw new InvalidArgumentException(\sprintf('Cannot set "%s" as a "%1$s" part.', self::class));
}
if ($type instanceof BuiltinType) {
if (TypeIdentifier::NULL === $type->getTypeIdentifier() && !is_a(static::class, NullableType::class, allow_string: true)) {
throw new InvalidArgumentException(\sprintf('Cannot create union with "null", please use "%s" instead.', NullableType::class));
}
if ($type->getTypeIdentifier()->isStandalone()) {
throw new InvalidArgumentException(\sprintf('Cannot create union with "%s" standalone type.', $type));
}
}
}
usort($types, fn (Type $a, Type $b): int => (string) $a <=> (string) $b);
$this->types = array_values(array_unique($types));
$builtinTypesIdentifiers = array_map(
fn (BuiltinType $t): TypeIdentifier => $t->getTypeIdentifier(),
array_filter($this->types, fn (Type $t): bool => $t instanceof BuiltinType),
);
if ((\in_array(TypeIdentifier::TRUE, $builtinTypesIdentifiers, true) || \in_array(TypeIdentifier::FALSE, $builtinTypesIdentifiers, true)) && \in_array(TypeIdentifier::BOOL, $builtinTypesIdentifiers, true)) {
throw new InvalidArgumentException('Cannot create union with redundant boolean type.');
}
if (\in_array(TypeIdentifier::TRUE, $builtinTypesIdentifiers, true) && \in_array(TypeIdentifier::FALSE, $builtinTypesIdentifiers, true)) {
throw new InvalidArgumentException('Cannot create union with both "true" and "false", "bool" should be used instead.');
}
if (\in_array(TypeIdentifier::OBJECT, $builtinTypesIdentifiers, true) && \count(array_filter($this->types, fn (Type $t): bool => $t instanceof ObjectType))) {
throw new InvalidArgumentException('Cannot create union with both "object" and class type.');
}
}
/**
* @return list<T>
*/
public function getTypes(): array
{
return $this->types;
}
public function composedTypesAreSatisfiedBy(callable $specification): bool
{
foreach ($this->types as $type) {
if ($type->isSatisfiedBy($specification)) {
return true;
}
}
return false;
}
public function __toString(): string
{
$string = '';
$glue = '';
foreach ($this->types as $t) {
$string .= $glue.($t instanceof CompositeTypeInterface ? '('.$t.')' : $t);
$glue = '|';
}
return $string;
}
}