Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
v1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
joanfabregat committed Dec 6, 2024
1 parent a8300b5 commit 11157e4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 56 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codeinc/pdf2txt-client",
"version": "v1.5",
"version": "v1.6",
"description": "A PHP client for the pdf2txt service",
"homepage": "https://github.com/codeinchq/pdf2txt-php-client",
"type": "library",
Expand Down
76 changes: 24 additions & 52 deletions src/Pdf2TxtClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,43 @@
use Psr\Http\Message\StreamInterface;

/**
* Class Pdf2TxtClient
* Class Pdf2TxtClient provides a simple way to interact with the Pdf2Txt API.
*
* @package CodeInc\Pdf2TxtClient
* @link https://github.com/codeinchq/pdf2txt
* @link https://github.com/codeinchq/pdf2txt-php-client
* @link https://github.com/codeinchq/pdf2txt
* @link https://github.com/codeinchq/pdf2txt-php-client
* @license https://opensource.org/licenses/MIT MIT
*/
class Pdf2TxtClient
readonly class Pdf2TxtClient
{
public ClientInterface $client;
public StreamFactoryInterface $streamFactory;
public RequestFactoryInterface $requestFactory;

/**
* Pdf2TxtClient constructor.
*
* @param string $baseUrl
* @param ClientInterface|null $client
* @param StreamFactoryInterface|null $streamFactory
* @param RequestFactoryInterface|null $requestFactory
*/
public function __construct(
private readonly string $baseUrl,
private ClientInterface|null $client = null,
private StreamFactoryInterface|null $streamFactory = null,
private RequestFactoryInterface|null $requestFactory = null,
private string $baseUrl,
ClientInterface|null $client = null,
StreamFactoryInterface|null $streamFactory = null,
RequestFactoryInterface|null $requestFactory = null,
) {
$this->client ??= Psr18ClientDiscovery::find();
$this->streamFactory ??= Psr17FactoryDiscovery::findStreamFactory();
$this->requestFactory ??= Psr17FactoryDiscovery::findRequestFactory();
$this->client = $client ?? Psr18ClientDiscovery::find();
$this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory();
$this->requestFactory ??= $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
}

/**
* Converts a PDF to text using streams and the PDF2TEXT API.
*
* @param StreamInterface|resource|string $stream The PDF content.
* @param ConvertOptions $options The convert options.
* @param ConvertOptions $options The convert options.
* @return StreamInterface
* @throws Exception
*/
Expand Down Expand Up @@ -121,46 +133,6 @@ public function processJsonResponse(StreamInterface $response): array
);
}

/**
* Opens a local file and creates a stream from it.
*
* @param string $path The path to the file.
* @param string $openMode The mode used to open the file.
* @return StreamInterface
* @throws Exception
*/
public function createStreamFromFile(string $path, string $openMode = 'r'): StreamInterface
{
$f = fopen($path, $openMode);
if ($f === false) {
throw new Exception("The file '$path' could not be opened", Exception::ERROR_FILE_OPEN);
}

return $this->streamFactory->createStreamFromResource($f);
}

/**
* Saves a stream to a local file.
*
* @param StreamInterface $stream
* @param string $path The path to the file.
* @param string $openMode The mode used to open the file.
* @throws Exception
*/
public function saveStreamToFile(StreamInterface $stream, string $path, string $openMode = 'w'): void
{
$f = fopen($path, $openMode);
if ($f === false) {
throw new Exception("The file '$path' could not be opened", Exception::ERROR_FILE_OPEN);
}

if (stream_copy_to_stream($stream->detach(), $f) === false) {
throw new Exception("The stream could not be copied to the file '$path'", Exception::ERROR_FILE_WRITE);
}

fclose($f);
}

/**
* Returns the convert endpoint URI.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/Pdf2TxtClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testExtractionFromLocalFileToText(): void
{
$client = $this->getNewClient();

$stream = $client->extract($client->createStreamFromFile(self::TEST_PDF_PATH));
$stream = $client->extract($client->streamFactory->createStreamFromFile(self::TEST_PDF_PATH));
$this->assertInstanceOf(StreamInterface::class, $stream, "The stream is not valid");

$text = (string)$stream;
Expand All @@ -54,7 +54,7 @@ public function testExtractionFromLocalFileToRawJson(): void
$client = $this->getNewClient();

$stream = $client->extract(
$client->createStreamFromFile(self::TEST_PDF_PATH),
$client->streamFactory->createStreamFromFile(self::TEST_PDF_PATH),
new ConvertOptions(format: Format::json)
);
$this->assertInstanceOf(StreamInterface::class, $stream, "The stream is not valid");
Expand All @@ -72,7 +72,7 @@ public function testExtractionFromLocalFileToProcessedJson(): void
$client = $this->getNewClient();

$stream = $client->extract(
$client->createStreamFromFile(self::TEST_PDF_PATH),
$client->streamFactory->createStreamFromFile(self::TEST_PDF_PATH),
new ConvertOptions(format: Format::json)
);
$this->assertInstanceOf(StreamInterface::class, $stream, "The stream is not valid");
Expand Down

0 comments on commit 11157e4

Please sign in to comment.