Skip to content

Commit

Permalink
Improve codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Sep 24, 2023
1 parent de74ab7 commit d6100e6
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,23 +355,28 @@ private function streamToString($stream): string
*/
public function parseHTML(DOMDocument|DOMElement|SimpleXMLElement|Stringable|string $source): TabularDataReader
{
$xpath = new DOMXPath($this->sourceToDomDocument($source));
/** @var DOMNodeList<DOMElement> $query */
$query = $xpath->query($this->expression);
$query = (new DOMXPath($this->sourceToDomDocument($source)))->query($this->expression);
$table = $query->item($this->tableOffset);
if (!$table instanceof DOMElement) {
throw new ParserError('The HTML table could not be found in the submitted html.');
}

return match (true) {
$table instanceof DOMElement => $this->convert(new DOMXPath($this->sourceToDomDocument($table))),
default => throw new ParserError('The HTML table could not be found in the submitted html.'),
$xpath = new DOMXPath($this->sourceToDomDocument($table));
$header = match (true) {
[] !== $this->tableHeader => $this->tableHeader,
$this->ignoreTableHeader => [],
default => $this->extractTableHeader($xpath, $this->tableHeaderSection->xpath()),
};

return new ResultSet($this->extractTableContents($xpath, $header), $header);
}

/**
* @throws ParserError
*/
private function sourceToDomDocument(
DOMDocument|SimpleXMLElement|DOMElement|Stringable|string $document,
): DOMDocument {
private function sourceToDomDocument(DOMDocument|SimpleXMLElement|DOMElement|Stringable|string $document): DOMDocument
{
if ($document instanceof DOMDocument) {
return $document;
}
Expand Down Expand Up @@ -400,21 +405,6 @@ private function sourceToDomDocument(
};
}

/**
* @throws ParserError
* @throws SyntaxError
*/
private function convert(DOMXPath $xpath): TabularDataReader
{
$header = match (true) {
[] !== $this->tableHeader => $this->tableHeader,
$this->ignoreTableHeader => [],
default => $this->extractTableHeader($xpath, $this->tableHeaderSection->xpath()),
};

return new ResultSet($this->extractTableContents($xpath, $header), $header);
}

/**
* @return array<string>
*/
Expand Down

0 comments on commit d6100e6

Please sign in to comment.