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