Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.31% |
12 / 13 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
Scopus | |
92.31% |
12 / 13 |
|
50.00% |
1 / 2 |
6.02 | |
0.00% |
0 / 1 |
isNameAbbreviation | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
splitNames | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | |
3 | /** |
4 | * Helpers for Scopus CSV import example. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2022. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package CSV |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/ Wiki |
28 | */ |
29 | |
30 | namespace VuFind\CSV\ImportHelper; |
31 | |
32 | use function strlen; |
33 | |
34 | /** |
35 | * Helpers for Scopus CSV import example. |
36 | * |
37 | * @category VuFind |
38 | * @package CSV |
39 | * @author Demian Katz <demian.katz@villanova.edu> |
40 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
41 | * @link https://vufind.org/wiki/ Wiki |
42 | */ |
43 | class Scopus |
44 | { |
45 | /** |
46 | * Is the provided text the abbreviation part of a name string? |
47 | * |
48 | * @param string $text Text to check |
49 | * |
50 | * @return bool |
51 | */ |
52 | protected static function isNameAbbreviation(string $text): bool |
53 | { |
54 | // A single character is very likely an abbreviation: |
55 | if (strlen($text) === 1) { |
56 | return true; |
57 | } |
58 | // A set of initials, possibly hyphen or space separated, is very likely |
59 | // an abbreviation: |
60 | return preg_match('/^(.\.[- ]*)+$/', $text); |
61 | } |
62 | |
63 | /** |
64 | * Given a string of multiple last name/initial pairs, split it into an array |
65 | * of name strings. |
66 | * |
67 | * @param string $names Names to split |
68 | * @param bool $firstOnly Set to true to return just the first extracted value |
69 | * |
70 | * @return string[] |
71 | */ |
72 | public static function splitNames(string $names, bool $firstOnly = false): array |
73 | { |
74 | $parts = explode(', ', $names); |
75 | $result = []; |
76 | while (!empty($parts)) { |
77 | $next = array_shift($parts); |
78 | // Look ahead: if the text element is a set of initials, it's part of |
79 | // the current name. |
80 | if (static::isNameAbbreviation($parts[0] ?? '')) { |
81 | $next .= ', ' . array_shift($parts); |
82 | } |
83 | $result[] = $next; |
84 | if ($firstOnly) { |
85 | return $result; |
86 | } |
87 | } |
88 | return $result; |
89 | } |
90 | } |