4
4
5
5
namespace OCR \Engine ;
6
6
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 ;
12
12
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 ;
16
14
17
15
class OcrSpaceEngine extends AbstractHttpEngine
18
16
{
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 ;
24
18
25
19
private MultipartFormFactoryInterface $ formFactory ;
26
20
27
21
private string $ key ;
28
22
29
23
public function __construct (
30
24
ClientInterface $ client ,
31
- RequestFactoryInterface $ requestFactory ,
25
+ BaseRequestFactoryInterface $ requestFactory ,
32
26
MultipartFormFactoryInterface $ formFactory ,
33
27
string $ key
34
28
) {
@@ -39,96 +33,21 @@ public function __construct(
39
33
$ this ->key = $ key ;
40
34
}
41
35
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
59
37
{
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
+ );
114
43
115
- return $ data ;
44
+ return $ factory ;
116
45
}
117
46
118
- /**
119
- * @param mixed[] $data
120
- */
121
- private function validateResponse (array $ data ): void
47
+ protected function createResponseParser (): ResponseParserInterface
122
48
{
123
- $ exitCode = $ data ['OCRExitCode ' ];
124
- $ success = (
125
- $ exitCode === 1
126
- ||
127
- $ exitCode === 2
128
- );
49
+ $ parser = new OcrSpaceResponseParser ();
129
50
130
- if (!$ success ) {
131
- throw new Exception ('Character recognition failed. ' );
132
- }
51
+ return $ parser ;
133
52
}
134
53
}
0 commit comments