|
14 | 14 | use Symfony\Component\TypeInfo\Type;
|
15 | 15 | use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
|
16 | 16 | use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
|
| 17 | +use Symfony\Component\TypeInfo\TypeIdentifier; |
17 | 18 |
|
18 | 19 | /**
|
19 | 20 | * @author Hugo Alliaume <hugo@alliau.me>
|
|
22 | 23 | */
|
23 | 24 | final class TypeHelper
|
24 | 25 | {
|
| 26 | + /** |
| 27 | + * TODO: To remove when supporting symfony/type-info >=7.3 only. |
| 28 | + */ |
| 29 | + public static function accepts(Type $type, mixed $value): bool |
| 30 | + { |
| 31 | + $parentAccepts = function (Type $_type0, mixed $_value0): bool { |
| 32 | + $specification = static function (Type $_type1) use (&$specification, $_value0): bool { |
| 33 | + if ($_type1 instanceof WrappingTypeInterface) { |
| 34 | + return $_type1->wrappedTypeIsSatisfiedBy($specification); |
| 35 | + } |
| 36 | + |
| 37 | + if ($_type1 instanceof CompositeTypeInterface) { |
| 38 | + return $_type1->composedTypesAreSatisfiedBy($specification); |
| 39 | + } |
| 40 | + |
| 41 | + return TypeHelper::accepts($_type1, $_value0); |
| 42 | + }; |
| 43 | + |
| 44 | + return $_type0->isSatisfiedBy($specification); |
| 45 | + }; |
| 46 | + |
| 47 | + if ($type instanceof Type\ArrayShapeType) { |
| 48 | + if (!\is_array($value)) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + foreach ($type->getShape() as $key => $shapeValue) { |
| 53 | + if (!($shapeValue['optional'] ?? false) && !\array_key_exists($key, $value)) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + foreach ($value as $key => $itemValue) { |
| 59 | + $valueType = $type->getShape()[$key]['type'] ?? false; |
| 60 | + |
| 61 | + if ($valueType && !TypeHelper::accepts($valueType, $itemValue)) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + if (!$valueType && ($type->isSealed() || !TypeHelper::accepts($type->getExtraKeyType(), $key) || !TypeHelper::accepts($type->getExtraValueType(), $itemValue))) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + // Also supports EnumType and BackedEnumType |
| 74 | + if ($type instanceof Type\ObjectType) { |
| 75 | + $className = $type->getClassName(); |
| 76 | + return $value instanceof $className; |
| 77 | + } |
| 78 | + |
| 79 | + if ($type instanceof Type\BuiltinType) { |
| 80 | + return match ($type->getTypeIdentifier()) { |
| 81 | + TypeIdentifier::ARRAY => \is_array($value), |
| 82 | + TypeIdentifier::BOOL => \is_bool($value), |
| 83 | + TypeIdentifier::CALLABLE => \is_callable($value), |
| 84 | + TypeIdentifier::FALSE => false === $value, |
| 85 | + TypeIdentifier::FLOAT => \is_float($value), |
| 86 | + TypeIdentifier::INT => \is_int($value), |
| 87 | + TypeIdentifier::ITERABLE => is_iterable($value), |
| 88 | + TypeIdentifier::MIXED => true, |
| 89 | + TypeIdentifier::NULL => null === $value, |
| 90 | + TypeIdentifier::OBJECT => \is_object($value), |
| 91 | + TypeIdentifier::RESOURCE => \is_resource($value), |
| 92 | + TypeIdentifier::STRING => \is_string($value), |
| 93 | + TypeIdentifier::TRUE => true === $value, |
| 94 | + default => false, |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + if ($type instanceof Type\CollectionType) { |
| 99 | + if (!$parentAccepts($type, $value)) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + if ($type->isList() && (!\is_array($value) || !array_is_list($value))) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + $keyType = $type->getCollectionKeyType(); |
| 108 | + $valueType = $type->getCollectionValueType(); |
| 109 | + |
| 110 | + if (is_iterable($value)) { |
| 111 | + foreach ($value as $k => $v) { |
| 112 | + // key or value do not match |
| 113 | + if (!TypeHelper::accepts($keyType, $k) || !TypeHelper::accepts($valueType, $v)) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + if ($type instanceof Type\NullableType) { |
| 123 | + return null === $value || $parentAccepts($type, $value); |
| 124 | + } |
| 125 | + |
| 126 | + if ($type instanceof Type\GenericType || $type instanceof Type\TemplateType || $type instanceof Type\UnionType || $type instanceof Type\IntersectionType) { |
| 127 | + return $parentAccepts($type, $value); |
| 128 | + } |
| 129 | + |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
25 | 133 | /**
|
26 | 134 | * TODO: To remove when supporting symfony/type-info >=7.3 only.
|
27 | 135 | *
|
|
0 commit comments