NumberValidator::formatCurrency()   A
last analyzed

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 3
Bugs 0 Features 0
Metric Value
c 3
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
     * @return  boolean Whether the input is valid.
17
     */
18 4
    protected function formatDefault()
19
    {
20 4
        return (false !== filter_var($this->input, FILTER_VALIDATE_FLOAT));
21
    }
22
23
24
    /**
25
     * Validate that the input is a valid currency.
26
     *
27
     * @return  boolean Whether the input is valid.
28
     */
29 5
    protected function formatCurrency()
30
    {
31
        // Remove any non-digits from the input.
32
        //TODO: Validate that any non-digits are valid currency characters.
33 5
        $input = preg_filter('/^\D./', '', $this->input);
34
35 5
        if (empty($input)) {
36
            return true;
37
        }
38
39
        // Check that the remainder of the input matches an expected currency format.
40
        // This regex is provided by Tim Pietzcker here: http://stackoverflow.com/a/4983648.
41 5
        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...
42
    }
43
}
44