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

withScheme weird results #16

Closed
xobotyi opened this issue Aug 12, 2020 · 3 comments
Closed

withScheme weird results #16

xobotyi opened this issue Aug 12, 2020 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@xobotyi
Copy link

xobotyi commented Aug 12, 2020

Bug Report

(Fill in the relevant information below to help triage your issue.)

Information Description
Version 6.2.1
PHP version 7.2
OS Platform Centos 6

Summary

withScheme('') should set make network-path reference, but it does invalid URI instead.

IMO when scheme set to empty string it shoud remove the scheme specification making the URI Network-Path Reference. And null to remove the scheme segment at all.

Standalone code, or other way to reproduce the problem

Uri::createFromString('domain.com')->withScheme('');

Expected result

"//domain.com"

Actual result

":domain.com"

@xobotyi xobotyi changed the title withScheme weird results withScheme weird results Aug 12, 2020
@xobotyi xobotyi changed the title withScheme weird results withScheme weird results Aug 12, 2020
nyamsprod referenced this issue in thephpleague/uri Aug 12, 2020
@nyamsprod
Copy link
Member

@xobotyi you indeed did caught a bug (a regression from URI v5 😢 ) but it's not what you think 😄

TL;DR:

Uri::createFromString('domain.com')->withScheme('');

Should throw as an URI scheme can not be the empty string. Only PSR-7 allow the empty string but there's a specific class for that in the repo (ie the Http class).

Nevertheless the behaviour is expected because when you do

$uri = Uri::createFromString('domain.com');
$uri->getHost(); // return null;
$uri->getPath(); //return 'domain.com'

you endup with an URI which only has a path set and not it's host. To set up the host you should do the following instead

$uri = Uri::createFromString('//domain.com');
$uri->getHost(); //return 'domain.com';
$uri->getPath(); //return ''

nyamsprod referenced this issue in thephpleague/uri Aug 12, 2020
@xobotyi
Copy link
Author

xobotyi commented Aug 12, 2020

Huh.
Anyways, as I understand there is no way to cast https://domain.com to //domain.com without concatenation?

@nyamsprod
Copy link
Member

yes there is

<?php

$uri = Uri::createFromString('https://domain.com')->withScheme(null);

// or

$uri = Http::createFromString('https://domain.com')->withScheme('');

Depending on the class you are using

nyamsprod referenced this issue in thephpleague/uri Aug 12, 2020
@nyamsprod nyamsprod self-assigned this Aug 12, 2020
@nyamsprod nyamsprod added the bug Something isn't working label Aug 12, 2020
nyamsprod referenced this issue in thephpleague/uri Aug 12, 2020
@nyamsprod nyamsprod transferred this issue from thephpleague/uri Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants