-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from TheDragonCode/2.x
Added ignoring certain URL keys
- Loading branch information
Showing
10 changed files
with
254 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "dragon-code/last-modified" project. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Andrey Helldar <helldar@ai-rus.com> | ||
* | ||
* @copyright 2021 Andrey Helldar | ||
* | ||
* @license MIT | ||
* | ||
* @see https://github.com/TheDragonCode/last-modified | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DragonCode\LastModified\Facades; | ||
|
||
use DragonCode\LastModified\Support\Url as Support; | ||
use DragonCode\Support\Helpers\Http\Builder; | ||
use Illuminate\Support\Facades\Facade; | ||
use Psr\Http\Message\UriInterface; | ||
|
||
/** | ||
* @method static Builder parse(string $url) | ||
* @method static string hash(UriInterface|string $url) | ||
*/ | ||
class Url extends Facade | ||
{ | ||
protected static function getFacadeAccessor(): string | ||
{ | ||
return Support::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "dragon-code/last-modified" project. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Andrey Helldar <helldar@ai-rus.com> | ||
* | ||
* @copyright 2021 Andrey Helldar | ||
* | ||
* @license MIT | ||
* | ||
* @see https://github.com/TheDragonCode/last-modified | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DragonCode\LastModified\Support; | ||
|
||
use DragonCode\LastModified\Facades\Config as ConfigSupport; | ||
use DragonCode\Support\Facades\Helpers\Ables\Arrayable; | ||
use DragonCode\Support\Facades\Helpers\Str; | ||
use DragonCode\Support\Facades\Http\Builder; | ||
use DragonCode\Support\Helpers\Http\Builder as HttpBuilder; | ||
|
||
class Url | ||
{ | ||
public function parse(string $url): HttpBuilder | ||
{ | ||
return Builder::parse($url); | ||
} | ||
|
||
public function hash($url): string | ||
{ | ||
$uri = $this->parse($url); | ||
|
||
$query = $this->filterQuery($uri); | ||
|
||
$uri->withQuery($query); | ||
|
||
return md5($uri->toUrl()); | ||
} | ||
|
||
protected function filterQuery(HttpBuilder $uri): array | ||
{ | ||
if ($keys = $this->getIgnoreKeys()) { | ||
return Arrayable::of($uri->getQueryArray()) | ||
->filter(static function ($key) use ($keys) { | ||
return ! Str::is($keys, $key); | ||
}, ARRAY_FILTER_USE_KEY) | ||
->get(); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
protected function getIgnoreKeys(): array | ||
{ | ||
return ConfigSupport::requestIgnoreKeys(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "dragon-code/last-modified" project. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Andrey Helldar <helldar@ai-rus.com> | ||
* | ||
* @copyright 2021 Andrey Helldar | ||
* | ||
* @license MIT | ||
* | ||
* @see https://github.com/TheDragonCode/last-modified | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\WhenDisabled\Support; | ||
|
||
use DragonCode\LastModified\Facades\Url; | ||
use Tests\WhenEnabled\TestCase; | ||
|
||
class UrlTest extends TestCase | ||
{ | ||
public function testParse() | ||
{ | ||
$url = 'https://example.com/foo/bar?id=1&qwe=rty'; | ||
|
||
$value = Url::parse($url); | ||
|
||
$this->assertSame($url, $value->toUrl()); | ||
} | ||
|
||
public function testHash() | ||
{ | ||
$items = [ | ||
'https://example.com/foo/bar?id=1&foo=bar' => 'ce620bb9be7299260fd7144652d421f9', | ||
'https://example.com/foo/bar?id=1&qwe=rty&amoleds=123' => '88a3e09cfc343a4d63671da376463579', | ||
'https://example.com/foo/bar?id=1&qwe=rty&details=123' => '3217e42fb45cf73fe6d7fb2a3a4a77f8', | ||
|
||
'https://example.com/foo/bar?id=1&qwe=rty&modified=123' => '66691dbad27d95fa743d5c09761fd0cd', | ||
'https://example.com/foo/bar?id=1&qwe=rty&database=123' => '66691dbad27d95fa743d5c09761fd0cd', | ||
'https://example.com/foo/bar?id=1&qwe=rty&amoled=123' => '66691dbad27d95fa743d5c09761fd0cd', | ||
'https://example.com/foo/bar?id=1&qwe=rty' => '66691dbad27d95fa743d5c09761fd0cd', | ||
'https://example.com/foo/bar?id=1' => '66691dbad27d95fa743d5c09761fd0cd', | ||
|
||
'https://example.com/foo/bar' => 'f6c18df6c8b2aa33d62818079fe6815d', | ||
]; | ||
|
||
foreach ($items as $url => $hash) { | ||
$message = sprintf('Url %s has an invalid hash', $url); | ||
|
||
$this->assertSame($hash, Url::hash($url), $message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "dragon-code/last-modified" project. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Andrey Helldar <helldar@ai-rus.com> | ||
* | ||
* @copyright 2021 Andrey Helldar | ||
* | ||
* @license MIT | ||
* | ||
* @see https://github.com/TheDragonCode/last-modified | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\WhenEnabled\Support; | ||
|
||
use DragonCode\LastModified\Facades\Url; | ||
use Tests\WhenEnabled\TestCase; | ||
|
||
class UrlTest extends TestCase | ||
{ | ||
public function testParse() | ||
{ | ||
$url = 'https://example.com/foo/bar?id=1&qwe=rty'; | ||
|
||
$value = Url::parse($url); | ||
|
||
$this->assertSame($url, $value->toUrl()); | ||
} | ||
|
||
public function testHash() | ||
{ | ||
$items = [ | ||
'https://example.com/foo/bar?id=1&foo=bar' => 'ce620bb9be7299260fd7144652d421f9', | ||
'https://example.com/foo/bar?id=1&qwe=rty&amoleds=123' => '88a3e09cfc343a4d63671da376463579', | ||
'https://example.com/foo/bar?id=1&qwe=rty&details=123' => '3217e42fb45cf73fe6d7fb2a3a4a77f8', | ||
|
||
'https://example.com/foo/bar?id=1&qwe=rty&modified=123' => '66691dbad27d95fa743d5c09761fd0cd', | ||
'https://example.com/foo/bar?id=1&qwe=rty&database=123' => '66691dbad27d95fa743d5c09761fd0cd', | ||
'https://example.com/foo/bar?id=1&qwe=rty&amoled=123' => '66691dbad27d95fa743d5c09761fd0cd', | ||
'https://example.com/foo/bar?id=1&qwe=rty' => '66691dbad27d95fa743d5c09761fd0cd', | ||
'https://example.com/foo/bar?id=1' => '66691dbad27d95fa743d5c09761fd0cd', | ||
|
||
'https://example.com/foo/bar' => 'f6c18df6c8b2aa33d62818079fe6815d', | ||
]; | ||
|
||
foreach ($items as $url => $hash) { | ||
$message = sprintf('Url %s has an invalid hash', $url); | ||
|
||
$this->assertSame($hash, Url::hash($url), $message); | ||
} | ||
} | ||
} |