Completed
Push — master ( 35f9c1...7f1afc )
by Xavier
01:04
created

IdConverter::extract()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException;
6
7
class IdConverter implements Extractor, ProvidesIdentifiersData
8
{
9
    protected $document;
10
11
    protected $searchTree;
12
13
    protected $typeMap = [
14
        'pmcid' => 'pmc',
15
        'pmid' => 'pubmed',
16
        'doi' => 'doi'
17
    ];
18
19
    protected $output;
20
21
    public function __construct($document)
22
    {
23
        $this->document = $document;
24
    }
25
26
    public function extract(): array
27
    {
28
        $this->getDataFromDocument();
29
        $this->extractIdentifiersData();
30
31
        return $this->output;
32
    }
33
34
    protected function getDataFromDocument()
35
    {
36
        if ('ok' !== $this->document['status']) {
37
            throw new UnparseableApiException();
38
        }
39
40
        $this->searchTree = $this->document['records'][0];
41
    }
42
43
    public function extractIdentifiersData()
44
    {
45
        foreach ($this->searchTree as $type => $identifier) {
46
            if(array_key_exists($type, $this->typeMap)) {
47
                $this->output['identifiers'][] = [
48
                    'value' => $identifier,
49
                    'type' => $this->typeMap[$type],
50
                ];
51
            }
52
        }
53
    }
54
}
55