Skip to content

Commit

Permalink
Add preprint information - #21
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier Roussel committed May 7, 2021
1 parent 3c23131 commit 4dddeaa
Show file tree
Hide file tree
Showing 10 changed files with 1,624 additions and 22 deletions.
48 changes: 48 additions & 0 deletions src/Models/Links.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace PubPeerFoundation\PublicationDataExtractor\Models;

class Links extends Model
{
/**
* Hold cherry picked list of links.
*
* @var array
*/
protected $list = [];

/**
* Add unknown links to the current list.
*/
public function add(array $links): array
{
foreach ($links as $link) {
if ($this->hasPreprint($link)) {
$this->list[] = $link;
}
if ($this->isPreprint($link)) {
$this->list[] = $link;
}
}

return $this->list;
}

private function hasPreprint(array $link): bool
{
if (! array_key_exists('has_preprint', $link)) {
return false;
}

return ! in_array(strtolower($link['has_preprint']), $this->knownIdentifierValues('has_preprint'));
}

private function isPreprint(array $link): bool
{
if (! array_key_exists('is_preprint_of', $link)) {
return false;
}

return ! in_array(strtolower($link['is_preprint_of']), $this->knownIdentifierValues('is_preprint_of'));
}
}
3 changes: 0 additions & 3 deletions src/Models/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ class Tags extends Model

/**
* Add unknown tags to the current list.
*
* @param array $tags
* @return array
*/
public function add(array $tags): array
{
Expand Down
24 changes: 22 additions & 2 deletions src/Resources/Extractors/Crossref.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;

use PubPeerFoundation\PublicationDataExtractor\Exceptions\JournalTitleNotFoundException;
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException;
use PubPeerFoundation\PublicationDataExtractor\Exceptions\JournalTitleNotFoundException;

class Crossref extends Extractor implements ProvidesPublicationData, ProvidesIdentifiersData, ProvidesJournalData, ProvidesAuthorsData, ProvidesTagsData
class Crossref extends Extractor implements ProvidesPublicationData, ProvidesIdentifiersData, ProvidesJournalData, ProvidesAuthorsData, ProvidesTagsData, ProvidesLinksData
{
/**
* @throws UnparseableApiException
Expand Down Expand Up @@ -117,6 +117,26 @@ public function extractTagsData(): void
}
}

/**
* Extract and format data needed for the links Relationship
* on the Publication Model.
*/
public function extractLinksData(): void
{
if (array_key_exists('relation', $this->searchTree)) {
foreach (get_array($this->searchTree['relation'], 'has-preprint') as $preprint) {
$this->resourceOutput['links'][] = [
'has_preprint' => $preprint['id'],
];
}
foreach (get_array($this->searchTree['relation'], 'is-preprint-of') as $preprint) {
$this->resourceOutput['links'][] = [
'is_preprint_of' => $preprint['id'],
];
}
}
}

/**
* @param $array
* @return mixed
Expand Down
9 changes: 3 additions & 6 deletions src/Resources/Extractors/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;

use PubPeerFoundation\PublicationDataExtractor\Exceptions\JournalTitleNotFoundException;
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException;
use PubPeerFoundation\PublicationDataExtractor\Output;
use PubPeerFoundation\PublicationDataExtractor\Schema;
use PubPeerFoundation\PublicationDataExtractor\Exceptions\UnparseableApiException;
use PubPeerFoundation\PublicationDataExtractor\Exceptions\JournalTitleNotFoundException;

abstract class Extractor
{
Expand Down Expand Up @@ -36,8 +36,7 @@ abstract class Extractor
/**
* Extractor constructor.
*
* @param mixed $document
* @param Output $output
* @param mixed $document
*/
public function __construct($document, Output $output)
{
Expand All @@ -47,8 +46,6 @@ public function __construct($document, Output $output)

/**
* Dynamically choose what methods to call on each Resource.
*
* @return array
*/
public function extract(): array
{
Expand Down
8 changes: 8 additions & 0 deletions src/Resources/Extractors/ProvidesLinksData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace PubPeerFoundation\PublicationDataExtractor\Resources\Extractors;

interface ProvidesLinksData
{
public function extractLinksData(): void;
}
14 changes: 10 additions & 4 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace PubPeerFoundation\PublicationDataExtractor;

use Volan\ValidatorResult;
use Volan\Volan;
use Volan\ValidatorResult;

class Schema
{
Expand Down Expand Up @@ -87,6 +87,15 @@ class Schema
'_type' => 'string',
],
],
'links' => [
'_type' => 'nested_array',
'has_preprint' => [
'_type' => 'string',
],
'is_preprint_of' => [
'_type' => 'string',
],
],
'affiliations' => [
'_type' => 'nested_array',
'name' => [
Expand Down Expand Up @@ -118,7 +127,6 @@ class Schema
* Validate the data against Schema.
*
* @param $data
* @return \Volan\ValidatorResult
*/
public static function validate($data): ValidatorResult
{
Expand All @@ -130,8 +138,6 @@ public static function validate($data): ValidatorResult

/**
* Get the main data types from Schema.
*
* @return array
*/
public static function getDataTypes(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Support/helpers.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use PubPeerFoundation\PublicationDataExtractor\Support\DateHelper;
use Tightenco\Collect\Support\Arr;
use PubPeerFoundation\PublicationDataExtractor\Support\DateHelper;

if (! function_exists('get_class_name')) {
/**
Expand Down
46 changes: 40 additions & 6 deletions tests/Integration/CrossrefDataExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
namespace PubPeerFoundation\PublicationDataExtractor\Test\Integration;

use PubPeerFoundation\PublicationDataExtractor\Output;
use PubPeerFoundation\PublicationDataExtractor\Resources\Extractors\Crossref;
use PubPeerFoundation\PublicationDataExtractor\Resources\Extractors\CrossrefUpdates;
use PubPeerFoundation\PublicationDataExtractor\Test\TestCase;
use PubPeerFoundation\PublicationDataExtractor\Test\TestHelpers;
use PubPeerFoundation\PublicationDataExtractor\Resources\Extractors\Crossref;
use PubPeerFoundation\PublicationDataExtractor\Resources\Extractors\CrossrefUpdates;

class CrossrefDataExtractorTest extends TestCase
{
use TestHelpers;

/** @test */
public function it_can_extract_publication_data_from_crossref_api()
public function itCanExtractPublicationDataFromCrossrefApi()
{
// Arrange
$file = $this->loadJson('Crossref/valid-article');
Expand All @@ -33,7 +33,7 @@ public function it_can_extract_publication_data_from_crossref_api()
}

/** @test */
public function it_can_extract_identifiers_data_from_crossref_api()
public function itCanExtractIdentifiersDataFromCrossrefApi()
{
// Arrange
$file = $this->loadJson('Crossref/valid-article');
Expand All @@ -59,7 +59,7 @@ public function it_can_extract_identifiers_data_from_crossref_api()
}

/** @test */
public function it_can_extract_authors_from_crossref_api()
public function itCanExtractAuthorsFromCrossrefApi()
{
// Arrange
$file = $this->loadJson('Crossref/valid-with-orcid-article');
Expand Down Expand Up @@ -101,7 +101,7 @@ public function it_can_extract_authors_from_crossref_api()
}

/** @test */
public function it_can_extract_updates_from_crossref_api()
public function itCanExtractUpdatesFromCrossrefApi()
{
// Arrange
$file = $this->loadJson('Crossref/valid-with-updates-article');
Expand All @@ -120,4 +120,38 @@ public function it_can_extract_updates_from_crossref_api()
],
], $extracted['updates']);
}

/** @test */
public function itCanExtractHasPreprintFromCrossrefApi()
{
// Arrange
$file = $this->loadJson('Crossref/valid-with-has-updates-article');

// Act
$extracted = (new Crossref($file, new Output()))->extract();

// Assert
$this->assertArraySubset([
[
'has_preprint' => '10.1101/335703',
],
], $extracted['links']);
}

/** @test */
public function itCanExtractIsPreprintOfFromCrossrefApi()
{
// Arrange
$file = $this->loadJson('Crossref/valid-with-is-preprint-of');

// Act
$extracted = (new Crossref($file, new Output()))->extract();

// Assert
$this->assertArraySubset([
[
'is_preprint_of' => '10.1016/j.neuron.2019.05.022',
],
], $extracted['links']);
}
}
Loading

0 comments on commit 4dddeaa

Please sign in to comment.