Debian Codegraph @

vendor/symfony/type-info/Type/BuiltinType.php

raw
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\Type;
15 use Symfony\Component\TypeInfo\TypeIdentifier;
16
17 /**
18 * @author Mathias Arlaud <[email protected]>
19 * @author Baptiste Leduc <[email protected]>
20 *
21 * @template T of TypeIdentifier
22 */
23 final class BuiltinType extends Type
24 {
25 /**
26 * @param T $typeIdentifier
27 */
28 public function __construct(
29 private readonly TypeIdentifier $typeIdentifier,
30 ) {
31 }
32
33 /**
34 * @return T
35 */
36 public function getTypeIdentifier(): TypeIdentifier
37 {
38 return $this->typeIdentifier;
39 }
40
41 public function isIdentifiedBy(TypeIdentifier|string ...$identifiers): bool
42 {
43 foreach ($identifiers as $identifier) {
44 if ($identifier === $this->typeIdentifier || $identifier === $this->typeIdentifier->value) {
45 return true;
46 }
47 }
48
49 return false;
50 }
51
52 public function isNullable(): bool
53 {
54 return \in_array($this->typeIdentifier, [TypeIdentifier::NULL, TypeIdentifier::MIXED], true);
55 }
56
57 public function accepts(mixed $value): bool
58 {
59 return match ($this->typeIdentifier) {
60 TypeIdentifier::ARRAY => \is_array($value),
61 TypeIdentifier::BOOL => \is_bool($value),
62 TypeIdentifier::CALLABLE => \is_callable($value),
63 TypeIdentifier::FALSE => false === $value,
64 TypeIdentifier::FLOAT => \is_float($value),
65 TypeIdentifier::INT => \is_int($value),
66 TypeIdentifier::ITERABLE => is_iterable($value),
67 TypeIdentifier::MIXED => true,
68 TypeIdentifier::NULL => null === $value,
69 TypeIdentifier::OBJECT => \is_object($value),
70 TypeIdentifier::RESOURCE => \is_resource($value),
71 TypeIdentifier::STRING => \is_string($value),
72 TypeIdentifier::TRUE => true === $value,
73 default => false,
74 };
75 }
76
77 public function __toString(): string
78 {
79 return $this->typeIdentifier->value;
80 }
81 }