diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ac6950..0f275a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/src/Parser.php b/src/Parser.php index 298c384..97b70c3 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -76,7 +76,7 @@ public static function new(): self } /** - * @throws Error + * @throws ParserError */ public function tablePosition(int|string $positionOrId): self { @@ -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, @@ -117,14 +117,14 @@ public function tablePosition(int|string $positionOrId): self /** * @param array $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, @@ -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, @@ -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 @@ -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); @@ -335,7 +335,7 @@ public function parseFile($filenameOrStream, $filenameContext = null): TabularDa /** * @param resource $stream * - * @throws Error + * @throws ParserError */ private function streamToString($stream): string { @@ -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 @@ -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, @@ -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 diff --git a/src/Error.php b/src/ParserError.php similarity index 96% rename from src/Error.php rename to src/ParserError.php index a52fb79..b5e2b57 100644 --- a/src/Error.php +++ b/src/ParserError.php @@ -16,7 +16,7 @@ use const PHP_EOL; -class Error extends InvalidArgumentException +class ParserError extends InvalidArgumentException { /** @var array */ private array $duplicateColumnNames = []; @@ -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; } } diff --git a/src/ErrorTest.php b/src/ParserErrorTest.php similarity index 79% rename from src/ErrorTest.php rename to src/ParserErrorTest.php index 5c3c739..8917158 100644 --- a/src/ErrorTest.php +++ b/src/ParserErrorTest.php @@ -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()); diff --git a/src/ParserTest.php b/src/ParserTest.php index e31ff27..1e22b71 100644 --- a/src/ParserTest.php +++ b/src/ParserTest.php @@ -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); @@ -66,7 +66,7 @@ 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 */ } @@ -74,7 +74,7 @@ public function it_will_throw_if_the_header_does_not_only_contains_string(): voi #[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'); } @@ -82,7 +82,7 @@ public function it_will_throw_if_the_identifier_is_invalid(): void #[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); } @@ -90,7 +90,7 @@ public function it_will_throw_if_the_identifier_is_a_negative_integer(): void #[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 */ } @@ -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'); } @@ -210,7 +210,7 @@ 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'); } @@ -218,7 +218,7 @@ public function it_will_throw_if_the_html_is_malformed(): void #[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('
  1. foo
'); } @@ -291,7 +291,7 @@ public function it_will_fails_on_malformed_html(): void df

sghfd TABLE; - $this->expectException(Error::class); + $this->expectException(ParserError::class); Parser::new() ->failOnXmlErrors() @@ -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')); }