Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add URL refiners #153

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
* Static method `UserAgent::mozilla5CompatibleBrowser()` to get a `UserAgent` instance with the user agent string `Mozilla/5.0 (compatible)` and also the new method `withMozilla5CompatibleUserAgent` in the `AnonymousHttpCrawlerBuilder` that you can use like this: `HttpCrawler::make()->withMozilla5CompatibleUserAgent()`.
* URL refiners: `UrlRefiner::withScheme()`, `UrlRefiner::withHost()`, `UrlRefiner::withPort()`, `UrlRefiner::withoutPort()`, `UrlRefiner::withPath()`, `UrlRefiner::withQuery()`, `UrlRefiner::withoutQuery()`, `UrlRefiner::withFragment()` and `UrlRefiner::withoutFragment()`.

## [1.9.5] - 2024-07-25
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion src/Steps/Refiners/String/StrAfterFirst.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct(protected readonly string $first) {}
public function refine(mixed $value): mixed
{
if (!is_string($value)) {
$this->logTypeWarning('Str::afterFirst()', $value);
$this->logTypeWarning('StringRefiner::afterFirst()', $value);

return $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Steps/Refiners/String/StrAfterLast.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct(protected readonly string $last) {}
public function refine(mixed $value): mixed
{
if (!is_string($value)) {
$this->logTypeWarning('Str::afterLast()', $value);
$this->logTypeWarning('StringRefiner::afterLast()', $value);

return $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Steps/Refiners/String/StrBeforeFirst.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct(protected readonly string $first) {}
public function refine(mixed $value): mixed
{
if (!is_string($value)) {
$this->logTypeWarning('Str::beforeFirst()', $value);
$this->logTypeWarning('StringRefiner::beforeFirst()', $value);

return $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Steps/Refiners/String/StrBeforeLast.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct(protected readonly string $last) {}
public function refine(mixed $value): mixed
{
if (!is_string($value)) {
$this->logTypeWarning('Str::beforeLast()', $value);
$this->logTypeWarning('StringRefiner::beforeLast()', $value);

return $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Steps/Refiners/String/StrBetweenFirst.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct(protected readonly string $start, protected readonly
public function refine(mixed $value): mixed
{
if (!is_string($value)) {
$this->logTypeWarning('Str::betweenFirst()', $value);
$this->logTypeWarning('StringRefiner::betweenFirst()', $value);

return $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Steps/Refiners/String/StrBetweenLast.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct(protected readonly string $start, protected readonly
public function refine(mixed $value): mixed
{
if (!is_string($value)) {
$this->logTypeWarning('Str::betweenLast()', $value);
$this->logTypeWarning('StringRefiner::betweenLast()', $value);

return $value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Steps/Refiners/String/StrReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
public function refine(mixed $value): mixed
{
if (!is_string($value)) {
$this->logTypeWarning('Str::replace()', $value);
$this->logTypeWarning('StringRefiner::replace()', $value);

return $value;
}
Expand Down
34 changes: 34 additions & 0 deletions src/Steps/Refiners/Url/AbstractUrlRefiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Crwlr\Crawler\Steps\Refiners\Url;

use Crwlr\Crawler\Steps\Refiners\AbstractRefiner;
use Crwlr\Url\Exceptions\InvalidUrlComponentException;
use Crwlr\Url\Url;
use Exception;
use Psr\Http\Message\UriInterface;

abstract class AbstractUrlRefiner extends AbstractRefiner
{
/**
* @throws InvalidUrlComponentException|Exception
*/
public function refine(mixed $value): mixed
{
if (!is_string($value) && !$value instanceof Url && !$value instanceof UriInterface) {
$this->logTypeWarning($this->staticRefinerMethod(), $value);

return $value;
}

if (!$value instanceof Url) {
$value = Url::parse($value);
}

return $this->refineUrl($value);
}

abstract protected function staticRefinerMethod(): string;

abstract protected function refineUrl(Url $url): string;
}
27 changes: 27 additions & 0 deletions src/Steps/Refiners/Url/WithFragment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Crwlr\Crawler\Steps\Refiners\Url;

use Crwlr\Url\Exceptions\InvalidUrlComponentException;
use Crwlr\Url\Url;
use Exception;

class WithFragment extends AbstractUrlRefiner
{
public function __construct(protected readonly string $fragment) {}

protected function staticRefinerMethod(): string
{
return 'UrlRefiner::withFragment()';
}

/**
* @throws InvalidUrlComponentException|Exception
*/
protected function refineUrl(Url $url): string
{
$url->fragment($this->fragment);

return (string) $url;
}
}
27 changes: 27 additions & 0 deletions src/Steps/Refiners/Url/WithHost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Crwlr\Crawler\Steps\Refiners\Url;

use Crwlr\Url\Exceptions\InvalidUrlComponentException;
use Crwlr\Url\Url;
use Exception;

class WithHost extends AbstractUrlRefiner
{
public function __construct(protected readonly string $host) {}

protected function staticRefinerMethod(): string
{
return 'UrlRefiner::withHost()';
}

/**
* @throws InvalidUrlComponentException|Exception
*/
protected function refineUrl(Url $url): string
{
$url->host($this->host);

return (string) $url;
}
}
27 changes: 27 additions & 0 deletions src/Steps/Refiners/Url/WithPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Crwlr\Crawler\Steps\Refiners\Url;

use Crwlr\Url\Exceptions\InvalidUrlComponentException;
use Crwlr\Url\Url;
use Exception;

class WithPath extends AbstractUrlRefiner
{
public function __construct(protected readonly string $path) {}

protected function staticRefinerMethod(): string
{
return 'UrlRefiner::withPath()';
}

/**
* @throws InvalidUrlComponentException|Exception
*/
protected function refineUrl(Url $url): string
{
$url->path($this->path);

return (string) $url;
}
}
27 changes: 27 additions & 0 deletions src/Steps/Refiners/Url/WithPort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Crwlr\Crawler\Steps\Refiners\Url;

use Crwlr\Url\Exceptions\InvalidUrlComponentException;
use Crwlr\Url\Url;
use Exception;

class WithPort extends AbstractUrlRefiner
{
public function __construct(protected readonly int $port) {}

protected function staticRefinerMethod(): string
{
return 'UrlRefiner::withPort()';
}

/**
* @throws InvalidUrlComponentException|Exception
*/
protected function refineUrl(Url $url): string
{
$url->port($this->port);

return (string) $url;
}
}
27 changes: 27 additions & 0 deletions src/Steps/Refiners/Url/WithQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Crwlr\Crawler\Steps\Refiners\Url;

use Crwlr\Url\Exceptions\InvalidUrlComponentException;
use Crwlr\Url\Url;
use Exception;

class WithQuery extends AbstractUrlRefiner
{
public function __construct(protected readonly string $query) {}

protected function staticRefinerMethod(): string
{
return 'UrlRefiner::withQuery()';
}

/**
* @throws InvalidUrlComponentException|Exception
*/
protected function refineUrl(Url $url): string
{
$url->query($this->query);

return (string) $url;
}
}
27 changes: 27 additions & 0 deletions src/Steps/Refiners/Url/WithScheme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Crwlr\Crawler\Steps\Refiners\Url;

use Crwlr\Url\Exceptions\InvalidUrlComponentException;
use Crwlr\Url\Url;
use Exception;

class WithScheme extends AbstractUrlRefiner
{
public function __construct(protected readonly string $scheme) {}

protected function staticRefinerMethod(): string
{
return 'UrlRefiner::withScheme()';
}

/**
* @throws InvalidUrlComponentException|Exception
*/
protected function refineUrl(Url $url): string
{
$url->scheme($this->scheme);

return (string) $url;
}
}
25 changes: 25 additions & 0 deletions src/Steps/Refiners/Url/WithoutPort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Crwlr\Crawler\Steps\Refiners\Url;

use Crwlr\Url\Exceptions\InvalidUrlComponentException;
use Crwlr\Url\Url;
use Exception;

class WithoutPort extends AbstractUrlRefiner
{
protected function staticRefinerMethod(): string
{
return 'UrlRefiner::withoutPort()';
}

/**
* @throws InvalidUrlComponentException|Exception
*/
protected function refineUrl(Url $url): string
{
$url->resetPort();

return (string) $url;
}
}
59 changes: 59 additions & 0 deletions src/Steps/Refiners/UrlRefiner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Crwlr\Crawler\Steps\Refiners;

use Crwlr\Crawler\Steps\Refiners\Url\WithFragment;
use Crwlr\Crawler\Steps\Refiners\Url\WithHost;
use Crwlr\Crawler\Steps\Refiners\Url\WithoutPort;
use Crwlr\Crawler\Steps\Refiners\Url\WithPath;
use Crwlr\Crawler\Steps\Refiners\Url\WithPort;
use Crwlr\Crawler\Steps\Refiners\Url\WithQuery;
use Crwlr\Crawler\Steps\Refiners\Url\WithScheme;

class UrlRefiner
{
public static function withScheme(string $scheme): WithScheme
{
return new WithScheme($scheme);
}

public static function withHost(string $host): WithHost
{
return new WithHost($host);
}

public static function withPort(int $port): WithPort
{
return new WithPort($port);
}

public static function withoutPort(): WithoutPort
{
return new WithoutPort();
}

public static function withPath(string $path): WithPath
{
return new WithPath($path);
}

public static function withQuery(string $query): WithQuery
{
return new WithQuery($query);
}

public static function withoutQuery(): WithQuery
{
return new WithQuery('');
}

public static function withFragment(string $fragment): WithFragment
{
return new WithFragment($fragment);
}

public static function withoutFragment(): WithFragment
{
return new WithFragment('');
}
}
2 changes: 1 addition & 1 deletion tests/Steps/Refiners/String/AfterFirstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

$logOutput = $this->getActualOutputForAssertion();

expect($logOutput)->toContain('Refiner Str::afterFirst() can\'t be applied to value of type ' . gettype($value));
expect($logOutput)->toContain('Refiner StringRefiner::afterFirst() can\'t be applied to value of type ' . gettype($value));

expect($refinedValue)->toBe($value);
})->with([
Expand Down
2 changes: 1 addition & 1 deletion tests/Steps/Refiners/String/AfterLastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

$logOutput = $this->getActualOutputForAssertion();

expect($logOutput)->toContain('Refiner Str::afterLast() can\'t be applied to value of type ' . gettype($value));
expect($logOutput)->toContain('Refiner StringRefiner::afterLast() can\'t be applied to value of type ' . gettype($value));

expect($refinedValue)->toBe($value);
})->with([
Expand Down
2 changes: 1 addition & 1 deletion tests/Steps/Refiners/String/BeforeFirstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

$logOutput = $this->getActualOutputForAssertion();

expect($logOutput)->toContain('Refiner Str::beforeFirst() can\'t be applied to value of type ' . gettype($value));
expect($logOutput)->toContain('Refiner StringRefiner::beforeFirst() can\'t be applied to value of type ' . gettype($value));

expect($refinedValue)->toBe($value);
})->with([
Expand Down
2 changes: 1 addition & 1 deletion tests/Steps/Refiners/String/BeforeLastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

$logOutput = $this->getActualOutputForAssertion();

expect($logOutput)->toContain('Refiner Str::beforeLast() can\'t be applied to value of type ' . gettype($value));
expect($logOutput)->toContain('Refiner StringRefiner::beforeLast() can\'t be applied to value of type ' . gettype($value));

expect($refinedValue)->toBe($value);
})->with([
Expand Down
Loading