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

[OptionsResolver] handle nested options#9819

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

Closed
Doctrs wants to merge1 commit intosymfony:masterfromDoctrs:patch-2
Closed
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletionscomponents/options_resolver.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -634,6 +634,93 @@ let you find out which options are defined::
}
}

OptionResolverNested
~~~~~~~~~~~~~~~~~~

OptionResolver supports nesting. In order to allow checking multilevel arrays, you need to pass a class instance `OptionResolverNested` as a nested array::

// ...
class Mailer
{
// ...
public function configureOptions(OptionsResolver $resolver)
{
// ...
$resolver->setDefaults(array(
'encryption' => null,
'recipient' => new OptionResolverNested([
'name' => 'John',
'mail' => 'john@example.com'
]),
'host' => 'example.org',
));
}
}

.. note::

OptionResolverNested is inherited by \ArrayObject. In case an array is passed as a nested element, then the check will go only to the key, and to the fact that the element is an array.
Data inside the array will not be validated.

To get access to the elements inside OptionResolverNested, you need to get the data as well as in multilevel arrays::

// ...
class Mailer
{
// ...
public function configureOptions(OptionsResolver $resolver)
{
// ...
$resolver->setDefaults(array(
'encryption' => function (Options $options) {
$options['recipient']['name']; // John
$options['recipient']['mail']; // john@example.com
$options['host']; // example.org

return true;
},
'recipient' => new OptionResolverNested([
'name' => 'John',
'mail' => 'john@example.com'
]),
'host' => 'example.org',
));
}
}

To access the parents from nested OptionResolverNested, it is necessary to change the closure arguments to `function (ResolveData $ data)`. In this case, in the form of $data there will be a completely assembled array of configurations starting from the root::

// ...
class Mailer
{
// ...
public function configureOptions(OptionsResolver $resolver)
{
// ...
$resolver->setDefaults(array(
'encryption' => null,
'default_mail' => 'default@example.com',
'recipient' => new OptionResolverNested([
'name' => 'John',
'mail' => function(ResolveData $data){
$data['encryption']; // null
$data['default_mail']; // default@example.com
$data['recipient']['name']; // John
$data['recipient']['mail']; // \Closure

return 'john@example.com';
},
]),
'host' => 'example.org',
));
}
}

.. caution::

The step with the transfer to the closure of `ResolveData $ data` is the most recent. Before it there are normalizers, the processing of lazy downloads and etc.
In the event that this closure refers to itself, the exception will not be discarded. The closure will not be processed and will return as it was declared.

Performance Tweaks
~~~~~~~~~~~~~~~~~~

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp