Skip to content

Commit f848597

Browse files
committed
Add ArrayShapeType examples to type_info.rst
1 parent b9f8400 commit f848597

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

components/type_info.rst

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This component gives you a :class:`Symfony\\Component\\TypeInfo\\Type` object th
2727
represents the PHP type of anything you built or asked to resolve.
2828

2929
There are two ways to use this component. First one is to create a type manually thanks
30-
to the :class:`Symfony\\Component\\TypeInfo\\Type` static methods as following::
30+
to the :class:`Symfony\\Component\\TypeInfo\\Type` static methods as follows::
3131

3232
use Symfony\Component\TypeInfo\Type;
3333

@@ -37,7 +37,7 @@ to the :class:`Symfony\\Component\\TypeInfo\\Type` static methods as following::
3737
Type::list(Type::bool());
3838
Type::intersection(Type::object(\Stringable::class), Type::object(\Iterator::class));
3939

40-
Many others methods are available and can be found
40+
Many other methods are available and can be found
4141
in :class:`Symfony\\Component\\TypeInfo\\TypeFactoryTrait`.
4242

4343
You can also use a generic method that detects the type automatically::
@@ -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 array shape type instance where 'id' is required and 'name' is optional
78+
7779

7880
// Types can be instantiated thanks to static factories
7981
$type = Type::list(Type::nullable(Type::bool()));
@@ -90,7 +92,55 @@ that need a simple way to describe a class or anything with a type::
9092

9193
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
93-
``bool`` parameter of the previous example)
95+
``bool`` parameter of the previous example).
96+
97+
98+
The TypeInfo component provides a new array shape type to define exact array structures with specific key-value type relationships.
99+
100+
.. versionadded:: 7.3
101+
102+
The array shape type was introduced in Symfony 7.3.
103+
104+
Array shape types support:
105+
106+
* Required and optional keys
107+
* Nested array shapes
108+
* Sealed and unsealed shapes (allowing or rejecting extra entries)
109+
110+
Array shapes can be sealed or unsealed:
111+
112+
- ``array{0: int}`` is sealed: it does not accept extra entries.
113+
- ``array{0: int, ...}`` is unsealed: it accepts extra entries.
114+
- You can also define the type of extra keys and values: ``array{0: int, ...<string, bool>}``.
115+
116+
This information is stored on the ``ArrayShapeType`` via its ``extraKeyType`` and ``extraValueType`` and validated by the ``accepts()`` method.
117+
118+
Array shapes support associative array definitions::
119+
120+
use Symfony\Component\TypeInfo\Type;
121+
122+
// Simple array shape
123+
$type = Type::arrayShape([
124+
'name' => Type::string(),
125+
'age' => Type::int()
126+
]);
127+
128+
// With optional keys (denoted by "?" suffix)
129+
$type = Type::arrayShape([
130+
'required_id' => Type::int(),
131+
'optional_name' => ['type' => Type::string(), 'optional' => true],
132+
]);
133+
134+
// Unsealed: allow extra entries not defined above (sealed = false)
135+
$type = Type::arrayShape([
136+
'id' => Type::int(),
137+
], false);
138+
139+
// Unsealed with typed extra keys and values (extraKeyType=string, extraValueType=bool)
140+
// Equivalent to: array{id: int, ...<string, bool>}
141+
$type = Type::arrayShape([
142+
'id' => Type::int(),
143+
], false, Type::string(), Type::bool());
94144

95145
PHPDoc Parsing
96146
~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)