Passed
Push — master ( 163405...503637 )
by George
02:38
created

NumberValidator::formatCurrency()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2.032
1
<?php
2
namespace JsonTable\Validate\Format;
3
4
use \JsonTable\Validate\AbstractFormatValidator;
5
6
/**
7
 * Lexical number validator.
8
 *
9
 * @package JSON table
10
 */
11
class NumberValidator extends AbstractFormatValidator
12
{
13
    /**
14
     * Validate that the input is a valid number (float).
15
     *
16
     * @access  protected
17
     *
18
     * @return  boolean Whether the input is valid.
19
     */
20 1
    protected function formatDefault()
21
    {
22 1
        return (false !== filter_var($this->input, FILTER_VALIDATE_FLOAT));
23
    }
24
25
26
    /**
27
     * Validate that the input is a valid currency.
28
     *
29
     * @access  protected
30
     *
31
     * @return  boolean Whether the input is valid.
32
     */
33 1
    protected function formatCurrency()
34
    {
35
        // Remove any non-digits from the input.
36
        //TODO: Validate that any non-digits are valid currency characters.
37 1
        $input = preg_filter('/^\D./', '', $this->input);
38
39 1
        if (empty($input)) {
40
            return true;
41
        }
42
43
        // Check that the remainder of the input matches an expected currency format.
44
        // This regex is provided by Tim Pietzcker here: http://stackoverflow.com/a/4983648.
45 1
        return (true == preg_match('/\b\d{1,3}(?:,?\d{3})*(?:\.\d{2})?\b/', $input));
1 ignored issue
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/\\b\\d{1,3}....\\d{2})?\\b/', $input) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
46
    }
47
}
48