Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
77.78% |
14 / 18 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
PdfFileWordCounter | |
77.78% |
14 / 18 |
|
0.00% |
0 / 2 |
8.70 | |
0.00% |
0 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
isSupported | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
countFromFile | |
80.00% |
12 / 15 |
|
0.00% |
0 / 1 |
4.13 |
1 | <?php |
2 | namespace Apie\CountWords\Strategies; |
3 | |
4 | use Apie\CountWords\Strategies\Concerns\UseStringForResource; |
5 | use Apie\CountWords\Strategies\Concerns\UseTempFileForString; |
6 | use Apie\CountWords\WordCounter; |
7 | use Smalot\PdfParser\Parser; |
8 | |
9 | final class PdfFileWordCounter implements WordCounterInterface |
10 | { |
11 | use UseStringForResource; |
12 | use UseTempFileForString; |
13 | /** |
14 | * @codeCoverageIgnore |
15 | */ |
16 | private function __construct() |
17 | { |
18 | } |
19 | |
20 | public static function isSupported(?string $fileExtension, ?string $mimeType): bool |
21 | { |
22 | if (!class_exists(Parser::class)) { |
23 | return false; |
24 | } |
25 | return $fileExtension === 'pdf' || $mimeType === 'application/pdf'; |
26 | } |
27 | |
28 | public static function countFromFile(string $path, array $counts = []): array |
29 | { |
30 | $parser = new Parser(); |
31 | try { |
32 | $pdf = $parser->parseFile($path); |
33 | $text = $pdf->getText(); |
34 | } catch (\Exception) { |
35 | return $counts; |
36 | } |
37 | $result = WordCounter::countFromString($text, $counts); |
38 | try { |
39 | $details = $pdf->getDetails(); |
40 | unset($details['CreationDate']); |
41 | unset($details['ModDate']); |
42 | unset($details['Pages']); |
43 | unset($details['Producer']); |
44 | foreach ($details as $detail) { |
45 | $result = WordCounter::countFromString($detail, $result); |
46 | } |
47 | } catch (\Exception) { |
48 | } |
49 | return $result; |
50 | } |
51 | } |