Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Validator
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 6
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 validate
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 __getState
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 __getRule
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 __getDescriptions
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 __getValidatorFlags
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace USDLRegex;
3/**
4 * Validation class.
5 */
6class Validator {
7    /**
8     * Map of states and their rules
9     * @var array
10     */
11    public $rules = array();
12    /**
13     * Flag for adding verbosity to output
14     * @var boolean
15     */
16    public $verbose         = FALSE;
17    /**
18     * Flag for making regex comparisons case insensitive. e.g. the `i` flag
19     * @var boolean
20     */
21    public $caseInsensitive = FALSE;
22    function __construct($options = array()) {
23        $defaults = array(
24            'rules'           => json_decode(file_get_contents(__DIR__.'/regex.json'), TRUE),
25            'verbose'         => FALSE,
26            'caseInsensitive' => FALSE,
27        );
28        $options = array_merge($defaults, $options);
29        $this->rules           = $options['rules'];
30        $this->verbose         = $options['verbose'];
31        $this->caseInsensitive = $options['caseInsensitive'];
32    }
33    /**
34     * Takes the passed state to lookup the regex rules, and tries to match
35     * in the provided string.
36     *
37     * @param  String $state     2 letter abbreviation of the state
38     * @param  String $dlString  string representatino of the driver license number
39     *
40     * @return Bool              TRUE/FALSE if the number is valid
41     */
42    public function validate($state, $dlString) {
43        $rule       = $this->__getRule($state);
44        $regexFlags = $this->__getValidatorFlags();
45        if ($this->verbose === TRUE) {
46            echo "Validating $dlString using rule $rule\n";
47        }
48        $isValid = (bool)preg_match("/$rule/$regexFlags", $dlString);
49        if ($isValid === FALSE && $this->verbose === TRUE) {
50            echo "Didn't match against the following criteria:\n";
51            foreach ($this->__getDescriptions($state) as $description) {
52                echo "  $description\n";
53            }
54        }
55        return $isValid;
56    }
57    /**
58     * Finds and returns the registered state definition.
59     *
60     * @param  String $key State to lookup rules for
61     *
62     * @return Array       Array of Rule and Descriptions
63     *
64     * @throws USDLRegex\ValueException         No State key was provided
65     * @throws USDLRegex\StateNotFoundException Passed state doesn't exist in key
66     */
67    private function __getState($key=NULL) {
68        if ($key === NULL) {
69            throw new ValueException('Must provide a key to get a rule');
70        }
71        $key = strtoupper($key);
72        if (!array_key_exists($key, $this->rules)) {
73            throw new StateNotFoundException("State '$key' not found in rule set");
74        }
75        return $this->rules[$key];
76    }
77    /**
78     * Finds and returns the validation rule for the state.
79     *
80     * @param  String $key State to lookup rules for
81     *
82     * @return String      String representation of regex
83     *
84     * @throws USDLRegex\RuleFormatException 'rule' key doesn't exist for the passed
85     *                                       state.
86     */
87    private function __getRule($key=NULL) {
88        $state = $this->__getState($key);
89        if (!array_key_exists('rule', $state)) {
90            throw new RuleFormatException("Malformed rules JSON: State '$key' does not have a 'rule' key.");
91        }
92        return $state['rule'];
93    }
94    /**
95     * Finds and returns the list of friendly descriptions for the state rule regex.
96     *
97     * @param  String $key State to lookup rules for
98     *
99     * @return Array       Array of strings with friendly regex rules
100     *
101     * @throws USDLRegex\RuleFormatException 'description' key doesn't exist for the
102     *                                       passed state.
103     */
104    private function __getDescriptions($key=NULL) {
105        $state = $this->__getState($key);
106        if (!array_key_exists('description', $state)) {
107            throw new RuleFormatException("Malformed rules JSON: State '$key' does not have a 'description' key.");
108        }
109        return $state['description'];
110    }
111    /**
112     * Compiles a list of regex directives to be used while validating.
113     *
114     * @return String string of regex directives
115     */
116    private function __getValidatorFlags() {
117        $validatorFlags = '';
118        if ($this->caseInsensitive === TRUE) {
119            $validatorFlags .= 'i';
120        }
121        return $validatorFlags;
122    }
123}