Skip to content

Commit e18a4a7

Browse files
committed
Update to version 2.0.0 of the http package
1 parent f1e8a63 commit e18a4a7

File tree

4 files changed

+157
-100
lines changed

4 files changed

+157
-100
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"require": {
88
"php": "^7.4|^8.0",
99
"ocr/core": "^1.0",
10-
"ocr/http": "^1.0",
10+
"ocr/http": "^2.0",
1111
"psr/http-client": "^1.0",
1212
"psr/http-factory": "^1.0",
1313
"psr/http-message": "^1.0"

sources/Engine/OcrSpaceEngine.php

Lines changed: 18 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,25 @@
44

55
namespace OCR\Engine;
66

7-
use OCR\Exception\Exception;
8-
use OCR\Input\InputInterface;
9-
use OCR\Utility\Http\MultipartFormFactoryInterface;
10-
use OCR\Utility\Http\MultipartFormInterface;
11-
use OCR\Utility\Http\MultipartFormItem;
7+
use OCR\Utility\Http\Request\Multipart\MultipartFormFactoryInterface;
8+
use OCR\Utility\Http\Request\OcrSpaceRequestFactory;
9+
use OCR\Utility\Http\Request\RequestFactoryInterface;
10+
use OCR\Utility\Http\Response\OcrSpaceResponseParser;
11+
use OCR\Utility\Http\Response\ResponseParserInterface;
1212
use Psr\Http\Client\ClientInterface;
13-
use Psr\Http\Message\RequestFactoryInterface;
14-
use Psr\Http\Message\RequestInterface;
15-
use Psr\Http\Message\ResponseInterface;
13+
use Psr\Http\Message\RequestFactoryInterface as BaseRequestFactoryInterface;
1614

1715
class OcrSpaceEngine extends AbstractHttpEngine
1816
{
19-
private const API_ENDPOINT = 'https://api.ocr.space/parse/image';
20-
21-
private const API_METHOD = 'POST';
22-
23-
private RequestFactoryInterface $requestFactory;
17+
private BaseRequestFactoryInterface $requestFactory;
2418

2519
private MultipartFormFactoryInterface $formFactory;
2620

2721
private string $key;
2822

2923
public function __construct(
3024
ClientInterface $client,
31-
RequestFactoryInterface $requestFactory,
25+
BaseRequestFactoryInterface $requestFactory,
3226
MultipartFormFactoryInterface $formFactory,
3327
string $key
3428
) {
@@ -39,96 +33,21 @@ public function __construct(
3933
$this->key = $key;
4034
}
4135

42-
protected function createRequest(InputInterface $input): RequestInterface
43-
{
44-
$form = $this->createRequestForm($input);
45-
$contentType = $form->getContentType();
46-
$body = $form->getStream();
47-
48-
$request = $this->requestFactory
49-
->createRequest(self::API_METHOD, self::API_ENDPOINT)
50-
->withHeader('apikey', $this->key)
51-
->withHeader('content-type', $contentType)
52-
->withBody($body)
53-
;
54-
55-
return $request;
56-
}
57-
58-
protected function parseResponse(ResponseInterface $response): string
36+
protected function createRequestFactory(): RequestFactoryInterface
5937
{
60-
$data = $this->decodeResponse($response);
61-
$this->validateResponse($data);
62-
63-
$getResult = function (array $data): string {
64-
$result = $data['ParsedText'] ?? null;
65-
if (!is_string($result)) {
66-
throw new Exception('Unexpected recognition service response.');
67-
}
68-
return $result;
69-
};
70-
71-
$results = $data['ParsedResults'] ?? null;
72-
if (!is_array($results)) {
73-
throw new Exception('Unexpected recognition service response.');
74-
}
75-
76-
$results = array_map($getResult, $results);
77-
$results = implode("\n", $results);
78-
79-
return $results;
80-
}
81-
82-
private function createRequestForm(InputInterface $input): MultipartFormInterface
83-
{
84-
$items = [];
85-
86-
$language = $this->getLanguage();
87-
if ($language) {
88-
$language = $language->toString();
89-
$items[] = new MultipartFormItem('language', $language);
90-
}
91-
92-
$detectOrientation = $this->getDetectOrientation() ? 'true' : 'false';
93-
$items[] = new MultipartFormItem('detectOrientation', $detectOrientation);
94-
95-
$items[] = new MultipartFormItem('file', $input);
96-
97-
$form = $this->formFactory->createForm($items);
98-
99-
return $form;
100-
}
101-
102-
/**
103-
* @return mixed[]
104-
*/
105-
private function decodeResponse(ResponseInterface $response): array
106-
{
107-
$json = $response->getBody()->getContents();
108-
109-
/** @var mixed[]|bool|null $data */
110-
$data = json_decode($json, true);
111-
if (!is_array($data)) {
112-
throw new Exception('Unexpected recognition service response.');
113-
}
38+
$factory = new OcrSpaceRequestFactory(
39+
$this->requestFactory,
40+
$this->formFactory,
41+
$this->key,
42+
);
11443

115-
return $data;
44+
return $factory;
11645
}
11746

118-
/**
119-
* @param mixed[] $data
120-
*/
121-
private function validateResponse(array $data): void
47+
protected function createResponseParser(): ResponseParserInterface
12248
{
123-
$exitCode = $data['OCRExitCode'];
124-
$success = (
125-
$exitCode === 1
126-
||
127-
$exitCode === 2
128-
);
49+
$parser = new OcrSpaceResponseParser();
12950

130-
if (!$success) {
131-
throw new Exception('Character recognition failed.');
132-
}
51+
return $parser;
13352
}
13453
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OCR\Utility\Http\Request;
6+
7+
use OCR\Engine\EngineInterface;
8+
use OCR\Input\InputInterface;
9+
use OCR\Utility\Http\Request\Multipart\MultipartFormFactoryInterface;
10+
use OCR\Utility\Http\Request\Multipart\MultipartFormInterface;
11+
use OCR\Utility\Http\Request\Multipart\MultipartFormItem;
12+
use OCR\Utility\Http\Request\Multipart\MultipartFormItemInterface;
13+
use Psr\Http\Message\RequestFactoryInterface;
14+
use Psr\Http\Message\RequestInterface;
15+
16+
class OcrSpaceRequestFactory extends AbstractRequestFactory
17+
{
18+
private const API_ENDPOINT = 'https://api.ocr.space/parse/image';
19+
20+
private const API_METHOD = 'POST';
21+
22+
private string $key;
23+
24+
public function __construct(
25+
RequestFactoryInterface $requestFactory,
26+
MultipartFormFactoryInterface $formFactory,
27+
string $key
28+
) {
29+
parent::__construct($requestFactory, $formFactory);
30+
31+
$this->key = $key;
32+
}
33+
34+
public function createRequest(EngineInterface $engine, InputInterface $input): RequestInterface
35+
{
36+
$form = $this->createRequestForm($engine, $input);
37+
$contentType = $form->getContentType();
38+
$body = $form->getStream();
39+
40+
$request = $this->requestFactory
41+
->createRequest(self::API_METHOD, self::API_ENDPOINT)
42+
->withHeader('apikey', $this->key)
43+
->withHeader('content-type', $contentType)
44+
->withBody($body)
45+
;
46+
47+
return $request;
48+
}
49+
50+
private function createRequestForm(EngineInterface $engine, InputInterface $input): MultipartFormInterface
51+
{
52+
$items = $this->getRequestFormItems($engine, $input);
53+
$form = $this->formFactory->createForm($items);
54+
55+
return $form;
56+
}
57+
58+
/**
59+
* @return MultipartFormItemInterface[]
60+
*/
61+
private function getRequestFormItems(EngineInterface $engine, InputInterface $input): array
62+
{
63+
$items = [];
64+
65+
$language = $engine->getLanguage();
66+
if ($language) {
67+
$language = $language->toString();
68+
$items[] = new MultipartFormItem('language', $language);
69+
}
70+
71+
$detectOrientation = $engine->getDetectOrientation() ? 'true' : 'false';
72+
$items[] = new MultipartFormItem('detectOrientation', $detectOrientation);
73+
74+
$items[] = new MultipartFormItem('file', $input);
75+
76+
return $items;
77+
}
78+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OCR\Utility\Http\Response;
6+
7+
use OCR\Exception\Exception;
8+
9+
class OcrSpaceResponseParser extends JsonResponseParser
10+
{
11+
protected function validateResponseData(array $data): bool
12+
{
13+
$exitCode = $data['OCRExitCode'];
14+
if (!is_integer($exitCode)) {
15+
throw new Exception();
16+
}
17+
18+
$success = (
19+
$exitCode === 1
20+
||
21+
$exitCode === 2
22+
);
23+
24+
return $success;
25+
}
26+
27+
protected function parseResponseData(array $data): string
28+
{
29+
$results = $data['ParsedResults'] ?? null;
30+
if (!is_array($results)) {
31+
throw new Exception();
32+
}
33+
34+
foreach ($results as &$result) {
35+
if (!is_array($result)) {
36+
throw new Exception();
37+
}
38+
39+
$result = $this->parseResult($result);
40+
}
41+
42+
/** @var string[] $results */
43+
$result = implode("\n", $results);
44+
45+
return $result;
46+
}
47+
48+
/**
49+
* @param mixed[] $data
50+
*/
51+
private function parseResult(array $data): string
52+
{
53+
$result = $data['ParsedText'] ?? null;
54+
if (!is_string($result)) {
55+
throw new Exception();
56+
}
57+
58+
return $result;
59+
}
60+
}

0 commit comments

Comments
 (0)