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