Skip to content

Commit

Permalink
Start v2
Browse files Browse the repository at this point in the history
Breaking changes:
* Drop __get() and __set(), so you can't access url components via magic
  properties anymore.
* Minimum required PHP version is now 8.0 (previously 7.2).

This now also allows to require the query-string package instead of
just suggesting it.
  • Loading branch information
otsch committed Sep 14, 2022
1 parent 62eec8c commit 5ac7071
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 103 deletions.
31 changes: 3 additions & 28 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
php-version: '8.0'

- name: Check PHP Version
run: php -v
Expand All @@ -25,37 +25,12 @@ jobs:
- name: Run PHP CS Fixer
run: composer cs

tests7x:
name: PHPUnit tests
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.2', '7.3', '7.4']

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Check PHP Version
run: php -v

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run tests
run: composer test

tests8x:
name: PHPUnit tests
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['8.0', '8.1']
php-versions: ['8.0', '8.1', '8.2']

steps:
- name: Checkout code
Expand Down Expand Up @@ -89,7 +64,7 @@ jobs:
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
php-version: '8.0'

- name: Check PHP Version
run: php -v
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,14 @@ $url = Url::parse('https://john:123@www.example.com:8080/foo?bar=baz');
```

The static `parse` method of the `Url` class provides a convenient way to
create a new instance and then access all of it's components separately.
create a new instance and then access all of its components separately.

```php
// Accessing URL components via method calls
$port = $url->port(); // => 8080
$domainSuffix = $url->domainSuffix(); // => "com"
$path = $url->path(); // => "/foo"
$fragment = $url->fragment(); // => NULL

// Or as properties
$scheme = $url->scheme; // => "https"
$user = $url->user; // => "john"
$host = $url->host; // => "www.example.com"
$domain = $url->domain; // => "example.com"
```

Of course you can also get a new instance using the `new` keyword.
Expand Down
12 changes: 5 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
"docs": "https://www.crwlr.software/packages/url"
},
"require": {
"php" : "^7.2|^8.0",
"php" : "^8.0",
"psr/http-message": "^1.0",
"symfony/polyfill-intl-idn": "^1.11"
"symfony/polyfill-intl-idn": "^1.11",
"crwlr/query-string": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0",
"phpunit/phpunit": "^9.0",
"friendsofphp/php-cs-fixer": "^3.4",
"phpstan/phpstan": "^1.3"
"phpstan/phpstan": "^1.8"
},
"autoload": {
"psr-4": {
Expand All @@ -54,8 +55,5 @@
"update-schemes": "@php bin/update-schemes",
"update-default-ports": "@php bin/update-default-ports",
"add-git-hooks": "@php bin/add-git-hooks"
},
"suggest": {
"crwlr/query-string": "The queryString() method requires this additional package"
}
}
36 changes: 4 additions & 32 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class Url
private $path;

/**
* @phpstan-ignore-next-line
* @var string|Query|null
*/
private $query;
Expand Down Expand Up @@ -112,33 +111,6 @@ public function __construct($url)
$this->populate($url);
}

/**
* @param string $name
* @return mixed
*/
public function __get(string $name)
{
if ($this->isValidComponentName($name)) {
return $this->$name();
}

return null;
}

/**
* @param string $name
* @param mixed $value
* @return mixed
*/
public function __set(string $name, $value)
{
if ($this->isValidComponentName($name)) {
return $this->$name($value);
}

return null;
}

/**
* Returns a new `Url` instance with param $url.
*
Expand Down Expand Up @@ -493,7 +465,7 @@ public function path(?string $path = null)
public function query(?string $query = null)
{
if ($query === null) {
return $this->query instanceof Query ? $this->query->toString() : $this->query; // @phpstan-ignore-line
return $this->query instanceof Query ? $this->query->toString() : $this->query;
} elseif ($query === '') {
$this->query = null;
} else {
Expand All @@ -513,8 +485,8 @@ public function query(?string $query = null)
public function queryArray(?array $query = null)
{
if ($query === null) {
if ($this->query instanceof Query) { // @phpstan-ignore-line
return $this->query->toArray(); // @phpstan-ignore-line
if ($this->query instanceof Query) {
return $this->query->toArray();
}

return $this->query ? Helpers::queryStringToArray($this->query) : [];
Expand All @@ -528,7 +500,7 @@ public function queryArray(?array $query = null)
/**
* @throws Exception
*/
public function queryString(): Query // @phpstan-ignore-line
public function queryString(): Query
{
if (version_compare(PHP_VERSION, '8.0.0', '<')) {
throw new Exception(
Expand Down
2 changes: 1 addition & 1 deletion tests/QueryStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testTheQueryStringCanBeAccessedViaMagicGetter(): void
$url = Url::parse('https://www.example.com/path?foo=bar');

if (version_compare(PHP_VERSION, '8.0.0', '>=') && class_exists(Query::class)) {
$url->queryString->set('baz', 'quz'); // @phpstan-ignore-line
$url->queryString()->set('baz', 'quz');

$this->assertEquals(['foo' => 'bar', 'baz' => 'quz'], $url->queryArray());

Expand Down
28 changes: 0 additions & 28 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,6 @@ public function testParseUrl(): void
$this->assertEquals('/some/path?some=query#fragment', $url->relative());
}

public function testClassPropertyAccess(): void
{
$url = $this->createDefaultUrlObject();
$this->assertEquals('https', $url->scheme); // @phpstan-ignore-line
$this->assertEquals('user:password@sub.sub.example.com:8080', $url->authority); // @phpstan-ignore-line
$this->assertEquals('user', $url->user); // @phpstan-ignore-line
$this->assertEquals('password', $url->password); // @phpstan-ignore-line
$this->assertEquals('password', $url->pass); // @phpstan-ignore-line
$this->assertEquals('user:password', $url->userInfo); // @phpstan-ignore-line
$this->assertEquals('sub.sub.example.com', $url->host); // @phpstan-ignore-line
$this->assertEquals('example.com', $url->domain); // @phpstan-ignore-line
$this->assertEquals('example', $url->domainLabel); // @phpstan-ignore-line
$this->assertEquals('com', $url->domainSuffix); // @phpstan-ignore-line
$this->assertEquals('sub.sub', $url->subdomain); // @phpstan-ignore-line
$this->assertEquals(8080, $url->port); // @phpstan-ignore-line
$this->assertEquals('/some/path', $url->path); // @phpstan-ignore-line
$this->assertEquals('some=query', $url->query); // @phpstan-ignore-line
$this->assertEquals(['some' => 'query'], $url->queryArray); // @phpstan-ignore-line
$this->assertEquals('fragment', $url->fragment); // @phpstan-ignore-line
$this->assertEquals('https://user:password@sub.sub.example.com:8080', $url->root); // @phpstan-ignore-line
$this->assertEquals('/some/path?some=query#fragment', $url->relative); // @phpstan-ignore-line

// other class properties that aren't components of the parsed URL should not be available.
$this->assertNull($url->parser); // @phpstan-ignore-line
$this->assertNull($url->validator); // @phpstan-ignore-line
$this->assertNull($url->resolver); // @phpstan-ignore-line
}

public function testParseIdnUrl(): void
{
$url = new Url('https://www.юбилейный.онлайн');
Expand Down

0 comments on commit 5ac7071

Please sign in to comment.