1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* citeproc-php |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/seboettg/citeproc-php for the source repository |
6
|
|
|
* @copyright Copyright (c) 2016 Sebastian Böttger. |
7
|
|
|
* @license https://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Seboettg\CiteProc\Constraint; |
11
|
|
|
|
12
|
|
|
use NumberFormatter; |
13
|
|
|
use Seboettg\CiteProc\CiteProc; |
14
|
|
|
use Seboettg\CiteProc\Util\NumberHelper; |
15
|
|
|
use stdClass; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class IsNumeric |
19
|
|
|
* @package Seboettg\CiteProc\Choose\Constraint |
20
|
|
|
* |
21
|
|
|
* @author Sebastian Böttger <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
/** @noinspection PhpUnused */ |
24
|
|
|
class IsNumeric extends AbstractConstraint |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $variable |
29
|
|
|
* @param stdClass $data |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
16 |
|
protected function matchForVariable($variable, $data) |
33
|
|
|
{ |
34
|
16 |
|
if (isset($data->{$variable})) { |
35
|
5 |
|
return $this->parseValue($data->{$variable}); |
36
|
|
|
} |
37
|
13 |
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Tests whether the given variables (Appendix IV - Variables) contain numeric content. Content is considered |
42
|
|
|
* numeric if it solely consists of numbers. Numbers may have prefixes and suffixes (“D2”, “2b”, “L2d”), and may be |
43
|
|
|
* separated by a comma, hyphen, or ampersand, with or without spaces (“2, 3”, “2-4”, “2 & 4”). For example, “2nd” |
44
|
|
|
* tests “true” whereas “second” and “2nd edition” test “false”. |
45
|
|
|
* |
46
|
|
|
* @param $evalValue |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
5 |
|
private function parseValue($evalValue) |
50
|
|
|
{ |
51
|
5 |
|
if (is_numeric($evalValue)) { |
52
|
3 |
|
return true; |
53
|
4 |
|
} elseif (preg_match(NumberHelper::PATTERN_ORDINAL, $evalValue)) { |
54
|
|
|
$numberFormatter = new NumberFormatter( |
55
|
|
|
CiteProc::getContext()->getLocale()->getLanguage(), |
56
|
|
|
NumberFormatter::ORDINAL |
57
|
|
|
); |
58
|
|
|
return $numberFormatter->parse($evalValue) !== false; |
59
|
4 |
|
} elseif (preg_match(NumberHelper::PATTERN_ROMAN, $evalValue)) { |
60
|
1 |
|
return NumberHelper::roman2Dec($evalValue) !== false; |
61
|
4 |
|
} elseif (preg_match(NumberHelper::PATTERN_COMMA_AMPERSAND_RANGE, $evalValue)) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
4 |
|
return false; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|