Skip to content
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

Update options_resolver.rst #6629

Closed
wants to merge 1 commit into from
Closed

Conversation

atailouloute
Copy link
Contributor

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

$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/

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

xabbuh commented Jun 5, 2016

👍

xabbuh added a commit that referenced this pull request Jun 6, 2016
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
@xabbuh
Copy link
Member

xabbuh commented Jun 6, 2016

Thank you @atailouloute. I have merged your change into the 2.7branch from where I will merge it up to all other branches. That's why your PR is shown as closed instead of merged. Though you will still get all the credits for your contribution.

@xabbuh xabbuh closed this Jun 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants