| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\TypeInfo\Type; |
| 13 |
|
| 14 |
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException; |
| 15 |
use Symfony\Component\TypeInfo\Type; |
| 16 |
use Symfony\Component\TypeInfo\TypeIdentifier; |
| 17 |
|
| 18 |
/** |
| 19 |
* Represents a generic type, which is a type that holds variable parts. |
| 20 |
* |
| 21 |
* @author Mathias Arlaud <[email protected]> |
| 22 |
* @author Baptiste Leduc <[email protected]> |
| 23 |
* |
| 24 |
* @template T of BuiltinType<TypeIdentifier::ARRAY>|BuiltinType<TypeIdentifier::ITERABLE>|ObjectType |
| 25 |
* |
| 26 |
* @implements WrappingTypeInterface<T> |
| 27 |
*/ |
| 28 |
final class GenericType extends Type implements WrappingTypeInterface |
| 29 |
{ |
| 30 |
/** |
| 31 |
* @var list<Type> |
| 32 |
*/ |
| 33 |
private readonly array $variableTypes; |
| 34 |
|
| 35 |
/** |
| 36 |
* @param T $type |
| 37 |
*/ |
| 38 |
public function __construct( |
| 39 |
private readonly BuiltinType|ObjectType $type, |
| 40 |
Type ...$variableTypes, |
| 41 |
) { |
| 42 |
if ($type instanceof BuiltinType && TypeIdentifier::ARRAY !== $type->getTypeIdentifier() && TypeIdentifier::ITERABLE !== $type->getTypeIdentifier()) { |
| 43 |
throw new InvalidArgumentException(\sprintf('Cannot create "%s" with "%s" type.', self::class, $type)); |
| 44 |
} |
| 45 |
|
| 46 |
$this->variableTypes = $variableTypes; |
| 47 |
} |
| 48 |
|
| 49 |
public function getWrappedType(): Type |
| 50 |
{ |
| 51 |
return $this->type; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @return list<Type> |
| 56 |
*/ |
| 57 |
public function getVariableTypes(): array |
| 58 |
{ |
| 59 |
return $this->variableTypes; |
| 60 |
} |
| 61 |
|
| 62 |
public function wrappedTypeIsSatisfiedBy(callable $specification): bool |
| 63 |
{ |
| 64 |
return $this->getWrappedType()->isSatisfiedBy($specification); |
| 65 |
} |
| 66 |
|
| 67 |
public function __toString(): string |
| 68 |
{ |
| 69 |
$typeString = (string) $this->type; |
| 70 |
|
| 71 |
$variableTypesString = ''; |
| 72 |
$glue = ''; |
| 73 |
foreach ($this->variableTypes as $t) { |
| 74 |
$variableTypesString .= $glue.$t; |
| 75 |
$glue = ', '; |
| 76 |
} |
| 77 |
|
| 78 |
return $typeString.'<'.$variableTypesString.'>'; |
| 79 |
} |
| 80 |
} |