-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete DataMerger and introduce Output objects + models
- Loading branch information
Showing
19 changed files
with
647 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.