Completed
Push — master ( e87eab...888d29 )
by Sebastian
03:03
created

IsNumeric::parseValue()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Seboettg\CiteProc\Constraint;
4
use NumberFormatter;
5
use Seboettg\CiteProc\CiteProc;
6
use Seboettg\CiteProc\Context;
7
use Seboettg\CiteProc\Util\Number;
8
9
10
/**
11
 * Class IsNumeric
12
 * @package Seboettg\CiteProc\Node\Choose\Constraint
13
 *
14
 * @author Sebastian Böttger <[email protected]>
15
 */
16
class IsNumeric implements ConstraintInterface
17
{
18
19
    private $isNumeric;
20
21
    private $match;
22
23
    public function __construct($value, $match)
24
    {
25
        $this->isNumeric = $value;
26
        $this->match = $match;
27
    }
28
29
    public function validate($value)
30
    {
31
        if (isset($value->{$this->isNumeric})) {
32
            return $this->parseValue($value->{$this->isNumeric});
33
        }
34
35
        return false;
36
    }
37
38
    /**
39
     * Tests whether the given variables (Appendix IV - Variables) contain numeric content. Content is considered
40
     * numeric if it solely consists of numbers. Numbers may have prefixes and suffixes (“D2”, “2b”, “L2d”), and may be
41
     * separated by a comma, hyphen, or ampersand, with or without spaces (“2, 3”, “2-4”, “2 & 4”). For example, “2nd”
42
     * tests “true” whereas “second” and “2nd edition” test “false”.
43
     *
44
     * @param $evalValue
45
     * @return bool
46
     */
47
    private function parseValue($evalValue)
48
    {
49
        if (is_numeric($evalValue)) {
50
            return true;
51
        }
52
        else if (preg_match(Number::PATTERN_ORDINAL, $evalValue)) {
53
            $numberFormatter = new NumberFormatter(CiteProc::getContext()->getLocale()->getLanguage(), NumberFormatter::ORDINAL);
54
            return $numberFormatter->parse($evalValue) !== false;
55
        }
56
        else if (preg_match(Number::PATTERN_ROMAN, $evalValue)) {
57
            return Number::roman2Dec($evalValue) !== false;
58
        }
59
        else if (preg_match(Number::PATTERN_COMMA_AMPERSAND_RANGE, $evalValue)){
60
            return true;
61
        }
62
        return false;
63
    }
64
}