Skip to content

Commit

Permalink
Error replaced by ParserError
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Sep 24, 2023
1 parent 7b355c3 commit a79fa95
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All Notable changes to `bakame/html-table` will be documented in this file.

- `Parser::withFormatter`
- `Parser::withoutFormatter`
- `ParserError` to replace `Error` exception

### Fixed

Expand All @@ -19,7 +20,7 @@ All Notable changes to `bakame/html-table` will be documented in this file.

### Removed

- None
- `Error` exception is renamed `ParserError`

## [0.1.0] - 2023-09-22

Expand Down
32 changes: 16 additions & 16 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function new(): self
}

/**
* @throws Error
* @throws ParserError
*/
public function tablePosition(int|string $positionOrId): self
{
Expand All @@ -97,9 +97,9 @@ public function tablePosition(int|string $positionOrId): self
$this->includeTableFooter,
$this->formatter,
),
default => throw new Error('the table offset must be a positive integer or the table id attribute value.'),
default => throw new ParserError('the table offset must be a positive integer or the table id attribute value.'),
},
1 === preg_match(",\s,", $positionOrId) => throw new Error("The id attribute's value must not contain whitespace (spaces, tabs etc.)"),
1 === preg_match(",\s,", $positionOrId) => throw new ParserError("The id attribute's value must not contain whitespace (spaces, tabs etc.)"),
default => new self(
$expression,
0,
Expand All @@ -117,14 +117,14 @@ public function tablePosition(int|string $positionOrId): self
/**
* @param array<string> $headerRow
*
* @throws Error
* @throws ParserError
*/
public function tableHeader(array $headerRow): self
{
return match (true) {
$headerRow === $this->tableHeader => $this,
$headerRow !== ($filteredHeader = array_filter($headerRow, is_string(...))) => throw new Error('The header record contains non string colum names.'),
$headerRow !== array_unique($filteredHeader) => throw Error::dueToDuplicateHeaderColumnNames($headerRow),
$headerRow !== ($filteredHeader = array_filter($headerRow, is_string(...))) => throw new ParserError('The header record contains non string colum names.'),
$headerRow !== array_unique($filteredHeader) => throw ParserError::dueToDuplicateHeaderColumnNames($headerRow),
default => new self(
$this->expression,
$this->tableOffset,
Expand Down Expand Up @@ -182,7 +182,7 @@ public function tableHeaderPosition(Section $section, int $offset = 0): self
{
return match (true) {
$section === $this->tableHeaderSection && $offset === $this->tableHeaderOffset => $this,
$offset < 0 => throw new Error('The table header row offset must be a positive integer or 0.'), /* @phpstan-ignore-line */
$offset < 0 => throw new ParserError('The table header row offset must be a positive integer or 0.'), /* @phpstan-ignore-line */
default => new self(
$this->expression,
$this->tableOffset,
Expand Down Expand Up @@ -306,7 +306,7 @@ public function withoutFormatter(): self
* @param resource|string $filenameOrStream
* @param resource|null $filenameContext
*
* @throws Error
* @throws ParserError
* @throws SyntaxError
*/
public function parseFile($filenameOrStream, $filenameContext = null): TabularDataReader
Expand All @@ -323,7 +323,7 @@ public function parseFile($filenameOrStream, $filenameContext = null): TabularDa
restore_error_handler();

if (!is_resource($resource)) {
throw new Error('`'.$filenameOrStream.'`: failed to open stream: No such file or directory.');
throw new ParserError('`'.$filenameOrStream.'`: failed to open stream: No such file or directory.');
}

$html = $this->streamToString($resource);
Expand All @@ -335,7 +335,7 @@ public function parseFile($filenameOrStream, $filenameContext = null): TabularDa
/**
* @param resource $stream
*
* @throws Error
* @throws ParserError
*/
private function streamToString($stream): string
{
Expand All @@ -344,13 +344,13 @@ private function streamToString($stream): string
restore_error_handler();

return match (false) {
$html => throw new Error('The resource could not be read.'),
$html => throw new ParserError('The resource could not be read.'),
default => $html,
};
}

/**
* @throws Error
* @throws ParserError
* @throws SyntaxError
*/
public function parseHTML(DOMDocument|DOMElement|SimpleXMLElement|Stringable|string $source): TabularDataReader
Expand All @@ -362,12 +362,12 @@ public function parseHTML(DOMDocument|DOMElement|SimpleXMLElement|Stringable|str

return match (true) {
$table instanceof DOMElement => $this->convert(new DOMXPath($this->sourceToDomDocument($table))),
default => throw new Error('The HTML table could not be found in the submitted html.'),
default => throw new ParserError('The HTML table could not be found in the submitted html.'),
};
}

/**
* @throws Error
* @throws ParserError
*/
private function sourceToDomDocument(
DOMDocument|SimpleXMLElement|DOMElement|Stringable|string $document,
Expand Down Expand Up @@ -395,13 +395,13 @@ private function sourceToDomDocument(
libxml_clear_errors();

return match (true) {
$this->throwOnXmlErrors && [] !== $errors => throw Error::dueToLibXmlErrors($errors),
$this->throwOnXmlErrors && [] !== $errors => throw ParserError::dueToLibXmlErrors($errors),
default => $dom,
};
}

/**
* @throws Error
* @throws ParserError
* @throws SyntaxError
*/
private function convert(DOMXPath $xpath): TabularDataReader
Expand Down
3 changes: 1 addition & 2 deletions src/Error.php → src/ParserError.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use const PHP_EOL;

class Error extends InvalidArgumentException
class ParserError extends InvalidArgumentException
{
/** @var array<string> */
private array $duplicateColumnNames = [];
Expand Down Expand Up @@ -49,7 +49,6 @@ public static function dueToDuplicateHeaderColumnNames(array $header): self
$instance = new self('The header record contains duplicate column names: `'.implode('`, `', $duplicateColumnNames).'`.');
$instance->duplicateColumnNames = $duplicateColumnNames;


return $instance;
}
}
4 changes: 2 additions & 2 deletions src/ErrorTest.php → src/ParserErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

final class ErrorTest extends TestCase
final class ParserErrorTest extends TestCase
{
#[Test]
public function it_will_return_the_duplicated_column_names(): void
{
$headerRow = ['foo', 'foo', 'toto', 'toto', 'baz'];
$exception = Error::dueToDuplicateHeaderColumnNames($headerRow);
$exception = ParserError::dueToDuplicateHeaderColumnNames($headerRow);

self::assertSame('The header record contains duplicate column names: `foo`, `toto`.', $exception->getMessage());
self::assertSame(['foo', 'toto'], $exception->duplicateColumnNames());
Expand Down
20 changes: 10 additions & 10 deletions src/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function it_will_return_the_same_options(): void
public function it_will_throw_if_the_header_contains_duplicate_values(): void
{
$headerRow = ['foo', 'foo', 'toto', 'toto', 'baz'];
$this->expectException(Error::class);
$this->expectException(ParserError::class);
$this->expectExceptionMessage('The header record contains duplicate column names: `foo`, `toto`.');

Parser::new()->tableHeader($headerRow);
Expand All @@ -66,31 +66,31 @@ public function it_will_throw_if_the_header_contains_duplicate_values(): void
#[Test]
public function it_will_throw_if_the_header_does_not_only_contains_string(): void
{
$this->expectException(Error::class);
$this->expectException(ParserError::class);

Parser::new()->tableHeader(['foo', 1]); /* @phpstan-ignore-line */
}

#[Test]
public function it_will_throw_if_the_identifier_is_invalid(): void
{
$this->expectException(Error::class);
$this->expectException(ParserError::class);

Parser::new()->tablePosition('foo bar');
}

#[Test]
public function it_will_throw_if_the_identifier_is_a_negative_integer(): void
{
$this->expectException(Error::class);
$this->expectException(ParserError::class);

Parser::new()->tablePosition(-1);
}

#[Test]
public function it_will_throw_if_the_table_header_row_offset_is_negative(): void
{
$this->expectException(Error::class);
$this->expectException(ParserError::class);

Parser::new()->tableHeaderPosition(Section::Header, -1); /* @phpstan-ignore-line */
}
Expand Down Expand Up @@ -172,7 +172,7 @@ public function it_uses_the_table_first_tr_to_search_for_the_header(): void
#[Test]
public function it_will_fail_to_load_a_missing_file(): void
{
$this->expectException(Error::class);
$this->expectException(ParserError::class);

Parser::new()->parseFile('/path/tp/my/heart.html');
}
Expand Down Expand Up @@ -210,15 +210,15 @@ public function it_uses_the_table_first_tr_in_the_first_tbody_to_search_for_the_
#[Test]
public function it_will_throw_if_the_html_is_malformed(): void
{
$this->expectExceptionObject(new Error('The HTML table could not be found in the submitted html.'));
$this->expectExceptionObject(new ParserError('The HTML table could not be found in the submitted html.'));

Parser::new()->parseHTML('vasdfadadf');
}

#[Test]
public function it_will_throw_if_no_table_is_found(): void
{
$this->expectExceptionObject(new Error('The HTML table could not be found in the submitted html.'));
$this->expectExceptionObject(new ParserError('The HTML table could not be found in the submitted html.'));

Parser::new()->parseHTML('<ol><li>foo</li></ol>');
}
Expand Down Expand Up @@ -291,7 +291,7 @@ public function it_will_fails_on_malformed_html(): void
df<body></p>sghfd
TABLE;

$this->expectException(Error::class);
$this->expectException(ParserError::class);

Parser::new()
->failOnXmlErrors()
Expand All @@ -301,7 +301,7 @@ public function it_will_fails_on_malformed_html(): void
#[Test]
public function it_will_fail_to_load_other_html_tag(): void
{
$this->expectException(Error::class);
$this->expectException(ParserError::class);

Parser::new()->parseHTML(new DOMElement('p', 'I know who you are'));
}
Expand Down

0 comments on commit a79fa95

Please sign in to comment.