Skip to content

Commit

Permalink
Adding more dedicated exceptions (#739)
Browse files Browse the repository at this point in the history
Co-authored-by: Konrad Abicht <[email protected]>
  • Loading branch information
ThomasLandauer and k00ni authored Oct 31, 2024
1 parent f8ae4b4 commit afa8deb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/Smalot/PdfParser/Exception/EmptyPdfException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Smalot\PdfParser\Exception;

/**
* This Exception is thrown when no PDF data was given.
*/
class EmptyPdfException extends \Exception
{
}
12 changes: 12 additions & 0 deletions src/Smalot/PdfParser/Exception/MissingPdfHeaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Smalot\PdfParser\Exception;

/**
* This Exception is thrown when the %PDF- header is missing.
*/
class MissingPdfHeaderException extends \Exception
{
}
12 changes: 12 additions & 0 deletions src/Smalot/PdfParser/Exception/NotImplementedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Smalot\PdfParser\Exception;

/**
* This Exception is thrown when a functionality has not yet been implemented.
*/
class NotImplementedException extends \Exception
{
}
15 changes: 9 additions & 6 deletions src/Smalot/PdfParser/RawData/FilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

namespace Smalot\PdfParser\RawData;

use Smalot\PdfParser\Exception\NotImplementedException;

class FilterHelper
{
protected $availableFilters = ['ASCIIHexDecode', 'ASCII85Decode', 'LZWDecode', 'FlateDecode', 'RunLengthDecode'];
Expand All @@ -54,7 +56,8 @@ class FilterHelper
*
* @return string Decoded data string
*
* @throws \Exception if a certain decode function is not implemented yet
* @throws \Exception
* @throws \Smalot\PdfParser\Exception\NotImplementedException if a certain decode function is not implemented yet
*/
public function decodeFilter(string $filter, string $data, int $decodeMemoryLimit = 0): string
{
Expand All @@ -75,15 +78,15 @@ public function decodeFilter(string $filter, string $data, int $decodeMemoryLimi
return $this->decodeFilterRunLengthDecode($data);

case 'CCITTFaxDecode':
throw new \Exception('Decode CCITTFaxDecode not implemented yet.');
throw new NotImplementedException('Decode CCITTFaxDecode not implemented yet.');
case 'JBIG2Decode':
throw new \Exception('Decode JBIG2Decode not implemented yet.');
throw new NotImplementedException('Decode JBIG2Decode not implemented yet.');
case 'DCTDecode':
throw new \Exception('Decode DCTDecode not implemented yet.');
throw new NotImplementedException('Decode DCTDecode not implemented yet.');
case 'JPXDecode':
throw new \Exception('Decode JPXDecode not implemented yet.');
throw new NotImplementedException('Decode JPXDecode not implemented yet.');
case 'Crypt':
throw new \Exception('Decode Crypt not implemented yet.');
throw new NotImplementedException('Decode Crypt not implemented yet.');
default:
return $data;
}
Expand Down
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\EmptyPdfException;
use Smalot\PdfParser\Exception\MissingPdfHeaderException;

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 afa8deb

Please sign in to comment.