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

StringValidator::formatUri()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 0
crap 3
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
     * @access  protected
17
     *
18
     * @return  boolean Whether the input is valid.
19
     */
20 1
    protected function formatDefault()
21
    {
22 1
        return is_string($this->input);
23
    }
24
25
26
    /**
27
     * Validate that the input is a valid email address.
28
     *
29
     * @access  protected
30
     *
31
     * @return  boolean Whether the input is valid.
32
     */
33 1
    protected function formatEmail()
34
    {
35 1
        return (false !== filter_var($this->input, FILTER_VALIDATE_EMAIL));
36
    }
37
38
39
    /**
40
     * Validate that the input is a valid URI.
41
     * Although the specification for a URI states
42
     * that it must have a scheme i.e. "http://" @see http://www.faqs.org/rfcs/rfc2396.html
43
     *
44
     * This validator allows the input to miss this off so an input
45
     * of "www.example.com" will be passed as valid.
46
     *
47
     * @access  protected
48
     *
49
     * @return  boolean Whether the input is valid.
50
     */
51 1
    protected function formatUri()
52
    {
53 1
        if ($urlParts = parse_url($this->input)) {
54 1
            if (!isset($urlParts['scheme'])) {
55 1
                $this->input = "http://$this->input";
56 1
            }
57 1
        }
58
59 1
        return (false !== filter_var($this->input, FILTER_VALIDATE_URL));
60
    }
61
62
63
    /**
64
     * Validate that the input is a valid binary string.
65
     * As PHP treats all stings as binary, this is currently just a check that the input is a string.
66
     * TODO: Find a better way of validating that the string is a binary.
67
     *
68
     * @access protected
69
     *
70
     * @return boolean Whether the input is valid.
71
     */
72
    protected function formatBinary()
73
    {
74
        return is_string($this->input);
75
    }
76
}
77