-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate CSS Selectors and XPath Queries earlier
When creating a `CssSelector` or `XPathQuery` instance with invalid selector/query syntax, an `InvalidDomQueryException` is now immediately thrown. This change is considered to be not only non-breaking, but actually a fix, because the `CssSelector` would otherwise throw an exception later when the `apply()` method is called. The `XPathQuery` would silently return no result without notifying you of the invalid query and generate a PHP warning.
- Loading branch information
Showing
8 changed files
with
125 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Crwlr\Crawler\Steps\Html\Exceptions; | ||
|
||
use Exception; | ||
use Symfony\Component\CssSelector\Exception\ExpressionErrorException; | ||
use Symfony\Component\CssSelector\Exception\SyntaxErrorException; | ||
|
||
class InvalidDomQueryException extends Exception | ||
{ | ||
protected string $query = ''; | ||
|
||
public static function make(string $message, string $domQuery): self | ||
{ | ||
$exception = new self($message); | ||
|
||
$exception->setDomQuery($domQuery); | ||
|
||
return $exception; | ||
} | ||
|
||
public static function fromSymfonyException( | ||
string $domQuery, | ||
ExpressionErrorException|SyntaxErrorException $originalException, | ||
): self { | ||
$exception = new self( | ||
$originalException->getMessage(), | ||
$originalException->getCode(), | ||
$originalException, | ||
); | ||
|
||
$exception->setDomQuery($domQuery); | ||
|
||
return $exception; | ||
} | ||
|
||
public function setDomQuery(string $domQuery): void | ||
{ | ||
$this->query = $domQuery; | ||
} | ||
} |
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