RoutedPath::getPath()   A
last analyzed

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 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_routed_paths;
4
5
6
use kalanis\kw_paths\PathsException;
7
use kalanis\kw_paths\Stuff;
8
9
10
/**
11
 * Class RoutedPath
12
 * @package kalanis\kw_routed_paths
13
 * Parsed route path data
14
 * Notes:
15
 * On web:
16
 * - documentRoot is usually container for value _SERVER['DOCUMENT_ROOT'] - path to the scripts
17
 * - staticalPath and virtualPrefix are changeable with documentRoot when the content is sent outside
18
 * - with all these values and constants inside the interface it's possible to make walk through the file tree
19
 * In admin:
20
 * - documentRoot is still basic dir from _SERVER
21
 * - pathToSystemRoot is then transfer from system root to dir where the user dir is stored
22
 * - user is name of logged user from some source
23
 * - path is path to currently processed content; depends on module if it's file or dir
24
 *
25
 * On Windows following variables contains backslashes as directory separators:
26
 * - path
27
 * - documentRoot
28
 * - pathToSystemRoot
29
 */
30
class RoutedPath
31
{
32
    protected string $staticPath = ''; // in browser the path which stay the same and targets the document root from the outside
33
    protected string $virtualPrefix = ''; // in browser the separation value between static part and virtual one
34
    protected string $user = ''; // user whom content is looked for
35
    protected string $lang = ''; // in which language will be content provided, also affects path
36
    /** @var string[] */
37
    protected array $path = []; // the rest of path
38
    /** @var string[] */
39
    protected array $module = []; // basic module which will be used as default one to present the content
40
    protected bool $isSingle = false; // is module the master of page and should be there another as wrapper?
41
42
    /**
43
     * @param Sources\ASource $source
44
     * @throws PathsException
45
     */
46 19
    public function __construct(Sources\ASource $source)
47
    {
48 19
        $params = $source->getData();
49 19
        $this->user = strval($params['user'] ?? $this->user );
50 19
        $this->lang = strval($params['lang'] ?? $this->lang );
51 19
        $this->path = array_filter(isset($params['path']) ? Stuff::linkToArray(strval($params['path'])) : $this->path);
52 19
        $this->module = array_filter(isset($params['module']) ? Support::moduleNameFromRequest(strval($params['module'])) : $this->module);
53 19
        $this->isSingle = isset($params['single']);
54 19
        $this->staticPath = strval($params['staticPath'] ?? $this->staticPath );
55 19
        $this->virtualPrefix = strval($params['virtualPrefix'] ?? $this->virtualPrefix );
56 19
    }
57
58 14
    public function getStaticPath(): string
59
    {
60 14
        return $this->staticPath;
61
    }
62
63 14
    public function getVirtualPrefix(): string
64
    {
65 14
        return $this->virtualPrefix;
66
    }
67
68 19
    public function getUser(): string
69
    {
70 19
        return $this->user;
71
    }
72
73 2
    public function getLang(): string
74
    {
75 2
        return $this->lang;
76
    }
77
78
    /**
79
     * @return string[]
80
     */
81 18
    public function getPath(): array
82
    {
83 18
        return $this->path;
84
    }
85
86
    /**
87
     * @return string[]
88
     */
89 18
    public function getModule(): array
90
    {
91 18
        return $this->module;
92
    }
93
94 18
    public function isSingle(): bool
95
    {
96 18
        return $this->isSingle;
97
    }
98
99
    /**
100
     * @return array<string, bool|string|int|array<string>>
101
     */
102 2
    public function getArray(): array
103
    {
104
        return [
105 2
            'user' => $this->user,
106 2
            'lang' => $this->lang,
107 2
            'path' => $this->path,
108 2
            'module' => $this->module,
109 2
            'isSingle' => $this->isSingle,
110 2
            'staticPath' => $this->staticPath,
111 2
            'virtualPrefix' => $this->virtualPrefix,
112
        ];
113
    }
114
}
115