<?php
namespace Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\TypeIdentifier;
final class GenericType extends Type implements WrappingTypeInterface
{
private readonly array $variableTypes;
public function __construct(
private readonly BuiltinType|ObjectType $type,
Type ...$variableTypes,
) {
if ($type instanceof BuiltinType && TypeIdentifier::ARRAY !== $type->getTypeIdentifier() && TypeIdentifier::ITERABLE !== $type->getTypeIdentifier()) {
throw new InvalidArgumentException(\sprintf('Cannot create "%s" with "%s" type.', self::class, $type));
}
$this->variableTypes = $variableTypes;
}
public function getWrappedType(): Type
{
return $this->type;
}
public function getVariableTypes(): array
{
return $this->variableTypes;
}
public function wrappedTypeIsSatisfiedBy(callable $specification): bool
{
return $this->getWrappedType()->isSatisfiedBy($specification);
}
public function __toString(): string
{
$typeString = (string) $this->type;
$variableTypesString = '';
$glue = '';
foreach ($this->variableTypes as $t) {
$variableTypesString .= $glue.$t;
$glue = ', ';
}
return $typeString.'<'.$variableTypesString.'>';
}
}