1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors; |
4
|
|
|
|
5
|
|
|
use PubPeerFoundation\PublicationDataExtractor\Helpers\DateHelper; |
6
|
|
|
use PubPeerFoundation\PublicationDataExtractor\Helpers\Helpers; |
7
|
|
|
use PubPeerFoundation\PublicationDataExtractor\Helpers\UpdateTypesStandardiser; |
8
|
|
|
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException; |
9
|
|
|
use PubPeerFoundation\PublicationDataExtractor\Exceptions\JournalTitleNotFoundException; |
10
|
|
|
|
11
|
|
|
class Crossref implements Extractor, ProvidesPublicationData, ProvidesIdentifiersData, ProvidesJournalData, ProvidesAuthorsData, ProvidesUpdatesData |
12
|
|
|
{ |
13
|
|
|
protected $document; |
14
|
|
|
|
15
|
|
|
protected $searchTree; |
16
|
|
|
|
17
|
24 |
|
protected $output = []; |
18
|
|
|
|
19
|
24 |
|
public function __construct($document) |
20
|
24 |
|
{ |
21
|
|
|
$this->document = $document; |
22
|
11 |
|
} |
23
|
|
|
|
24
|
11 |
|
public function extract(): array |
25
|
|
|
{ |
26
|
9 |
|
$this->getDataFromDocument(); |
27
|
9 |
|
|
28
|
9 |
|
$this->extractAuthorsData(); |
29
|
9 |
|
$this->extractIdentifiersData(); |
30
|
9 |
|
try { |
31
|
9 |
|
$this->extractJournalData(); |
32
|
9 |
|
} catch (JournalTitleNotFoundException $e) { |
33
|
|
|
$this->searchTree['container-title'] = $this->searchTree['publisher']; |
34
|
9 |
|
} |
35
|
|
|
$this->extractPublicationData(); |
36
|
|
|
$this->extractTagsData(); |
37
|
7 |
|
$this->extractTypesData(); |
38
|
|
|
$this->extractUpdatesData(); |
39
|
7 |
|
|
40
|
2 |
|
return $this->output; |
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
protected function getDataFromDocument() |
44
|
5 |
|
{ |
45
|
|
|
if ('ok' !== $this->document['status']) { |
46
|
|
|
throw new UnparseableApiException(); |
47
|
|
|
} |
48
|
|
|
|
49
|
11 |
|
$this->searchTree = $this->document['message']; |
50
|
|
|
} |
51
|
11 |
|
|
52
|
|
|
/** |
53
|
11 |
|
* Extract and format data needed for the Publication Model. |
54
|
11 |
|
*/ |
55
|
11 |
|
public function extractPublicationData() |
56
|
11 |
|
{ |
57
|
11 |
|
$date = $this->extractDateFrom(['published-print', 'published-online', 'issued']); |
58
|
|
|
|
59
|
11 |
|
$this->output['publication'] = [ |
60
|
|
|
'title' => Helpers::flatten($this->searchTree['title'] ?? null) , |
61
|
|
|
'abstract' => Helpers::flatten($this->searchTree['abstract'] ?? null), |
62
|
|
|
'url' => $this->searchTree['URL'] ?? null, |
63
|
|
|
'published_at' => $date, |
64
|
11 |
|
]; |
65
|
|
|
} |
66
|
11 |
|
|
67
|
|
|
/** |
68
|
|
|
* Extract and format data needed for the Identifiers Relationship |
69
|
|
|
* on the Publication Model. |
70
|
|
|
*/ |
71
|
|
|
public function extractIdentifiersData() |
72
|
|
|
{ |
73
|
17 |
|
if (! empty($this->searchTree['DOI'])) { |
74
|
|
|
$this->output['identifiers'][] = [ |
75
|
17 |
|
'value' => $this->searchTree['DOI'], |
76
|
9 |
|
'type' => 'doi', |
77
|
9 |
|
]; |
78
|
9 |
|
} |
79
|
|
|
|
80
|
|
|
foreach ($this->getIssnList() as $issn) { |
81
|
|
|
$this->output['identifiers'][] = [ |
82
|
17 |
|
'value' => $issn, |
83
|
12 |
|
'type' => 'issn', |
84
|
11 |
|
]; |
85
|
11 |
|
} |
86
|
11 |
|
} |
87
|
11 |
|
|
88
|
|
|
/** |
89
|
|
|
* Extract and format data needed for the Journal Relationship |
90
|
|
|
* on the Publication Model. |
91
|
1 |
|
*/ |
92
|
1 |
|
public function extractJournalData() |
93
|
1 |
|
{ |
94
|
|
|
if (empty($this->searchTree['container-title']) && empty($this->searchTree['ISSN'])) { |
95
|
|
|
throw new JournalTitleNotFoundException(); |
96
|
|
|
} |
97
|
17 |
|
|
98
|
|
|
$this->output['journal'] = [ |
99
|
|
|
'title' => Helpers::flatten($this->searchTree['container-title'] ?? null), |
100
|
|
|
'issn' => $this->getIssnList() ?? null, |
101
|
|
|
'publisher' => Helpers::flatten($this->searchTree['publisher'] ?? null), |
102
|
|
|
]; |
103
|
12 |
|
} |
104
|
|
|
|
105
|
12 |
|
/** |
106
|
1 |
|
* Extract and format data needed for the Authors Relationship |
107
|
|
|
* on the Publication Model. |
108
|
|
|
*/ |
109
|
12 |
|
public function extractAuthorsData() |
110
|
12 |
|
{ |
111
|
12 |
|
if (array_key_exists('author', $this->searchTree)) { |
112
|
12 |
|
foreach ($this->searchTree['author'] as $author) { |
113
|
|
|
if (isset($author['family'])) { |
114
|
12 |
|
$this->output['authors'][] = [ |
115
|
|
|
'first_name' => $author['given'] ?? null, |
116
|
|
|
'last_name' => $author['family'] ?? null, |
117
|
|
|
'orcid' => $author['ORCID'] ?? null, |
118
|
|
|
'affiliation' => $author['affiliation'] ?? null, |
119
|
|
|
]; |
120
|
9 |
|
} |
121
|
|
|
} |
122
|
9 |
|
} |
123
|
9 |
|
} |
124
|
9 |
|
|
125
|
9 |
|
/** |
126
|
9 |
|
* Extract and format data needed for the Types Relationship |
127
|
9 |
|
* on the Publication Model. |
128
|
9 |
|
*/ |
129
|
9 |
|
public function extractTypesData() |
130
|
|
|
{ |
131
|
|
|
$this->output['types'][] = [ |
132
|
|
|
'name' => $this->searchTree['type'] ?? null, |
133
|
|
|
]; |
134
|
9 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Extract and format data needed for the tags Relationship |
138
|
|
|
* on the Publication Model. |
139
|
|
|
*/ |
140
|
9 |
|
public function extractTagsData() |
141
|
|
|
{ |
142
|
9 |
|
if (array_key_exists('subject', $this->searchTree)) { |
143
|
9 |
|
foreach ($this->searchTree['subject'] as $tag) { |
144
|
|
|
$this->output['tags'][] = [ |
145
|
9 |
|
'name' => $tag, |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
9 |
|
protected function extractDateFrom($array) |
152
|
|
|
{ |
153
|
9 |
|
$datePartsContainer = array_values(array_filter($array, function ($string) { |
154
|
9 |
|
return isset($this->searchTree[$string]); |
155
|
9 |
|
}))[0]; |
156
|
9 |
|
|
157
|
|
|
return (new DateHelper()) |
158
|
|
|
->dateFromDateParts($this->searchTree[$datePartsContainer]['date-parts'][0]); |
159
|
|
|
} |
160
|
9 |
|
|
161
|
|
|
public function extractUpdatesData() |
162
|
|
|
{ |
163
|
|
|
if (array_key_exists('update-to', $this->searchTree)) { |
164
|
11 |
|
foreach ($this->searchTree['update-to'] as $update) { |
165
|
11 |
|
$this->output['updates'][] = [ |
166
|
11 |
|
'timestamp' => $update['updated']['timestamp'], |
167
|
|
|
'identifier' => [ |
168
|
11 |
|
'doi' => $update['DOI'], |
169
|
11 |
|
], |
170
|
|
|
'type' => UpdateTypesStandardiser::getType($update['type']), |
171
|
|
|
]; |
172
|
9 |
|
} |
173
|
|
|
} |
174
|
9 |
|
} |
175
|
2 |
|
|
176
|
2 |
|
protected function getIssnList() |
177
|
2 |
|
{ |
178
|
|
|
if (! empty($this->searchTree['ISSN'])) { |
179
|
2 |
|
return (is_array($this->searchTree['ISSN'])) |
180
|
|
|
? $this->searchTree['ISSN'] |
181
|
2 |
|
: [$this->searchTree['ISSN']]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return []; |
185
|
9 |
|
} |
186
|
|
|
} |
187
|
|
|
|