Skip to content

Commit

Permalink
Merge pull request #4 from nadar/encode-url
Browse files Browse the repository at this point in the history
add encoding option and test
  • Loading branch information
nadar authored Oct 21, 2020
2 parents 02637ef + bcfc037 commit 5b52010
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 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.1.0 (21. October 2020)

+ [#4](https://github.com/nadar/crawler/pull/4) Add option to encode the url paths.

## 1.0.0 (25. September 2020)

- First stable release.
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class Url
*/
protected $parsed;

/**
* @var boolean Whether values should be encoded when retrieving values or not. By default this is disabled.
* @since 1.1.0
*/
public $encode = false;

/**
* Constructor
*
Expand Down Expand Up @@ -79,7 +85,7 @@ public function getHost()
*/
public function getPath()
{
return isset($this->parsed['path']) ? $this->parsed['path'] : false;
return isset($this->parsed['path']) ? $this->processEncoding($this->parsed['path']) : false;
}

/**
Expand All @@ -99,7 +105,7 @@ public function getPathExtension()
*/
public function getPathFileName()
{
return basename($this->getPath());
return $this->processEncoding(basename($this->getPath()));
}

/**
Expand Down Expand Up @@ -167,4 +173,16 @@ public function merge(Url $url)

return $this;
}

/**
* Process a value which will be encoded when enabled. If not the value will be the same from input.
*
* @param string $value
* @return string If encoding is enabled the value will be encoded otherwise return original.
* @since 1.1
*/
private function processEncoding($value)
{
return $this->encode ? urlencode($value) : $value;
}
}
18 changes: 18 additions & 0 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,22 @@ public function testMerge()
$this->assertSame('nadar.io', $b->merge($a)->getHost()); // nothing will happen as host exists
$this->assertSame('https', $b->merge($a)->getScheme()); // nothing will happen as scheme exists
}

public function testEncode()
{
// https://www.ahv-iv.ch/de/Merkblätter-Formulare/Formulare/Elektronische-Formulare/AHV-Formulare/318260-Anmeldung-für-einen-Versicherungsausweis
// https://www.ahv-iv.ch/it/Opuscoli-Moduli/Moduli/Prestazioni-dellIPG-servizio-e-maternità

$u = new Url('https://www.ahv-iv.ch/de/Merkblätter-Formulare/Formulare/Elektronische-Formulare/AHV-Formulare/318260-Anmeldung-für-einen-Versicherungsausweis');
$u->encode = true;

$this->assertSame('%2Fde%2FMerkbl%C3%A4tter-Formulare%2FFormulare%2FElektronische-Formulare%2FAHV-Formulare%2F318260-Anmeldung-f%C3%BCr-einen-Versicherungsausweis', $u->getPath());

$u = new Url('https://luya.io/äà');
$this->assertSame('https://luya.io/äà', $u->getNormalized());

$u = new Url('https://luya.io/äà');
$u->encode = true;
$this->assertSame('https://luya.io/%2F%C3%A4%C3%A0', $u->getNormalized());
}
}

0 comments on commit 5b52010

Please sign in to comment.