1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* citeproc-php |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/seboettg/citeproc-php for the source repository |
6
|
|
|
* @copyright Copyright (c) 2017 Sebastian Böttger. |
7
|
|
|
* @license https://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Seboettg\CiteProc\Util; |
11
|
|
|
|
12
|
|
|
use Seboettg\CiteProc\CiteProc; |
13
|
|
|
use Seboettg\CiteProc\Exception\CiteProcException; |
14
|
|
|
use stdClass; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class NameHelper |
18
|
|
|
* @package Seboettg\CiteProc\Util |
19
|
|
|
* @author Sebastian Böttger <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class NameHelper |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param stdClass $precedingItem |
26
|
|
|
* @param array $currentAuthor |
27
|
|
|
* @return bool |
28
|
|
|
* @throws CiteProcException |
29
|
|
|
*/ |
30
|
6 |
|
public static function identicalAuthors($precedingItem, $currentAuthor) |
31
|
|
|
{ |
32
|
6 |
|
if (!property_exists($precedingItem, "author")) { |
33
|
1 |
|
throw new CiteProcException("No author to present"); |
34
|
|
|
} |
35
|
|
|
|
36
|
5 |
|
if (count($precedingItem->author) !== count($currentAuthor)) { |
37
|
4 |
|
return false; |
38
|
|
|
} |
39
|
3 |
|
foreach ($currentAuthor as $current) { |
40
|
3 |
|
if (self::precedingHasAuthor($precedingItem, $current)) { |
41
|
3 |
|
continue; |
42
|
|
|
} |
43
|
1 |
|
return false; |
44
|
|
|
} |
45
|
3 |
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param stdClass $preceding |
50
|
|
|
* @param stdClass $name |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
4 |
|
public static function precedingHasAuthor($preceding, $name) |
54
|
|
|
{ |
55
|
4 |
|
foreach ($preceding->author as $author) { |
56
|
4 |
|
if ($author->family === $name->family && $author->given === $name->given) { |
57
|
4 |
|
return true; |
58
|
|
|
} |
59
|
|
|
} |
60
|
2 |
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* removes the field $particle from $data and appends its content to the $namePart field of $data |
66
|
|
|
* @param stdClass $data |
67
|
|
|
* @param string $namePart |
68
|
|
|
* @param string $particle |
69
|
|
|
*/ |
70
|
79 |
|
public static function appendParticleTo(&$data, $namePart, $particle) |
71
|
|
|
{ |
72
|
79 |
|
if (isset($data->{$particle}) && isset($data->{$namePart})) { |
73
|
9 |
|
$data->{$namePart} = $data->{$namePart} . " " . $data->{$particle}; // append $particle to $namePart |
74
|
9 |
|
unset($data->{$particle}); //remove particle from $data |
75
|
|
|
} |
76
|
79 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* removes the field $particle from $data and prepends its content to the $namePart field of $data |
80
|
|
|
* @param stdClass $data |
81
|
|
|
* @param string $namePart ("given"|"family") |
82
|
|
|
* @param string $particle |
83
|
|
|
*/ |
84
|
88 |
|
public static function prependParticleTo(&$data, $namePart, $particle) |
85
|
|
|
{ |
86
|
88 |
|
if (isset($data->{$particle}) && isset($data->{$namePart})) { |
87
|
7 |
|
$data->{$namePart} = $data->{$particle} . " " . $data->{$namePart}; //prepend $particle to $namePart |
88
|
7 |
|
unset($data->{$particle}); //remove particle from $data |
89
|
|
|
} |
90
|
88 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $persons1 |
94
|
|
|
* @param array $persons2 |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
2 |
|
public static function sameNames($persons1, $persons2) |
98
|
|
|
{ |
99
|
2 |
|
$same = count($persons1) === count($persons2); |
100
|
|
|
|
101
|
2 |
|
if (!$same) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
array_walk($persons1, function ($name, $key) use ($persons2, &$same) { |
106
|
2 |
|
$family1 = $name->family; |
107
|
2 |
|
$family2 = $persons2[$key]->family; |
108
|
2 |
|
$same = $same && ($family1 === $family2); |
109
|
2 |
|
}); |
110
|
|
|
|
111
|
2 |
|
return (bool) $same; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $data |
116
|
|
|
* @return string |
117
|
|
|
* @throws CiteProcException |
118
|
|
|
*/ |
119
|
97 |
|
public static function normalizeName($data) |
120
|
|
|
{ |
121
|
97 |
|
if (empty($data->family)) { |
122
|
|
|
throw new CiteProcException("Illegal argument. Name has no family name."); |
123
|
|
|
} |
124
|
97 |
|
return $data->family . (isset($data->given) ? $data->given : ""); |
125
|
|
|
} |
126
|
|
|
|
127
|
98 |
|
public static function addExtendedMarkup($nameVar, $nameItem, $formattedName) |
128
|
|
|
{ |
129
|
98 |
|
$markupExtension = CiteProc::getContext()->getMarkupExtension(); |
130
|
98 |
|
if (array_key_exists($nameVar, $markupExtension)) { |
131
|
1 |
|
$function = $markupExtension[$nameVar]; |
132
|
1 |
|
if (is_callable($function)) { |
133
|
1 |
|
return $function($nameItem, $formattedName); |
134
|
|
|
} |
135
|
97 |
|
} elseif (array_key_exists($mode = CiteProc::getContext()->getMode(), $markupExtension)) { |
136
|
2 |
|
if (array_key_exists($nameVar, $markupExtension[$mode])) { |
137
|
1 |
|
$function = $markupExtension[$mode][$nameVar]; |
138
|
1 |
|
if (is_callable($function)) { |
139
|
1 |
|
return $function($nameItem, $formattedName); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
97 |
|
return $formattedName; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|