vendor/symfony/property-info/Type.php line 79

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\PropertyInfo;
  11. /**
  12.  * Type value object (immutable).
  13.  *
  14.  * @author Kévin Dunglas <dunglas@gmail.com>
  15.  *
  16.  * @final
  17.  */
  18. class Type
  19. {
  20.     public const BUILTIN_TYPE_INT 'int';
  21.     public const BUILTIN_TYPE_FLOAT 'float';
  22.     public const BUILTIN_TYPE_STRING 'string';
  23.     public const BUILTIN_TYPE_BOOL 'bool';
  24.     public const BUILTIN_TYPE_RESOURCE 'resource';
  25.     public const BUILTIN_TYPE_OBJECT 'object';
  26.     public const BUILTIN_TYPE_ARRAY 'array';
  27.     public const BUILTIN_TYPE_NULL 'null';
  28.     public const BUILTIN_TYPE_FALSE 'false';
  29.     public const BUILTIN_TYPE_TRUE 'true';
  30.     public const BUILTIN_TYPE_CALLABLE 'callable';
  31.     public const BUILTIN_TYPE_ITERABLE 'iterable';
  32.     /**
  33.      * List of PHP builtin types.
  34.      *
  35.      * @var string[]
  36.      */
  37.     public static $builtinTypes = [
  38.         self::BUILTIN_TYPE_INT,
  39.         self::BUILTIN_TYPE_FLOAT,
  40.         self::BUILTIN_TYPE_STRING,
  41.         self::BUILTIN_TYPE_BOOL,
  42.         self::BUILTIN_TYPE_RESOURCE,
  43.         self::BUILTIN_TYPE_OBJECT,
  44.         self::BUILTIN_TYPE_ARRAY,
  45.         self::BUILTIN_TYPE_CALLABLE,
  46.         self::BUILTIN_TYPE_FALSE,
  47.         self::BUILTIN_TYPE_TRUE,
  48.         self::BUILTIN_TYPE_NULL,
  49.         self::BUILTIN_TYPE_ITERABLE,
  50.     ];
  51.     /**
  52.      * List of PHP builtin collection types.
  53.      *
  54.      * @var string[]
  55.      */
  56.     public static $builtinCollectionTypes = [
  57.         self::BUILTIN_TYPE_ARRAY,
  58.         self::BUILTIN_TYPE_ITERABLE,
  59.     ];
  60.     private $builtinType;
  61.     private $nullable;
  62.     private $class;
  63.     private $collection;
  64.     private $collectionKeyType;
  65.     private $collectionValueType;
  66.     /**
  67.      * @param Type[]|Type|null $collectionKeyType
  68.      * @param Type[]|Type|null $collectionValueType
  69.      *
  70.      * @throws \InvalidArgumentException
  71.      */
  72.     public function __construct(string $builtinTypebool $nullable falsestring $class nullbool $collection false, array|Type $collectionKeyType null, array|Type $collectionValueType null)
  73.     {
  74.         if (!\in_array($builtinTypeself::$builtinTypes)) {
  75.             throw new \InvalidArgumentException(sprintf('"%s" is not a valid PHP type.'$builtinType));
  76.         }
  77.         $this->builtinType $builtinType;
  78.         $this->nullable $nullable;
  79.         $this->class $class;
  80.         $this->collection $collection;
  81.         $this->collectionKeyType $this->validateCollectionArgument($collectionKeyType5'$collectionKeyType') ?? [];
  82.         $this->collectionValueType $this->validateCollectionArgument($collectionValueType6'$collectionValueType') ?? [];
  83.     }
  84.     private function validateCollectionArgument(array|Type|null $collectionArgumentint $argumentIndexstring $argumentName): ?array
  85.     {
  86.         if (null === $collectionArgument) {
  87.             return null;
  88.         }
  89.         if (\is_array($collectionArgument)) {
  90.             foreach ($collectionArgument as $type) {
  91.                 if (!$type instanceof self) {
  92.                     throw new \TypeError(sprintf('"%s()": Argument #%d (%s) must be of type "%s[]", "%s" or "null", array value "%s" given.'__METHOD__$argumentIndex$argumentNameself::class, self::class, get_debug_type($collectionArgument)));
  93.                 }
  94.             }
  95.             return $collectionArgument;
  96.         }
  97.         return [$collectionArgument];
  98.     }
  99.     /**
  100.      * Gets built-in type.
  101.      *
  102.      * Can be bool, int, float, string, array, object, resource, null, callback or iterable.
  103.      */
  104.     public function getBuiltinType(): string
  105.     {
  106.         return $this->builtinType;
  107.     }
  108.     public function isNullable(): bool
  109.     {
  110.         return $this->nullable;
  111.     }
  112.     /**
  113.      * Gets the class name.
  114.      *
  115.      * Only applicable if the built-in type is object.
  116.      */
  117.     public function getClassName(): ?string
  118.     {
  119.         return $this->class;
  120.     }
  121.     public function isCollection(): bool
  122.     {
  123.         return $this->collection;
  124.     }
  125.     /**
  126.      * Gets collection key types.
  127.      *
  128.      * Only applicable for a collection type.
  129.      *
  130.      * @return Type[]
  131.      */
  132.     public function getCollectionKeyTypes(): array
  133.     {
  134.         return $this->collectionKeyType;
  135.     }
  136.     /**
  137.      * Gets collection value types.
  138.      *
  139.      * Only applicable for a collection type.
  140.      *
  141.      * @return Type[]
  142.      */
  143.     public function getCollectionValueTypes(): array
  144.     {
  145.         return $this->collectionValueType;
  146.     }
  147. }