StringValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 84.62%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 6
c 8
b 0
f 0
lcom 1
cbo 1
dl 0
loc 58
ccs 11
cts 13
cp 0.8462
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A formatDefault() 0 4 1
A formatEmail() 0 4 1
A formatUri() 0 10 3
A formatBinary() 0 4 1
1
<?php
2
namespace JsonTable\Validate\Format;
3
4
use \JsonTable\Validate\AbstractFormatValidator;
5
6
/**
7
 * Lexical string validator.
8
 *
9
 * @package JSON table
10
 */
11
class StringValidator extends AbstractFormatValidator
12
{
13
    /**
14
     * Validate that the input is a valid string.
15
     *
16
     * @return  boolean Whether the input is valid.
17
     */
18 72
    protected function formatDefault()
19
    {
20 72
        return is_string($this->input);
21
    }
22
23
24
    /**
25
     * Validate that the input is a valid email address.
26
     *
27
     * @return  boolean Whether the input is valid.
28
     */
29 72
    protected function formatEmail()
30
    {
31 72
        return (false !== filter_var($this->input, FILTER_VALIDATE_EMAIL));
32
    }
33
34
35
    /**
36
     * Validate that the input is a valid URI.
37
     * Although the specification for a URI states
38
     * that it must have a scheme i.e. "http://" @see http://www.faqs.org/rfcs/rfc2396.html
39
     *
40
     * This validator allows the input to miss this off so an input
41
     * of "www.example.com" will be passed as valid.
42
     *
43
     * @return  boolean Whether the input is valid.
44
     */
45 72
    protected function formatUri()
46
    {
47 72
        if ($urlParts = parse_url($this->input)) {
48 72
            if (!isset($urlParts['scheme'])) {
49 72
                $this->input = "http://$this->input";
50 72
            }
51 72
        }
52
53 72
        return (false !== filter_var($this->input, FILTER_VALIDATE_URL));
54
    }
55
56
57
    /**
58
     * Validate that the input is a valid binary string.
59
     * As PHP treats all stings as binary, this is currently just a check that the input is a string.
60
     * TODO: Find a better way of validating that the string is a binary.
61
     *
62
     * @return boolean Whether the input is valid.
63
     */
64
    protected function formatBinary()
65
    {
66
        return is_string($this->input);
67
    }
68
}
69