Passed
Branch release (695ec7)
by Henry
04:31
created

Path::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of the Divergence package.
4
 *
5
 * (c) Henry Paradiz <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Divergence\Routing;
11
12
/**
13
 * Tree style router with path traversal
14
 * @author Henry Paradiz <[email protected]>
15
 */
16
class Path
17
{
18
    public array $pathStack;
19
    public array $requestPath;
20
    protected array $_path;
21
22
    public function __construct(string $path)
23
    {
24
        $this->setPath($path);
25
    }
26
27
    /**
28
     * If path is /my/example this function returns my the first time and example the second time
29
     * False is returned once you run out of path.
30
     * @return string|false
31
     */
32
    public function peekPath()
33
    {
34
        return count($this->_path) ? $this->_path[0] : false;
35
    }
36
37
    /**
38
     * The shifted value, or null; if array is empty or is not an array
39
     *
40
     * @return mixed
41
     */
42
    public function shiftPath()
43
    {
44
        return array_shift($this->_path);
45
    }
46
47
    /**
48
     * Returns the path stack
49
     *
50
     * @return array
51
     */
52
    public function getPath()
53
    {
54
        return $this->_path;
55
    }
56
57
    /**
58
     * Adds a value to the path stack so that the next shiftPath or peekPath is what you provide here
59
     *
60
     * @param string $string
61
     * @return int Count of path stack after appending to it
62
     */
63
    public function unshiftPath($string)
64
    {
65
        return array_unshift($this->_path, $string);
66
    }
67
68
    /**
69
     * Private makes this class immutable
70
     *
71
     * @param string $requestURI
72
     * @return void
73
     */
74
    protected function setPath($requestURI = null)
75
    {
76
        if (!isset($this->pathStack)) {
77
            $parsedURL = parse_url($requestURI);
78
            $this->pathStack = $this->requestPath = explode('/', ltrim($parsedURL['path'], '/'));
79
        }
80
81
        $this->_path = isset($path) ? $path : $this->pathStack;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $path seems to never exist and therefore isset should always be false.
Loading history...
82
    }
83
}
84