AbstractFormatValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.65%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 8
c 8
b 0
f 0
lcom 1
cbo 0
dl 0
loc 85
ccs 22
cts 23
cp 0.9565
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setInput() 0 4 1
A validateFormat() 0 16 3
A getFormatMethodDetails() 0 13 3
1
<?php
2
namespace JsonTable\Validate;
3
4
/**
5
 * Format validator abstract.
6
 *
7
 * @package JSON table
8
 */
9
abstract class AbstractFormatValidator
10
{
11
    /**
12
     * @var string The type of validation being done. eg "string", "number".
13
     */
14
    protected $type;
15
16
    /**
17
     * @var mixed The input being validated.
18
     */
19
    protected $input;
20
21
22
    /**
23
     * Construct the validator.
24
     *
25
     * @param string $type The type of validation being validated.
26
     */
27 72
    public function __construct($type)
28
    {
29 72
        $this->type = (string) $type;
30 72
    }
31
32
33
    /**
34
     * Set the input to validate.
35
     *
36
     * @param mixed $input The input to validate.
37
     *
38
     * @return void
39
     */
40 72
    public function setInput($input)
41
    {
42 72
        $this->input = $input;
43 72
    }
44
45
46
    /**
47
     * Check that the input matches the specified format.
48
     *
49
     * @param string $format The format to validate against.
50
     *
51
     * @return boolean Is the data valid.
52
     *
53
     * @throws \Exception if the method to validate the format couldn't be found.
54
     */
55 72
    public function validateFormat($format)
56
    {
57 72
        if ('' === $this->input) {
58 11
            return true;
59
        }
60
61 72
        $methodDetails = $this->getFormatMethodDetails($format);
62
63 72
        if (!method_exists($this, $methodDetails['name'])) {
64
            throw new \Exception("Could not find a method to validate the $format format.");
65
        }
66
67 72
        $lb_valid = $this->$methodDetails['name']($methodDetails['parameter']);
68
69 72
        return $lb_valid;
70
    }
71
72
73
    /**
74
     * Get the name of the method and the parameter to pass to it for the specified format.
75
     *
76
     * @param   string  $format         The format to validate against.
77
     *
78
     * @return  array   $methodDetails  The name and parameter for the format.
79
     */
80 72
    private function getFormatMethodDetails($format)
81
    {
82 72
        $methodDetails = [];
83 72
        $methodDetails['name'] = 'format' . ucwords($format);
84 72
        $methodDetails['parameter'] = null;
85
86 72
        if ('datetime' === $this->type && 'default' !== $format) {
87 11
            $methodDetails['name'] = "formatDate";
88 11
            $methodDetails['parameter'] = $format;
89 11
        }
90
91 72
        return $methodDetails;
92
    }
93
}
94