Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
indianmodassir committed Nov 19, 2024
1 parent 4ab978e commit e3af335
Show file tree
Hide file tree
Showing 12 changed files with 684 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/tests export-ignore
/vendor-bin export-ignore

/CHANGELOG.md export-ignore
/README.md export-ignore
/UPGRADING.md export-ignore
/CODE_OF_CONDUCT.md export-ignore
Expand Down
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Shahzada Modassir <codingmodassir@gmail.com>
Shahzada Modassir <shahzadamodassir@gmail.com>
Shahzadi Afsara <[email protected]>
66 changes: 64 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,64 @@
# url
URL resolution and parsing meant to have feature parity with PHP core.
# PHP URL
> URL resolution and parsing meant to have feature parity with PHP core.
## Composer Installation

Installation is super-easy via [Composer](https://getcomposer.org)

```bash
composer require web/url
```

or add it by hand to your `composer.json` file.

## Usage

```php
Url::parse('https://user:[email protected]:5500/path/example.html?id=123&user=test#section');
// Results:
// Web\Url\Parser\Parser Object
// (
// [hash] => #section
// [search] => ?id=123&user=test
// [query] => id=123&user=test
// [slashes] => //
// [auth] => user:pass
// [protocol] => https:
// [host] => www.example.com:5500
// [href] => https://user:[email protected]:5500/path/example.html?id=123&user=test#section
// [pathname] => /path/example.html
// [port] => 5500
// [uri] => /path/example.html?id=123&user=test
// [hostname] => example.com
// [origin] => https://user:[email protected]:5500
// [username] => user
// [password] => pass
// [www] => www.
// )

new Url('path/example.html?id=123&user=test#section', 'https://user:[email protected]:5500/');
// Results:
Web\Url\Url Object
// (
// [hash] => #section
// [password] => pass
// [username] => user
// [search] => ?id=123&user=test
// [query] => id=123&user=test
// [origin] => https://user:[email protected]:5500
// [slashes] => //
// [searchParams] =>
// [auth] => user:pass
// [protocol] => https:
// [www] => www.
// [host] => www.example.com:5500
// [href] => https://user:[email protected]:5500/path/example.html?id=123&user=test#section
// [pathname] => /path/example.html
// [port] => 5500
// [uri] => /path/example.html?id=123&user=test
// [hostname] => example.com
// )
```

## Resources
- [Report issue](https://github.com/lazervel/url/issues) and [send Pull Request](https://github.com/lazervel/url/pulls) in the [main Lazervel repository](https://github.com/lazervel/url)
21 changes: 16 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
"description": "URL resolution and parsing meant to have feature parity with PHP core",
"type": "library",
"license": "MIT",
"keywords": ["url", "parser"],
"homepage": "https://github.com/lazervel",
"keywords": ["url", "php", "parser"],
"autoload": {
"psr-4": {
"Url\\": "src/"
"Web\\Url\\": "src/"
}
},
"authors": [
{
"name": "Shahzada Modassir",
"email": "codingmodassir@gmail.com",
"email": "shahzadamodassir@gmail.com",
"homepage": "https://github.com/shahzadamodassir"
},
{
Expand All @@ -25,8 +26,18 @@
{
"type": "Github",
"url": "https://github.com/shahzadamodassir"
},
{
"type": "Patreon",
"url": "https://patreon.com/shahzadamodassir"
},
{
"type": "Opencollective",
"url": "https://opencollective.com/shahzadamodassir"
}
],
"minimum-stability": "dev",
"require": {}
"minimum-stability": "stable",
"require": {
"web/url-search-params": "^1.0"
}
}
230 changes: 230 additions & 0 deletions src/BaseURL.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php

declare(strict_types=1);

/**
* URL resolution and parsing meant to have feature parity with PHP core.
* PHP Version 7.0.0
*
* Github (url) Repository
* @see https://github.com/javercel/url
*
* @author Shahzada Modassir <[email protected]>
* @author Shahzadi Afsara <[email protected]>
*
* @copyright 2020 - 2024 Shahzada Modassir
* @copyright 2024 - 2025 Shahzadi Afsara
* @copyright 2024 - All rights reserved!
*
* @license MIT License
* @see https://github.com/lazervel/url/blob/main/LICENSE
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Web\Url;

use Web\Url\Parser\Parser;

/**
* @internal
*/
abstract class BaseURL
{
/**
* PHP Regular Expression
*
* ? (Capture: <href>) (e.g., 'https://example.com:8080/path?id=123#section1')
* ? (Capture: <origin>) (e.g., 'https://example.com')
* ? (Capture: <protocol>) (e.g., 'http:', 'https:', 'ftp:')
* ? (Capture: <slashes>) (e.g., '//' will be preserved)
* ? (Capture: <auth>) (e.g., 'user:password' in 'user:[email protected]')
* ? (Capture: <username>) (e.g., 'user' in '[email protected]')
* ? (Capture: <password>) (e.g., 'password' in 'user:[email protected]')
* ? (Capture: <host>) (e.g., 'example.com' or 'example.com:8080')
* ? (Capture: <www>) (e.g., 'www' for 'www.example.com')
* ? (Capture: <hostname>) (e.g., 'example.com')
* ? (Capture: <port>) (e.g., '8080' in 'http://example.com:8080')
* ? (Capture: <uri>) (e.g., '/path?id=123')
* ? (Capture: <pathname>) (e.g., '/path/to/resource')
* ? (Capture: <search>) (e.g., '?id=123&name=test')
* ? (Capture: <query>) (e.g., 'id=123&name=test')
* ? (Capture: <hash>) (e.g., '#section1' in 'https://example.com/#section1')
*
* @var string
*/
private const URL = '/^(?P<href>(?:(?P<origin>((?P<protocol>[\w-]+:(?=\/\/)*)(?P<slashes>\/\/)?|)((?P<auth>(?P<username>[\w-]+)(?:\:(?P<password>[\w-]+))?)@)*(?<host>(?P<www>www\.)*((?P<hostname>[^\/:?#]+)*(:(?P<port>\d+))*)))(?P<uri>(?P<pathname>[^?#]+)*(?P<search>\?(?P<query>[^#]+))*)(?P<hash>#[\w-]*(\?[=\&\w-]+)?)*))$/';

/**
* The hash fragment (anchor) of a URL, used for identifying a section within the resource
* (e.g., '#section1').
*
* @var string
*/
public $hash;

/**
* The password component of a URL
* (e.g., 'password' in 'user:[email protected]').
*
* @var string
*/
public $password;

/**
* The username component of a URL
* (e.g., 'user' in '[email protected]').
*
* @var string
*/
public $username;

/**
* The search/query string portion of the URL, typically after the '?'
* (e.g., '?id=123&name=test').
*
* @var string
*/
public $search;

/**
* The query string of a URL, usually in the form of key-value pairs after the '?'
* (e.g., 'id=123').
*
* @var string
*/
public $query;

/**
* The origin of the URL, which includes the protocol and hostname
* (e.g., 'https://example.com').
*
* @var string
*/
public $origin;

/**
* Indicates whether slashes should be included in the URL
* (e.g., 'true' means slashes will be preserved).
*
* @var bool
*/
public $slashes;

/**
* A PHP implementation URLSearchParams for handling query parameters easily.
*
* @var \Web\URLSearchParams\URLSearchParams
*/
public $searchParams;

/**
* The username, password both or single component of a URL
* (e.g., 'user:password' or 'user' in 'user:[email protected]').
*
* @var string
*/
public $auth;

/**
* The protocol part of the URL
* (e.g., 'http:', 'https:', 'ftp:').
*
* @var string
*/
public $protocol;

/**
* Indicates if the URL includes 'www' prefix
* (e.g., 'true' for 'www.example.com').
*
* @var bool
*/
public $www;

/**
* The host portion of the URL, which includes both hostname and optional port
* (e.g., 'example.com' or 'example.com:8080').
*
* @var string
*/
public $host;

/**
* The full URL, including protocol, hostname, port, path, search, and hash
* (e.g., 'https://example.com:8080/path?id=123#section1').
*
* @var string
*/
public $href;

/**
* The pathname part of the URL, which comes after the domain and port
* (e.g., '/path/to/resource').
*
* @var string
*/
public $pathname;

/**
* The port number used in the URL, if specified
* (e.g., '8080' in 'http://example.com:8080').
*
* @var int
*/
public $port;

/**
* The path part of the URL, which comes after the domain and port with
* search query key-value pairs after the '?'
* (e.g., '/path/to/resource?name=test&id=1234').
*
* @var string
*/

/**
* The URI of the URL, which is typically the path and query string
* (e.g., '/path?id=123').
*
* @var string
*/
public $uri;

/**
* The hostname of the URL, which is the domain name without the protocol or port
* (e.g., 'example.com').
*
* @var string
*/
public $hostname;

/**
* Creates a new URL constructor.
* Initializes a new instance of BaseURL => Url with the given $input and $base.
*
* @param string $input [required]
* @param string|\Web\Url\Url $base [optional]
*
* @throws \Web\Url\Exception\InvalidUrlException
*
* @return void
*/
public function __construct(string $input, $base)
{
Parser::with($input, $base, false, false, $this, self::URL)->parse();
}

/**
* The Url::parse() method takes a URL string, parses it, and returns a URL object.
*
* @param string $url [required]
* @param bool $parseQuery [optional]
* @param bool slashesDenoteHost [optional]
*
* @return \Web\Url\ParserInterface Returns a URL object
*/
public static function parse(string $url, bool $parseQuery = false, bool $slashesDenoteHost = false)
{
return Parser::with($url, null, $parseQuery, $slashesDenoteHost, null, self::URL)->parse();
}
}
?>
11 changes: 11 additions & 0 deletions src/Exception/InvalidUrlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Web\Url\Exception;

final class InvalidUrlException extends \InvalidArgumentException implements \Throwable
{
//
}
?>
11 changes: 11 additions & 0 deletions src/Exception/URIError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Web\Url\Exception;

final class URIError extends \TypeError implements \Throwable
{
//
}
?>
Loading

0 comments on commit e3af335

Please sign in to comment.