Skip to content

Commit

Permalink
Adding more dedicated exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasLandauer committed Oct 2, 2024
1 parent 15da82a commit ea85dc8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/Smalot/PdfParser/Exception/EmptyPdfException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types = 1);

namespace Smalot\PdfParser\Exception;

class EmptyPdfException extends \Exception
{
}
7 changes: 7 additions & 0 deletions src/Smalot/PdfParser/Exception/MissingPdfHeaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php declare(strict_types = 1);

namespace Smalot\PdfParser\Exception;

class MissingPdfHeaderException extends \Exception
{
}
10 changes: 6 additions & 4 deletions src/Smalot/PdfParser/RawData/RawDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
namespace Smalot\PdfParser\RawData;

use Smalot\PdfParser\Config;
use Smalot\PdfParser\Exception\MissingPdfHeaderException;
use Smalot\PdfParser\Exception\EmptyPdfException;

class RawDataParser
{
Expand Down Expand Up @@ -938,17 +940,17 @@ protected function getXrefData(string $pdfData, int $offset = 0, array $xref = [
*
* @return array array of parsed PDF document objects
*
* @throws \Exception if empty PDF data given
* @throws \Exception if PDF data missing %PDF header
* @throws EmptyPdfException if empty PDF data given
* @throws MissingPdfHeaderException if PDF data missing `%PDF-` header
*/
public function parseData(string $data): array
{
if (empty($data)) {
throw new \Exception('Empty PDF data given.');
throw new EmptyPdfException('Empty PDF data given.');
}
// find the pdf header starting position
if (false === ($trimpos = strpos($data, '%PDF-'))) {
throw new \Exception('Invalid PDF data: missing %PDF header.');
throw new MissingPdfHeaderException('Invalid PDF data: Missing `%PDF-` header.');
}

// get PDF content string
Expand Down

0 comments on commit ea85dc8

Please sign in to comment.