Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit85aa35b

Browse files
authored
Add ArrayShapeType examples to type_info.rst
1 parentb9f8400 commit85aa35b

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

‎components/type_info.rst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ that need a simple way to describe a class or anything with a type::
7474
// Then resolve types for any subject
7575
$typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type instance
7676
$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+
7779

7880
// Types can be instantiated thanks to static factories
7981
$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
9294
static method used. You can also resolve types from a string (as shown in the
9395
``bool`` parameter of the previous example)
9496

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;
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+
95145
PHPDoc Parsing
96146
~~~~~~~~~~~~~~
97147

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp