@@ -74,6 +74,8 @@ that need a simple way to describe a class or anything with a type::
74
74
// Then resolve types for any subject
75
75
$typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type instance
76
76
$typeResolver->resolve('bool'); // returns a "bool" Type instance
77
+ $typeResolver->resolve('array{id: int, name?: string}'); // returns an "ArrayShapeType" Type instance with 'id' is required, 'name' is optional
78
+
77
79
78
80
// Types can be instantiated thanks to static factories
79
81
$type = Type::list(Type::nullable(Type::bool()));
@@ -92,6 +94,54 @@ Each of these calls will return you a ``Type`` instance that corresponds to the
92
94
static method used. You can also resolve types from a string (as shown in the
93
95
``bool `` parameter of the previous example)
94
96
97
+
98
+ The TypeInfo component provides a new Type "ArrayShapeType" to define exact array structures with specific key-value type relationships.
99
+
100
+ .. versionadded :: 7.3
101
+
102
+ The ``ArrayShapeType `` method was introduced in Symfony 7.3.
103
+
104
+ Array shape types support:
105
+
106
+ * Required and optional keys
107
+ * Nested array shapes
108
+ * Union types for values
109
+ * Exact key ordering validation
110
+
111
+ The ArrayShapeType support Associative Array definition::
112
+
113
+ use Symfony\Component\TypeInfo\Type;
114
+
115
+ // Simple array shape
116
+ $type = Type::arrayShape([
117
+ 'name' => Type::string(),
118
+ 'age' => Type::int()
119
+ ]);
120
+
121
+ // With optional keys (denoted by "?" suffix)
122
+ $type = Type::arrayShape([
123
+ 'required_id' => Type::int(),
124
+ 'optional_name?' => Type::string()
125
+ ]);
126
+
127
+ But also, ``StringTypeResolver `` now supports parsing array shape notation::
128
+
129
+ use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;
130
+
131
+ $resolver = new TypeResolver();
132
+
133
+ // Parse array shape definition
134
+ $type = $resolver->resolve('array{name: string, age: int}');
135
+
136
+ // Equivalent to:
137
+ Type::arrayShape([
138
+ 'name' => Type::string(),
139
+ 'age' => Type::int()
140
+ ]);
141
+
142
+ $type->is(typeof(['name' => 'Alice', 'age' => 30, ])); // true
143
+ $type->is(typeof(['name' => 'Alice', 'age' => '30', ])); // false (wrong age type)
144
+
95
145
PHPDoc Parsing
96
146
~~~~~~~~~~~~~~
97
147
0 commit comments