|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is a part of HDS (HeBIS Discovery System). HDS is an |
|
4
|
|
|
* extension of the open source library search engine VuFind, that |
|
5
|
|
|
* allows users to search and browse beyond resources. More |
|
6
|
|
|
* Information about VuFind you will find on http://www.vufind.org |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (C) 2016 |
|
9
|
|
|
* HeBIS Verbundzentrale des HeBIS-Verbundes |
|
10
|
|
|
* Goethe-Universität Frankfurt / Goethe University of Frankfurt |
|
11
|
|
|
* http://www.hebis.de |
|
12
|
|
|
* |
|
13
|
|
|
* This program is free software; you can redistribute it and/or |
|
14
|
|
|
* modify it under the terms of the GNU General Public License |
|
15
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
16
|
|
|
* of the License, or (at your option) any later version. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, |
|
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21
|
|
|
* GNU General Public License for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU General Public License |
|
24
|
|
|
* along with this program; if not, write to the Free Software |
|
25
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace Seboettg\CiteProc\Util; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class Number |
|
33
|
|
|
* @package Seboettg\CiteProc\Util |
|
34
|
|
|
* |
|
35
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
class Number |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
const PATTERN_ORDINAL = "/\d+(st|nd|rd|th)?\.?$/"; |
|
41
|
|
|
|
|
42
|
|
|
const PATTERN_ROMAN = "/^[ivxlcdm]+\.?$/i"; |
|
43
|
|
|
|
|
44
|
|
|
const PATTERN_AFFIXES = "/^[a-z]?\d+[a-z]?$/i"; |
|
45
|
|
|
|
|
46
|
|
|
const PATTERN_COMMA_AMPERSAND_RANGE = "/\d*([\s?\-&+,;\s])+\d+/"; |
|
47
|
|
|
|
|
48
|
|
|
const ROMAN_NUMERALS = [ |
|
49
|
|
|
["", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"], |
|
50
|
|
|
["", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"], |
|
51
|
|
|
["", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"], |
|
52
|
|
|
["", "m", "mm", "mmm", "mmmm", "mmmmm"] |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return \Closure |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function getCompareNumber() |
|
59
|
|
|
{ |
|
60
|
|
|
return function ($numA, $numB, $order) { |
|
61
|
|
|
if (is_numeric($numA) && is_numeric($numB)) { |
|
62
|
|
|
$ret = $numA - $numB; |
|
63
|
|
|
} else { |
|
64
|
|
|
$ret = strcmp($numA, $numB); |
|
65
|
|
|
} |
|
66
|
|
|
if ("descending" === $order) { |
|
67
|
|
|
return $ret > 0 ? -1 : 1; |
|
68
|
|
|
} |
|
69
|
|
|
return $ret > 0 ? 1 : -1; |
|
70
|
|
|
}; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $num |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function dec2roman($num) |
|
78
|
|
|
{ |
|
79
|
|
|
$ret = ""; |
|
80
|
|
|
if ($num < 6000) { |
|
81
|
|
|
|
|
82
|
|
|
$numStr = strrev($num); |
|
83
|
|
|
$len = strlen($numStr); |
|
84
|
|
|
for ($pos = 0; $pos < $len; $pos++) { |
|
85
|
|
|
$n = $numStr[$pos]; |
|
86
|
|
|
$ret = self::ROMAN_NUMERALS[$pos][$n] . $ret; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
return $ret; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/* |
|
|
|
|
|
|
93
|
|
|
public static function roman2dec($roman) |
|
94
|
|
|
{ |
|
95
|
|
|
|
|
96
|
|
|
$preDecimal = [1, 10, 100, 1000]; |
|
97
|
|
|
|
|
98
|
|
|
$roman = strtolower($roman); |
|
99
|
|
|
//XVII |
|
100
|
|
|
$ret = 0; |
|
101
|
|
|
for ($i = strlen($roman); $i > 0;) { |
|
102
|
|
|
$numberFound = false; |
|
103
|
|
|
for ($j = 0; $j < $i && $j < strlen($roman);) { |
|
104
|
|
|
$char = substr($roman, $j, ($i - $j)); |
|
105
|
|
|
|
|
106
|
|
|
for ($k = 0; $k < 4; ++$k) { |
|
107
|
|
|
if (($pos = array_search($char, self::ROMAN_NUMERALS[$k])) !== false) { |
|
108
|
|
|
$ret = ($preDecimal[$k] * $pos) + $ret; |
|
109
|
|
|
$i = $j; |
|
110
|
|
|
$j = 0; |
|
111
|
|
|
$numberFound = true; |
|
112
|
|
|
break; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
if (!$numberFound) { |
|
116
|
|
|
++$j; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
if (!$numberFound) { |
|
120
|
|
|
--$i; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
$x = "foo"; |
|
124
|
|
|
return $ret; |
|
125
|
|
|
} |
|
126
|
|
|
*/ |
|
127
|
|
|
|
|
128
|
|
|
public static function roman2Dec($romanNumber) |
|
129
|
|
|
{ |
|
130
|
|
|
|
|
131
|
|
|
$roman = [ |
|
132
|
|
|
"M" => 1000, |
|
133
|
|
|
"D" => 500, |
|
134
|
|
|
"C" => 100, |
|
135
|
|
|
"L" => 50, |
|
136
|
|
|
"X" => 10, |
|
137
|
|
|
"V" => 5, |
|
138
|
|
|
"I" => 1 |
|
139
|
|
|
]; |
|
140
|
|
|
|
|
141
|
|
|
if(is_numeric($romanNumber)) { |
|
142
|
|
|
return 0; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
// Convert the string to an array of roman values: |
|
147
|
|
|
for ($i = 0; $i < strlen($romanNumber); ++$i) { |
|
148
|
|
|
$char = strtoupper($romanNumber{$i}); |
|
149
|
|
|
if (isset($roman[$char])) { |
|
150
|
|
|
$values[] = $roman[$char]; |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$sum = 0; |
|
155
|
|
|
while($current = current($values)) { |
|
|
|
|
|
|
156
|
|
|
$next = next($values); |
|
157
|
|
|
$next > $current ? $sum += $next - $current + 0 * next($values) : $sum += $current; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
// Return the value: |
|
161
|
|
|
return $sum; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.