Passed
Push — master ( a1e05c...a3afcf )
by Divine Niiquaye
02:24
created

RouteValidation::compareMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing;
19
20
trait RouteValidation
21
{
22
    /**
23
     * Check if given request method matches given route method.
24
     *
25
     * @param string[] $routeMethod
26
     * @param string   $requestMethod
27
     *
28
     * @return bool
29
     */
30 10
    protected function compareMethod(array $routeMethod, string $requestMethod): bool
31
    {
32 10
        return \in_array($requestMethod, $routeMethod, true);
33
    }
34
35
    /**
36
     * Check if given request domain matches given route domain.
37
     *
38
     * @param null|string              $routeDomain
39
     * @param string                   $requestDomain
40
     * @param array<int|string,string> $parameters
41
     *
42
     * @return bool
43
     */
44 11
    protected function compareDomain(?string $routeDomain, string $requestDomain, array &$parameters): bool
45
    {
46 11
        return ($routeDomain === null || empty($routeDomain)) ||
47 11
            (bool) \preg_match($routeDomain, $requestDomain, $parameters);
48
    }
49
50
    /**
51
     * Check if given request uri matches given uri method.
52
     *
53
     * @param string                   $routeUri
54
     * @param string                   $requestUri
55
     * @param array<int|string,string> $parameters
56
     *
57
     * @return bool
58
     */
59 13
    protected function compareUri(string $routeUri, string $requestUri, array &$parameters): bool
60
    {
61 13
        return (bool) \preg_match($routeUri, $requestUri, $parameters);
62
    }
63
64
    /**
65
     * Check if given request uri scheme matches given route scheme.
66
     *
67
     * @param string[] $routeScheme
68
     * @param string   $requestScheme
69
     *
70
     * @return bool
71
     */
72 8
    protected function compareScheme(array $routeScheme, string $requestScheme): bool
73
    {
74 8
        return empty($routeScheme) || \in_array($requestScheme, $routeScheme, true);
75
    }
76
77
    /**
78
     * Get merged default parameters.
79
     *
80
     * @param array<int|string,mixed> $params
81
     * @param array<string,string>    $defaults
82
     *
83
     * @return array<string,string> Merged default parameters
84
     */
85 7
    protected function mergeDefaults(array $params, array $defaults): array
86
    {
87 7
        foreach ($params as $key => $value) {
88 7
            if (!\is_int($key) && (!isset($defaults[$key]) || null !== $value)) {
89 1
                $defaults[$key] = $value;
90
            }
91
        }
92
93 7
        return $defaults;
94
    }
95
}
96