Skip to content

Commit

Permalink
Merge pull request #11 from nadar/relative-urls
Browse files Browse the repository at this point in the history
add option for relative url check
  • Loading branch information
nadar authored Dec 20, 2020
2 parents c1ed88c + cab16d4 commit 3efd2b5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md).

## 1.3.0

+ [#10](https://github.com/nadar/crawler/issues/10) Add relative url check to `Url` class.

## 1.2.1 (17. December 2020)

+ [#9](https://github.com/nadar/crawler/pull/9) Fix issue where `CRAWL_IGNORE` tag had no effect. Trim the array value for found linkes, which is equals to the link title.
Expand Down
24 changes: 24 additions & 0 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
*
* In order to retrieve the url from an object use `getNormalized()`. This value is mainly used to identife and store.
*
* An example where the url is checked for relative path, merge otherwise and validate:
*
* ```php
* $url = new Url('/about/me');
* if ($url->isRelative()) {
* $url->merge(new Url('https://luya.io'));
* }
*
* if ($url->isValid()) {
* echo $url->getNormalized(); // outputs https://luya.io/about/me
* }
* ```
*
* @author Basil Suter <[email protected]>
* @since 1.0.0
*/
Expand Down Expand Up @@ -150,6 +163,17 @@ public function isValid()
{
return !in_array($this->getScheme(), ['mailto', 'tel', 'ftp']);
}

/**
* Whether the original url is a relative url or not
*
* @return boolean
* @since 1.3.0
*/
public function isRelative()
{
return strncmp($this->url, '//', 2) && strpos($this->url, '://') === false;
}

/**
* If the current URL is missing informations, it cain obtain informations from the to merge url
Expand Down
9 changes: 9 additions & 0 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ public function testInvalidUrl()
$this->assertTrue((new Url('https://luya.io'))->isValid());
$this->assertTrue((new Url('/admin/path'))->isValid());
}

public function testIsRelative()
{
$this->assertFalse((new Url('https://luya.io'))->isRelative());
$this->assertFalse((new Url('https://luya.io/path//double'))->isRelative());
$this->assertFalse((new Url('//path-without-host'))->isRelative());
$this->assertTrue((new Url('/path-without-host'))->isRelative());
$this->assertTrue((new Url('path-without-host/base-path-info-required'))->isRelative());
}
}

0 comments on commit 3efd2b5

Please sign in to comment.