Completed
Push — master ( 3ca135...46448f )
by Derek
02:11
created

Scheme::compileValidPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
crap 1
1
<?php
2
namespace Subreality\Dilmun\Anshar\Http\UriParts;
3
4
/**
5
 * Class Scheme
6
 * @package Subreality\Dilmun\Anshar\Http\UriParts
7
 */
8
class Scheme extends AbstractUriPart
9
{
10
    /**
11
     * Scheme constructor. Accepts a string representing a URI scheme. Construction will throw an exception if the
12
     * scheme is either not a string or does not conform to the RFC3986 URI scheme specification.
13
     *
14
     * Construction also results in the scheme string being converted to lowercase per RFC3986.
15
     *
16
     * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
17
     *
18
     * @see https://tools.ietf.org/html/rfc3986#section-3.1
19
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
20
     *
21
     * @throws \InvalidArgumentException
22
     *
23
     * @param string $scheme    A string representing a URI scheme
24
     */
25 88
    public function __construct($scheme)
26
    {
27 88
        $this->compileValidPattern();
28
29 88
        if (!is_string($scheme)) {
30 6
            throw new \InvalidArgumentException("Scheme must be a string");
31 82
        } elseif (!$this->isValid($scheme)) {
32 3
            throw new \InvalidArgumentException("Scheme must conform to RFC3986 specification");
33
        }
34
35 79
        $this->data = strtolower($scheme);
36 79
    }
37
38
    /**
39
     * Returns a string representation of the scheme formatted so that it can be compiled into a complete URI string
40
     * per the Uri object's string specification.
41
     *
42
     * If the scheme is empty, toUriString returns an empty string; if the scheme is not empty, toUriString returns the
43
     * scheme suffixed with a colon.
44
     *
45
     * @see Uri::__toString
46
     *
47
     * @return string   A string representation of the scheme formatted for a complete URI string
48
     */
49 33
    public function toUriString()
50
    {
51 33
        $uri_scheme = $this->data;
52
53 33
        if (!empty($this->data)) {
54 29
            $uri_scheme .= ":";
55 29
        }
56
57 33
        return $uri_scheme;
58
    }
59
60
    /**
61
     * Compiles a regexp pattern based on predefined patterns that define allowed characters for a scheme.
62
     */
63 88
    protected function compileValidPattern()
64
    {
65 88
        self::$valid_pattern = '/^$|^[a-z][\w\+\-\.]*$/i'; //the beginning '^$|' part allows for empty schemes
66 88
    }
67
68
    /**
69
     * Abstract implementation; scheme does not allow percent-encoding.
70
     */
71
    protected function compileUnencodedCharacters()
72
    {
73
        $this->unencoded_characters = array();
74
    }
75
}
76