Skip to content

Commit

Permalink
Delete DataMerger and introduce Output objects + models
Browse files Browse the repository at this point in the history
  • Loading branch information
XavRsl committed Jun 21, 2018
1 parent c3bafba commit 29fa5ff
Show file tree
Hide file tree
Showing 19 changed files with 647 additions and 261 deletions.
5 changes: 5 additions & 0 deletions src/ApiDataChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ public static function check($data)

return $result;
}

public static function getDataTypes()
{
return array_slice(array_keys(ApiDataChecker::SCHEMA['root']), 1);
}
}
5 changes: 2 additions & 3 deletions src/ApiDataFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use GrahamCampbell\GuzzleFactory\GuzzleFactory;
use PubPeerFoundation\PublicationDataExtractor\Models\Output;
use PubPeerFoundation\PublicationDataExtractor\Resources\Resource;
use PubPeerFoundation\PublicationDataExtractor\Identifiers\Identifier;

Expand Down Expand Up @@ -106,9 +107,7 @@ public function getData(): array

$this->fetchComplementaryData();

return array_values(
array_filter($this->apiData)
);
return Output::getInstance()->format();
}

/**
Expand Down
31 changes: 0 additions & 31 deletions src/ApiDataMerger.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Identifiers/Doi.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Doi extends Identifier
protected $resources = [
\PubPeerFoundation\PublicationDataExtractor\Resources\Doi::class,
\PubPeerFoundation\PublicationDataExtractor\Resources\Crossref::class,
\PubPeerFoundation\PublicationDataExtractor\Resources\PubmedWebsite::class,
// \PubPeerFoundation\PublicationDataExtractor\Resources\PubmedWebsite::class,
\PubPeerFoundation\PublicationDataExtractor\Resources\IdConverter::class,
\PubPeerFoundation\PublicationDataExtractor\Resources\EutilsEsearch::class,
];
Expand Down
30 changes: 30 additions & 0 deletions src/Models/Affiliations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace PubPeerFoundation\PublicationDataExtractor\Models;

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

/**
* Add unknown affiliations to the current list.
*
* @param $affiliations
* @return array
*/
public function add(array $affiliations): array
{
foreach ($affiliations as $affiliation) {
if (! in_array(strtolower($affiliation['name']), $this->knownIdentifierValues('name'))) {
$this->list[] = $affiliation;
}
}

return $this->list;
}
}
48 changes: 48 additions & 0 deletions src/Models/Authors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace PubPeerFoundation\PublicationDataExtractor\Models;

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

/**
* Add unknown authors to the current list.
*
* @param $authors
* @return array
*/
public function add(array $authors): array
{
if (($count = count($authors)) !== ($listCount = count($this->list))) {
if ($count > $listCount) {
return $this->list = $authors;
}

return $this->list;
}

for ($i = 0; $i < $count; ++$i) {
$this->addUnknownAttributes($authors, $i);
}

return $this->list;
}

protected function addUnknownAttributes($authors, $i)
{
foreach ($authors[$i] as $key => $value) {
if (empty($value)) {
continue;
}
if (isset($this->list[$i][$key]) && ! empty($this->list[$i][$key])) {
$this->list[$i][$key] = $value;
}
}
}
}
30 changes: 30 additions & 0 deletions src/Models/Identifiers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace PubPeerFoundation\PublicationDataExtractor\Models;

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

/**
* Add unknown identifiers to the current list.
*
* @param $identifiers
* @return array
*/
public function add(array $identifiers): array
{
foreach ($identifiers as $identifier) {
if (! in_array(strtolower($identifier['value']), $this->knownIdentifierValues('value'))) {
$this->list[] = $identifier;
}
}

return $this->list;
}
}
30 changes: 30 additions & 0 deletions src/Models/Journal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace PubPeerFoundation\PublicationDataExtractor\Models;

class Journal extends Model
{
/**
* Hold the journal details.
*
* @var array
*/
protected $list = [];

/**
* Add unknown attributes to the current details array.
*
* @param $journal
* @return array
*/
public function add(array $journal): array
{
foreach ($journal as $key => $value) {
if ($this->shouldKeepAttribute($key, $value)) {
$this->list[$key] = $value;
}
}

return $this->list;
}
}
90 changes: 90 additions & 0 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace PubPeerFoundation\PublicationDataExtractor\Models;

use Exception;
use Tightenco\Collect\Support\Arr;

class Model
{
/**
* This var should never be used, in favor of Model's internal var.
*
* @var array
*/
protected $list = [];

/**
* List of statically created instances.
*
* @var array
*/
private static $instances = [];

protected function __construct()
{
}

protected function __clone()
{
}

/**
* @throws Exception
*/
public function __wakeup()
{
throw new Exception('Cannot unserialize singleton');
}

/**
* Return the current Model's instance.
*
* @return Model
*/
public static function getInstance()
{
$cls = get_called_class();
if (! isset(self::$instances[$cls])) {
self::$instances[$cls] = new static();
}

return self::$instances[$cls];
}

/**
* Figure out which values are already known.
*
* @param $key
* @return array
*/
protected function knownIdentifierValues($key): array
{
return array_map(function ($value) {
return strtolower($value);
}, Arr::pluck($this->list, $key));
}

/**
* Should the attribute be kept?
*
* @param $key
* @param $value
* @return bool
*/
protected function shouldKeepAttribute($key, $value): bool
{
$function = is_array($value) ? 'count' : 'strlen';

if (! isset($this->list[$key])) {
return true;
}

return $function($this->list[$key]) > $function($value);
}

protected function reset()
{
$this->list = [];
}
}
51 changes: 51 additions & 0 deletions src/Models/Output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace PubPeerFoundation\PublicationDataExtractor\Models;

use PubPeerFoundation\PublicationDataExtractor\ApiDataChecker;

class Output extends Model
{
/**
* Global content array.
*
* @var array
*/
protected $content = [];

/**
* Dynamically call the add method on the desired model.
*
* @param string $name
* @param array $resourceData
*/
public function __call($name, array $resourceData): void
{
$name = strtolower(substr($name, 3));

if (in_array($name, ApiDataChecker::getDataTypes())) {
$className = __NAMESPACE__.'\\'.ucfirst($name);
$this->content[$name] = $className::getInstance()->add($resourceData[0]);
}
}

/**
* Output the formatted content array.
*
* @return array
*/
public function format()
{
$this->resetLists();

return $this->content;
}

protected function resetLists()
{
foreach (ApiDataChecker::getDataTypes() as $type) {
$className = __NAMESPACE__.'\\'.ucfirst($type);
$className::getInstance()->reset();
}
}
}
30 changes: 30 additions & 0 deletions src/Models/Publication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace PubPeerFoundation\PublicationDataExtractor\Models;

class Publication extends Model
{
/**
* Hold the publication details.
*
* @var array
*/
protected $list = [];

/**
* Add unknown attributes to the current details array.
*
* @param $publication
* @return array
*/
public function add(array $publication): array
{
foreach ($publication as $key => $value) {
if ($this->shouldKeepAttribute($key, $value)) {
$this->list[$key] = $value;
}
}

return $this->list;
}
}
Loading

0 comments on commit 29fa5ff

Please sign in to comment.