@@ -23,49 +23,36 @@ class LocalResponse
2323 private $ file ;
2424
2525 /**
26- * @param resource|string|array $inputFile A string, path or file-like object to load as a local response.
26+ * @param resource|string|array<string> $inputFile A string, path or file-like object to load as a local response.
2727 * @throws MindeeException Throws if the input file isn't acceptable.
2828 */
2929 public function __construct (mixed $ inputFile )
3030 {
31- if (is_resource ($ inputFile ) && get_resource_type ($ inputFile ) === 'file ' ) {
32- $ content = fread ($ inputFile , fstat ($ inputFile )['size ' ]);
33- $ strStripped = str_replace (["\r" , "\n" ], '' , $ content );
34- $ this ->file = fopen ('php://memory ' , 'r+ ' );
35- fwrite ($ this ->file , $ strStripped );
36- rewind ($ this ->file );
37- } elseif (is_resource ($ inputFile ) && get_resource_type ($ inputFile ) === 'stream ' ) {
38- $ content = stream_get_contents ($ inputFile );
39- $ strStripped = str_replace (["\r" , "\n" ], '' , $ content );
40- $ this ->file = fopen ('php://memory ' , 'r+ ' );
41- fwrite ($ this ->file , $ strStripped );
42- rewind ($ this ->file );
43- } elseif (is_string ($ inputFile ) && file_exists ($ inputFile )) {
44- $ content = file_get_contents ($ inputFile );
45- $ strStripped = str_replace (["\r" , "\n" ], '' , $ content );
46- $ this ->file = fopen ('php://memory ' , 'r+ ' );
47- fwrite ($ this ->file , $ strStripped );
48- rewind ($ this ->file );
31+ if (is_resource ($ inputFile )) {
32+ $ resourceType = get_resource_type ($ inputFile );
33+ if ($ resourceType === 'file ' ) {
34+ $ content = fread ($ inputFile , fstat ($ inputFile )['size ' ]);
35+ } elseif ($ resourceType === 'stream ' ) {
36+ $ content = stream_get_contents ($ inputFile );
37+ } else {
38+ throw new MindeeException ("Unsupported resource type. " , ErrorCode::USER_INPUT_ERROR );
39+ }
4940 } elseif (is_string ($ inputFile )) {
50- $ strStripped = str_replace (["\r" , "\n" ], '' , $ inputFile );
51- $ this ->file = fopen ('php://memory ' , 'r+ ' );
52- fwrite ($ this ->file , $ strStripped );
53- rewind ($ this ->file );
54- } elseif (is_string ($ inputFile ) || is_array ($ inputFile )) {
55- if (is_array ($ inputFile ))
56- {
57- $ inputFile = implode ($ inputFile );
41+ if (file_exists ($ inputFile ) && is_file ($ inputFile )) {
42+ $ content = file_get_contents ($ inputFile );
43+ } else {
44+ $ content = $ inputFile ;
5845 }
59- $ strStripped = str_replace (["\r" , "\n" ], '' , $ inputFile );
60- $ this ->file = fopen ('php://memory ' , 'r+ ' );
61- fwrite ($ this ->file , $ strStripped );
62- rewind ($ this ->file );
46+ } elseif (is_array ($ inputFile )) {
47+ $ content = implode ('' , $ inputFile );
6348 } else {
64- throw new MindeeException (
65- "Incompatible type for input. " ,
66- ErrorCode::USER_INPUT_ERROR
67- );
49+ throw new MindeeException ("Incompatible type for input. " , ErrorCode::USER_INPUT_ERROR );
6850 }
51+
52+ $ strStripped = str_replace (["\r" , "\n" ], '' , (string ) $ content );
53+ $ this ->file = fopen ('php://memory ' , 'r+ ' );
54+ fwrite ($ this ->file , $ strStripped );
55+ rewind ($ this ->file );
6956 }
7057
7158 /**
0 commit comments