Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.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 ourterms of service andprivacy 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?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Friendly ping@mtarld |
* Nested array shapes | ||
* Union types for values |
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.
I don't think it worth mentioning these, as array shape basically supportsany type as values.
* Required and optional keys | ||
* Nested array shapes | ||
* Union types for values | ||
* Exact key ordering validation |
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.
This type does not perform any key ordering validation.
The ``ArrayShapeType`` method was introduced in Symfony 7.3. | ||
Array shape types support: |
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 byextraKeyType
andextraValueType
in theArrayShapeType
and is validated in theaccept
method.
// With optional keys (denoted by "?" suffix) | ||
$type = Type::arrayShape([ | ||
'required_id' => Type::int(), | ||
'optional_name?' => Type::string() | ||
]); |
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.
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],]);
// With optional keys (denoted by "?" suffix) | ||
$type = Type::arrayShape([ | ||
'required_id' => Type::int(), | ||
'optional_name?' => Type::string() | ||
]); |
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 examples for$sealed
,$extraKeyType
, and$extraValueType
of:
publicstaticfunction arrayShape(array$shape,bool$sealed =true, ?Type$extraKeyType =null, ?Type$extraValueType =null):ArrayShapeType
* Union types for values | ||
* Exact key ordering validation | ||
The ArrayShapeType support Associative Array definition:: |
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.
The ArrayShapeTypesupport Associative Array definition:: | |
The ArrayShapeTypesupports associative array definition: |
$type->is(typeof(['name' => 'Alice', 'age' => 30, ])); // true | ||
$type->is(typeof(['name' => 'Alice', 'age' => '30', ])); // false (wrong age type) |
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.
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 theStringTypeResolver
section.
thanks@mtarld for the feedback, I will adjust the PR with following your comment 🙌🏼 |
Uh oh!
There was an error while loading.Please reload this page.
Fixes#20763
Update the Symfony TypeInfo component documentation to reflect the new ArrayShapeType and StringTypeResolver capabilities .