Arxiv::extractPublicationData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
c 0
b 0
f 0
rs 10
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;
4
5
class Arxiv extends Extractor implements ProvidesPublicationData, ProvidesIdentifiersData, ProvidesAuthorsData, ProvidesJournalData, ProvidesTypesData
6
{
7
    /**
8
     * Create search tree.
9
     */
10
    protected function fillSearchTree(): void
11
    {
12
        $this->searchTree = $this->document->entry;
13
    }
14
15 4
    /**
16
     * Extract and format data needed for the Publication Model.
17 4
     */
18 4
    public function extractPublicationData(): void
19
    {
20 4
        $this->resourceOutput['publication'] = [
21
            'title' => get_string($this->searchTree, 'title'),
22 4
            'abstract' => get_string($this->searchTree, 'summary'),
23
            'url' => get_string($this->searchTree, 'id'),
24 4
            'published_at' => date_from_parseable_format(get_string($this->searchTree, 'published')),
25 4
        ];
26 4
    }
27 4
28 4
    /**
29
     * Extract and format data needed for the Identifiers Relationship
30 4
     * on the Publication Model.
31
     */
32
    public function extractIdentifiersData(): void
33 4
    {
34
        $this->resourceOutput['identifiers'][] = [
35 4
            'value' => (string) $this->getIdentifier(),
36 4
            'type' => 'arxiv',
37
        ];
38
39
        $this->resourceOutput['identifiers'][] = [
40
            'value' => '2331-8422',
41 4
            'type' => 'issn',
42
        ];
43 4
44 4
        $namespace = $this->searchTree->getNamespaces(true);
45 4
        foreach ($this->searchTree->children($namespace['arxiv']) as $key => $child) {
46 4
            if ('doi' === $key) {
47 4
                $this->resourceOutput['identifiers'][] = [
48
                    'value' => (string) $child,
49 4
                    'type' => 'doi',
50
                ];
51
            }
52
        }
53
    }
54
55 4
    /**
56
     * Extract and format data needed for the Journals Relationship
57 4
     * on the Publication Model.
58 4
     */
59 4
    public function extractJournalData(): void
60
    {
61
        $this->resourceOutput['journal'] = [
62 4
            'title' => 'arXiv',
63
            'issn' => ['2331-8422'],
64
        ];
65
    }
66 4
67
    /**
68
     * Extract and format data needed for the Authors Relationship
69
     * on the Publication Model.
70
     */
71
    public function extractAuthorsData(): void
72 4
    {
73
        foreach ($this->searchTree->author as $author) {
74 4
            $name = explode(' ', $author->name, 2);
75
            $this->resourceOutput['authors'][] = [
76
                'first_name' => $name[0] ?? null,
77
                'last_name' => $name[1] ?? null,
78 4
            ];
79
        }
80
    }
81
82
    /**
83
     * Extract and format data needed for the Types Relationship
84 4
     * on the Publication Model.
85
     */
86 4
    public function extractTypesData(): void
87 4
    {
88 4
        $this->resourceOutput['types'][] = [
89 4
            'name' => 'arxiv',
90 4
        ];
91
    }
92
93 4
    /**
94
     * Get the Identifier from the data.
95
     *
96
     * @return string
97
     */
98
    protected function getIdentifier(): string
99 4
    {
100
        preg_match('/(\d{2}(0|1)[0-9]\.\d{4,5})(v|V)?(\d)?/', $this->searchTree->id[0], $matches);
101 4
102
        return $matches[1];
103
    }
104
}
105