Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more dedicated exceptions #739

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading