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

PubmedWebsite::extractAffiliationsData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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