RouterParser::countParams()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace Routify;
4
5
6
class RouterParser {
7
8
    /**
9
     * @var string The path requested by the browser(or any client)
10
     */
11
    private $path;
12
13
    /**
14
     * The delimiter used to separate different paths(e.g mydomain.com/api/v1/user/Siro_Diaz).
15
     */
16
    const DELIMITER = '/';
17
18
    /**
19
     * The parameter identifier for the path pattern (e.g /api/v1/user/:screen_name).
20
     */
21
    const PARAMETER_IDENTIFIER = ':';
22
23
    public function __construct($path) {
24
        $this->path = $path;
25
    }
26
27
    /**
28
     * Sets the path.
29
     *
30
     * @param $path
31
     */
32
33
    public function setPath($path) {
34
        $this->path = $path;
35
    }
36
37
    /**
38
     * Give the requested path.
39
     *
40
     * @return string
41
     */
42
43
    public function getPath() {
44
        return $this->path;
45
    }
46
47
    /**
48
     * Checks if the path pattern has parameters(strings with : at
49
     * the beginning).
50
     *
51
     * @param $pattern
52
     * @return bool
53
     */
54
55
    public function hasParams($pattern) {
56
        $position = strpos($pattern, self::PARAMETER_IDENTIFIER);
57
        return ($position === false) ? false : true;
58
    }
59
60
    /**
61
     * Returns the number of parameters in the path pattern.
62
     *
63
     * @param $pattern
64
     * @return int
65
     */
66
67
    public function countParams($pattern) {
68
        if($this->hasParams($pattern) === false) {
69
            return 0;
70
        }
71
72
        $pattern = str_split($pattern); // split string in characters
73
        $totalParams = 0;
74
        for($i = 0; $i < count($pattern); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
75
            if($pattern[$i] === self::PARAMETER_IDENTIFIER) {
76
                $totalParams++;
77
            }
78
        }
79
80
        return $totalParams;
81
    }
82
83
    /**
84
     * Returns an associative array with the parameter name
85
     * and its value. If there is not any parameter in the pattern
86
     * then return an empty array.
87
     *
88
     * @param $pattern
89
     * @return array
90
     */
91
92
    public function getParams($pattern) {
93
        if(!$this->hasParams($pattern)) {
94
            return [];
95
        }
96
97
        $pattern = explode('/', $pattern);
98
        $path = explode('/', $this->path);
99
        $params = [];
100
        for($i = 0; $i < count($pattern); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
101
            if(strpos($pattern[$i], self::PARAMETER_IDENTIFIER) !== false) {
102
                $params[substr($pattern[$i], 1)] = $path[$i];
103
            }
104
        }
105
106
        return $params;
107
    }
108
109
    /**
110
     * Checks if the path pattern matches with the requested uri.
111
     *
112
     * @param $pattern
113
     * @return bool
114
     */
115
116
    public function match($pattern) {
117
        $pattern = explode('/', $pattern);
118
        $path = explode('/', $this->path);
119
120
        if(count($pattern) !== count($path)) {
121
            return false;
122
        }
123
124
        $found = true;
125
        $index = 0;
126
        while($found && $index < count($pattern)) {
127
            $check = true;
128
            if(strpos($pattern[$index], self::PARAMETER_IDENTIFIER) !== false) {
129
                $check = false;
130
            }
131
132
            if($check) {
133
                if (!preg_match('/^'. $pattern[$index] .'$/', $path[$index])) {
134
                    $found = false;
135
                }
136
            }
137
138
            $index++;
139
        }
140
141
        return $found;
142
    }
143
144
}