-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[TypeInfo] Add ArrayShapeType examples to type_info.rst #20998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.3
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -74,6 +74,8 @@ that need a simple way to describe a class or anything with a type:: | |||||
// Then resolve types for any subject | ||||||
$typeResolver->resolve(new \ReflectionProperty(Dummy::class, 'id')); // returns an "int" Type instance | ||||||
$typeResolver->resolve('bool'); // returns a "bool" Type instance | ||||||
$typeResolver->resolve('array{id: int, name?: string}'); // returns an "ArrayShapeType" Type instance with 'id' is required, 'name' is optional | ||||||
|
||||||
|
||||||
// Types can be instantiated thanks to static factories | ||||||
$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 | |||||
static method used. You can also resolve types from a string (as shown in the | ||||||
``bool`` parameter of the previous example) | ||||||
|
||||||
|
||||||
The TypeInfo component provides a new Type "ArrayShapeType" to define exact array structures with specific key-value type relationships. | ||||||
|
||||||
.. versionadded:: 7.3 | ||||||
|
||||||
The ``ArrayShapeType`` method was introduced in Symfony 7.3. | ||||||
|
||||||
Array shape types support: | ||||||
|
||||||
* Required and optional keys | ||||||
* Nested array shapes | ||||||
* Union types for values | ||||||
Comment on lines
+107
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it worth mentioning these, as array shape basically supports any type as values. |
||||||
* Exact key ordering validation | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type does not perform any key ordering validation. |
||||||
|
||||||
The ArrayShapeType support Associative Array definition:: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
use Symfony\Component\TypeInfo\Type; | ||||||
|
||||||
// Simple array shape | ||||||
$type = Type::arrayShape([ | ||||||
'name' => Type::string(), | ||||||
'age' => Type::int() | ||||||
]); | ||||||
|
||||||
// With optional keys (denoted by "?" suffix) | ||||||
$type = Type::arrayShape([ | ||||||
'required_id' => Type::int(), | ||||||
'optional_name?' => Type::string() | ||||||
]); | ||||||
Comment on lines
+121
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not work like that. If you want to create optional keys, you must do: $type = Type::arrayShape([
'required_id' => Type::int(),
'optional_name' => ['type' => Type::string(), 'optional' => true],
]);
Comment on lines
+121
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also add examples for public static function arrayShape(array $shape, bool $sealed = true, ?Type $extraKeyType = null, ?Type $extraValueType = null): ArrayShapeType |
||||||
|
||||||
But also, ``StringTypeResolver`` now supports parsing array shape notation:: | ||||||
|
||||||
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver; | ||||||
|
||||||
$resolver = new TypeResolver(); | ||||||
|
||||||
// Parse array shape definition | ||||||
$type = $resolver->resolve('array{name: string, age: int}'); | ||||||
|
||||||
// Equivalent to: | ||||||
Type::arrayShape([ | ||||||
'name' => Type::string(), | ||||||
'age' => Type::int() | ||||||
]); | ||||||
|
||||||
$type->is(typeof(['name' => 'Alice', 'age' => 30, ])); // true | ||||||
$type->is(typeof(['name' => 'Alice', 'age' => '30', ])); // false (wrong age type) | ||||||
Comment on lines
+142
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code does not work as well. The valid one is: $type->accepts(['name' => 'Alice', 'age' => 30]); // true
$type->accepts(['name' => 'Alice', 'age' => '30']); // false (invalid age type) Plus, I don't think it should be under the |
||||||
|
||||||
PHPDoc Parsing | ||||||
~~~~~~~~~~~~~~ | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also add a word about the "sealed" concept.
Indeed,
array{0: int}
is sealed whereasarray{0: int, ...}
is not. This means that the second type accepts extra values and the first does not. It is also worth mentioning that you can define the type of extra keys/values:array{0: int, ...<string, bool>}
.This data is held by
extraKeyType
andextraValueType
in theArrayShapeType
and is validated in theaccept
method.