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

NumberValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85.71%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 3
c 6
b 0
f 0
lcom 1
cbo 1
dl 0
loc 37
ccs 6
cts 7
cp 0.8571
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A formatDefault() 0 4 1
A formatCurrency() 0 14 2
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