Completed
Push — master ( 3b20ab...7259eb )
by Derek
05:04
created

Scheme   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

2 Methods

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