Completed
Push — master ( 4c30b1...acf2b2 )
by Derek
02:33
created

Scheme   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 0
dl 0
loc 62
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 4
A __toString() 0 4 1
A isValid() 0 4 1
A toUriString() 0 10 2
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
9
{
10
    private static $valid_pattern = '/^[a-z][\w\+\-\.]*$/i';
11
12
    private $scheme = "";
13
14
    /**
15
     * Scheme constructor. Accepts a string representing a URI scheme. Construction will throw an exception if the
16
     * scheme is either not a string or does not conform to the RFC3986 URI scheme specification.
17
     *
18
     * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
19
     *
20
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
21
     *
22
     * @throws \InvalidArgumentException
23
     *
24
     * @param string $scheme    A string representing a URI scheme
25
     */
26 12
    public function __construct($scheme)
27
    {
28 12
        if (!is_string($scheme)) {
29 6
            throw new \InvalidArgumentException("Scheme must be a string");
30 6
        } elseif (!empty($scheme) && !$this->isValid($scheme)) {
31 3
            throw new \InvalidArgumentException("Scheme must conform to RFC3986 specification");
32
        }
33
34 3
        $this->scheme = $scheme;
35 3
    }
36
37 3
    public function __toString()
38
    {
39 3
        return $this->scheme;
40
    }
41
42
    /**
43
     * Determines whether a given scheme adheres to the RFC3986 specification:
44
     *
45
     * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
46
     *
47
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
48
     * @see Scheme::$valid_pattern
49
     *
50
     * @param string $scheme    The scheme to check for validity
51
     * @return bool             Returns true if the provided scheme matches the RFC3986 specification
52
     *                          Returns false otherwise
53
     */
54 10
    public static function isValid($scheme)
55
    {
56 10
        return (bool) preg_match(self::$valid_pattern, $scheme);
57
    }
58
59 2
    public function toUriString()
60
    {
61 2
        $uri_scheme = $this->scheme;
62
63 2
        if (!empty($this->scheme)) {
64 1
            $uri_scheme .= ":";
65 1
        }
66
67 2
        return $uri_scheme;
68
    }
69
}
70