NumberValidator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85.71%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 3
c 7
b 0
f 0
lcom 1
cbo 1
dl 0
loc 33
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
     * @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