Passed
Branch master (f320c5)
by Xavier
01:33
created

PubmedWebsite   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 43
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A extractAffiliationsData() 0 7 2
A __construct() 0 4 1
A extract() 0 6 1
A extractIdentifiersData() 0 8 2
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
use Symfony\Component\DomCrawler\Crawler;
6
7
class PubmedWebsite implements Extractor, ProvidesIdentifiersData, ProvidesAffiliationsData
8
{
9
    protected $document;
10
11
    protected $searchTree;
12
13
    protected $output = [];
14
15
    protected $crawler;
16
17
    public function __construct($document)
18
    {
19
        $this->crawler = new Crawler();
20
        $this->crawler->addHtmlContent($document);
21
    }
22
23
    public function extract(): array
24
    {
25
        $this->extractIdentifiersData();
26
        $this->extractAffiliationsData();
27
28
        return $this->output;
29
    }
30
31
    public function extractIdentifiersData()
32
    {
33
        $pubmed = stringify($this->crawler->evaluate('string(//input[@id="absid"]/@value)'));
34
35
        if (! empty($pubmed)) {
36
            $this->output['identifiers'][] = [
37
                'value' => $pubmed,
38
                'type' => 'pubmed',
39
            ];
40
        }
41
    }
42
43
    public function extractAffiliationsData()
44
    {
45
        $affiliations = $this->crawler->evaluate('//div[@class="afflist"]/dl/dd');
46
47
        foreach ($affiliations as $affiliation) {
48
            $this->output['affiliations'][] = [
49
                'name' => (string) $affiliation->textContent,
50
            ];
51
        }
52
    }
53
}
54