1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PubPeerFoundation\PublicationDataExtractor\Models; |
4
|
|
|
|
5
|
|
|
class Authors extends Model |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Hold cherry picked list of authors. |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $list = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Add unknown authors to the current list. |
16
|
|
|
* |
17
|
|
|
* @param array $authors |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
public function add(array $authors): array |
21
|
|
|
{ |
22
|
|
|
if (($count = count($authors)) !== ($listCount = count($this->list))) { |
23
|
|
|
if ($count > $listCount) { |
24
|
|
|
return $this->list = $authors; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $this->list; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
for ($i = 0; $i < $count; $i++) { |
31
|
|
|
$this->addUnknownAttributes($authors, $i); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->list; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add unknown attributes to current author. |
39
|
|
|
* |
40
|
|
|
* @param array $authors |
41
|
|
|
* @param int $counter |
42
|
|
|
*/ |
43
|
|
|
protected function addUnknownAttributes(array $authors, int $counter): void |
44
|
|
|
{ |
45
|
|
|
foreach ($authors[$counter] as $key => $value) { |
46
|
|
|
if (empty($value)) { |
47
|
|
|
continue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($this->attributeShouldBeAdded($counter, $key, $value)) { |
51
|
|
|
$this->list[$counter][$key] = $value; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Should the current attribute be added to the author array? |
58
|
|
|
* |
59
|
|
|
* @param $counter |
60
|
|
|
* @param $key |
61
|
|
|
* @param $value |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
protected function attributeShouldBeAdded($counter, $key, $value) |
65
|
|
|
{ |
66
|
|
|
return $this->foundLongerFirstName($counter, $key, $value) |
67
|
|
|
|| $this->noKnownAttribute($counter, $key); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Is this attribute already known? |
72
|
|
|
* |
73
|
|
|
* @param $counter |
74
|
|
|
* @param $key |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
protected function noKnownAttribute($counter, $key) |
78
|
|
|
{ |
79
|
|
|
return ! isset($this->list[$counter][$key]) |
80
|
|
|
|| empty($this->list[$counter][$key]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Did we find a longer first name? |
85
|
|
|
* |
86
|
|
|
* @param $counter |
87
|
|
|
* @param $key |
88
|
|
|
* @param $value |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
protected function foundLongerFirstName($counter, $key, $value) |
92
|
|
|
{ |
93
|
|
|
return 'first_name' === $key |
94
|
|
|
&& strlen($this->list[$counter][$key]) < strlen($value); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|