IsNumeric   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 41
rs 10
wmc 7

2 Methods

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