Skip to content

Commit

Permalink
minor #6629 Update options_resolver.rst (atailouloute)
Browse files Browse the repository at this point in the history
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #6629).

Discussion
----------

Update options_resolver.rst

The old code will not work as expected if the `host` option starts with `https`, the `substr` function will return `https:/` and not `https://`.

```php
$resolver = new Symfony\Component\OptionsResolver\OptionsResolver;

$resolver->setDefined(['host', 'encryption']);
$resolver->setNormalizer('host', function (Options $options, $value) {

     if (!in_array(substr($value, 0, 7), array('http://', 'https://'))) {
        if ('ssl' === $options['encryption']) {
            $value = 'https://'.$value;
        } else {
            $value = 'http://'.$value;
        }
    }

    return $value;
});;

$options = $resolver->resolve(array(
    'host'       => 'https://symfony.com/',
    'encryption' => 'ssl'
));

echo $options['host'];
// Expected value : https://symfony.com/
// Result         : https://https://symfony.com/
```

Commits
-------

a474785 Update options_resolver.rst
  • Loading branch information
xabbuh committed Jun 6, 2016
2 parents 78da6d5 + a474785 commit e665c92
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion components/options_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ if you need to use other options during normalization::
{
// ...
$resolver->setNormalizer('host', function (Options $options, $value) {
if (!in_array(substr($value, 0, 7), array('http://', 'https://'))) {
if ('http://' !== substr($value, 0, 7) && 'https://' !== substr($value, 0, 8)) {
if ('ssl' === $options['encryption']) {
$value = 'https://'.$value;
} else {
Expand Down

0 comments on commit e665c92

Please sign in to comment.