Skip to content

Commit 18f1287

Browse files
committed
Refactor
1 parent ae4c905 commit 18f1287

11 files changed

Lines changed: 172 additions & 141 deletions

ext/uri/php_uri.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,26 +1225,22 @@ PHP_METHOD(Uri_Rfc3986_UriBuilder, build)
12251225
const zval *query = Z_RFC3986_URI_PROP_QUERY_P(ZEND_THIS);
12261226
const zval *fragment = Z_RFC3986_URI_PROP_FRAGMENT_P(ZEND_THIS);
12271227

1228-
zend_string *uri_str = php_uri_parser_rfc3986_recompose_from_zval(scheme, userinfo, host, port, path, query, fragment);
1229-
if (uri_str == NULL) {
1230-
RETURN_THROWS();
1231-
}
1232-
1233-
php_uri_parser_rfc3986_uris *base_uri = NULL;
1228+
php_uri_parser_rfc3986_uris *base_uris = NULL;
12341229
if (base_url != NULL) {
1235-
base_uri = Z_URI_OBJECT_P(base_url)->uri;
1230+
base_uris = Z_URI_OBJECT_P(base_url)->uri;
12361231
}
12371232

1238-
php_uri_parser_rfc3986_uris *uri = php_uri_parser_rfc3986_parse_ex(ZSTR_VAL(uri_str), ZSTR_LEN(uri_str), base_uri, false);
1239-
zend_string_release(uri_str);
1240-
if (uri == NULL) {
1233+
php_uri_parser_rfc3986_uris *uriparser_uris = php_uri_parser_rfc3986_build_from_zval(
1234+
base_uris, scheme, userinfo, host, port, path, query, fragment
1235+
);
1236+
if (uriparser_uris == NULL) {
12411237
RETURN_THROWS();
12421238
}
12431239

12441240
object_init_ex(return_value, php_uri_ce_rfc3986_uri);
12451241
php_uri_object *uri_object = Z_URI_OBJECT_P(return_value);
12461242
uri_object->parser = &php_uri_parser_rfc3986;
1247-
uri_object->uri = uri;
1243+
uri_object->uri = uriparser_uris;
12481244
}
12491245

12501246
PHPAPI php_uri_object *php_uri_object_create(zend_class_entry *class_type, const php_uri_parser *parser)

ext/uri/php_uri_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef enum php_uri_component_read_mode {
4646

4747
typedef zend_result (*php_uri_property_handler_read)(void *uri, php_uri_component_read_mode read_mode, zval *retval);
4848

49-
typedef zend_result (*php_uri_property_handler_write)(void *uri, zval *value, zval *errors);
49+
typedef zend_result (*php_uri_property_handler_write)(void *uri, const zval *value, zval *errors);
5050

5151
typedef enum php_uri_property_name {
5252
PHP_URI_PROPERTY_NAME_SCHEME,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test Uri\Rfc3986\UriBuilder basic - error - with base URL
3+
--FILE--
4+
<?php
5+
6+
$builder = new Uri\Rfc3986\UriBuilder()
7+
->setPath("/foo/bar/baz");
8+
9+
try {
10+
$builder->build(new Uri\Rfc3986\Uri("/foo/bar"));
11+
} catch (Throwable $e) {
12+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
13+
}
14+
15+
?>
16+
--EXPECT--
17+
Uri\InvalidUriException: The specified base URI must be absolute
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Test Uri\Rfc3986\UriBuilder basic - success - with base URL
3+
--FILE--
4+
<?php
5+
6+
$builder = new Uri\Rfc3986\UriBuilder()
7+
->setPath("/foo/bar/baz");
8+
$uri = $builder->build(new Uri\Rfc3986\Uri("https://example.com"));
9+
10+
var_dump($uri->toRawString());
11+
var_dump($uri);
12+
13+
?>
14+
--EXPECTF--
15+
string(31) "https://example.com/foo/bar/baz"
16+
object(Uri\Rfc3986\Uri)#%d (%d) {
17+
["scheme"]=>
18+
string(5) "https"
19+
["username"]=>
20+
NULL
21+
["password"]=>
22+
NULL
23+
["host"]=>
24+
string(11) "example.com"
25+
["port"]=>
26+
NULL
27+
["path"]=>
28+
string(12) "/foo/bar/baz"
29+
["query"]=>
30+
NULL
31+
["fragment"]=>
32+
NULL
33+
}

ext/uri/tests/rfc3986/builder/path_error_missing_leading_slash.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ try {
1515

1616
?>
1717
--EXPECT--
18-
Uri\InvalidUriException: A path must be either empty or begin with "/" when the URI contains a host
18+
Uri\InvalidUriException: The specified path is malformed

ext/uri/tests/rfc3986/builder/port_error_missing_host.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ try {
1414

1515
?>
1616
--EXPECT--
17-
Uri\InvalidUriException: The URI must contain a host if either the userinfo or the port component is present
17+
Uri\InvalidUriException: Cannot set a port without having a host

ext/uri/tests/rfc3986/builder/userinfo_error_missing_host.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ try {
1414

1515
?>
1616
--EXPECT--
17-
Uri\InvalidUriException: The URI must contain a host if either the userinfo or the port component is present
17+
Uri\InvalidUriException: Cannot set a userinfo without having a host

ext/uri/tests/rfc3986/modification/port_error_negative.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test Uri\Rfc3986\Uri component modification - host - too small number
2+
Test Uri\Rfc3986\Uri component modification - port - too small number
33
--EXTENSIONS--
44
uri
55
--FILE--

0 commit comments

Comments
 (0)