SchemeRegistry::isStandardPort()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 12
1
<?php
2
/**
3
 * This file is part of the Uri package.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Uri;
9
10
/**
11
 * Scheme registry class.
12
 *
13
 * @package GravityMedia\Uri
14
 */
15
class SchemeRegistry
16
{
17
    /**
18
     * The schemes.
19
     *
20
     * @var array
21
     */
22
    protected static $schemes = [
23
        'http' => 80,
24
        'https' => 443,
25
    ];
26
27
    /**
28
     * Register scheme.
29
     *
30
     * @param string     $scheme
31
     * @param int|string $port
32
     *
33
     * @throws \InvalidArgumentException
34
     */
35
    public static function registerScheme($scheme, $port)
36
    {
37
        if (!is_string($scheme)) {
38
            throw new \InvalidArgumentException('Invalid scheme argument');
39
        }
40
41
        if (!is_numeric($port)) {
42
            throw new \InvalidArgumentException('Invalid port argument');
43
        }
44
45
        static::$schemes[strtolower($scheme)] = (int)$port;
46
    }
47
48
    /**
49
     * Register schemes.
50
     *
51
     * @param array $schemes
52
     *
53
     * @throws \InvalidArgumentException
54
     */
55
    public static function registerSchemes(array $schemes)
56
    {
57
        foreach ($schemes as $scheme => $port) {
58
            static::registerScheme($scheme, $port);
59
        }
60
    }
61
62
    /**
63
     * Return whether or not the scheme is registered.
64
     *
65
     * @param string $scheme
66
     *
67
     * @return bool
68
     * @throws \InvalidArgumentException
69
     */
70
    public static function isSchemeRegistered($scheme)
71
    {
72
        if (!is_string($scheme)) {
73
            throw new \InvalidArgumentException('Invalid scheme argument');
74
        }
75
76
        return isset(static::$schemes[strtolower($scheme)]);
77
    }
78
79
    /**
80
     * Return whether or not the port is the standard port of the scheme.
81
     *
82
     * @param string     $scheme
83
     * @param int|string $port
84
     *
85
     * @return bool
86
     * @throws \InvalidArgumentException
87
     */
88
    public static function isStandardPort($scheme, $port)
89
    {
90
        if (!static::isSchemeRegistered($scheme)) {
91
            throw new \InvalidArgumentException('Scheme is not registered');
92
        }
93
94
        if (!is_numeric($port)) {
95
            return false;
96
        }
97
98
        return (int)$port === static::$schemes[strtolower($scheme)];
99
    }
100
}
101