Passed
Branch master (ee1eb5)
by Sebastian
08:07 queued 04:25
created

IsNumeric::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
16
17
/**
18
 * Class IsNumeric
19
 * @package Seboettg\CiteProc\Choose\Constraint
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
20
 *
21
 * @author Sebastian Böttger <[email protected]>
22
 */
3 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
23
class IsNumeric implements ConstraintInterface
24
{
25
26
    private $isNumeric;
0 ignored issues
show
Coding Style introduced by
Private member variable "isNumeric" must be prefixed with an underscore
Loading history...
27
28 28
    public function __construct($value)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
29
    {
30 28
        $this->isNumeric = $value;
31 28
    }
32
33
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
34
     * @param $value
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
35
     * @param int|null $citationNumber
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
36
     * @return bool
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
37
     */
38 12
    public function validate($value, $citationNumber = null)
39
    {
40 12
        if (isset($value->{$this->isNumeric})) {
41 2
            return $this->parseValue($value->{$this->isNumeric});
42
        }
43
44 11
        return false;
45
    }
46
47
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $evalValue should have a doc-comment as per coding-style.
Loading history...
48
     * Tests whether the given variables (Appendix IV - Variables) contain numeric content. Content is considered
49
     * numeric if it solely consists of numbers. Numbers may have prefixes and suffixes (“D2”, “2b”, “L2d”), and may be
50
     * separated by a comma, hyphen, or ampersand, with or without spaces (“2, 3”, “2-4”, “2 & 4”). For example, “2nd”
51
     * tests “true” whereas “second” and “2nd edition” test “false”.
52
     *
53
     * @param $evalValue
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
54
     * @return bool
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
55
     */
56 2
    private function parseValue($evalValue)
1 ignored issue
show
Coding Style introduced by
Private method name "IsNumeric::parseValue" must be prefixed with an underscore
Loading history...
57
    {
58 2
        if (is_numeric($evalValue)) {
59 1
            return true;
60 2
        } else if (preg_match(NumberHelper::PATTERN_ORDINAL, $evalValue)) {
61
            $numberFormatter = new NumberFormatter(CiteProc::getContext()->getLocale()->getLanguage(), NumberFormatter::ORDINAL);
62
            return $numberFormatter->parse($evalValue) !== false;
63 2
        } else if (preg_match(NumberHelper::PATTERN_ROMAN, $evalValue)) {
64
            return NumberHelper::roman2Dec($evalValue) !== false;
65 2
        } else if (preg_match(NumberHelper::PATTERN_COMMA_AMPERSAND_RANGE, $evalValue)) {
66
            return true;
67
        }
68 2
        return false;
69
    }
70
}