Passed
Push — master ( f36a08...6258cd )
by Sebastian
12:50
created

IsNumeric   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 41
ccs 12
cts 17
cp 0.7059
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A matchForVariable() 0 6 2
A parseValue() 0 16 5
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