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