Completed
Pull Request — master (#86)
by Vladimir
08:24
created

RouteMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Server;
9
10
use __\__;
11
use allejo\stakx\Document\BasePageView;
12
use allejo\stakx\Document\DynamicPageView;
13
use allejo\stakx\Document\RepeaterPageView;
14
use allejo\stakx\Document\StaticPageView;
15
use allejo\stakx\FrontMatter\FrontMatterParser;
16
17
class RouteMapper
18
{
19
    private $redirects;
20
    private $baseUrl;
21
    private $mapping;
22
23
    public function __construct()
24
    {
25
        $this->mapping = [];
26
    }
27
28
    public function getBaseUrl()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
29
    {
30
        return $this->baseUrl;
31
    }
32
33
    public function setBaseUrl($baseUrl)
34
    {
35
        $this->baseUrl = $baseUrl;
36
    }
37
38
    /**
39
     * Register a PageView to this router.
40
     */
41
    public function registerPageView(BasePageView $pageView)
42
    {
43
        switch ($pageView->getType())
44
        {
45
            case BasePageView::STATIC_TYPE:
46
                $this->mapping[$pageView->getPermalink()] = $pageView;
47
48
                foreach ($pageView->getRedirects() as $redirect)
49
                {
50
                    $this->redirects[$redirect] = $pageView->getPermalink();
51
                }
52
53
                break;
54
55
            case BasePageView::DYNAMIC_TYPE:
56
                $rawFM = $pageView->getRawFrontMatter();
57
                $permalinkFM = \__::get($rawFM, 'permalink');
58
59
                $permalink = is_array($permalinkFM) ? $permalinkFM[0] : $permalinkFM;
60
                $permalink = preg_replace(FrontMatterParser::VARIABLE_DEF, '{$1}', $permalink);
61
62
                $this->mapping[$permalink] = $pageView;
63
                break;
64
65
            case BasePageView::REPEATER_TYPE:
66
                $rawFM = $pageView->getRawFrontMatter();
67
                $permalinkFM = \__::get($rawFM, 'permalink');
68
69
                $permalink = is_array($permalinkFM) ? $permalinkFM[0] : $permalinkFM;
70
                $permalink = preg_replace(FrontMatterParser::VARIABLE_DEF, '{$1}', $permalink);
71
                $permalink = preg_replace(FrontMatterParser::ARRAY_DEF, '{$1}', $permalink);
72
73
                // Replace `.` in complex FM variables because they don't work in routes
74
                $permalink = preg_replace('/\./', '_', $permalink);
75
76
                $this->mapping[$permalink] = $pageView;
77
                break;
78
        }
79
    }
80
81
    /**
82
     * Get the PageView used for a specified route.
83
     *
84
     * @param string $route
85
     *
86
     * @return StaticPageView|DynamicPageView|RepeaterPageView|null
87
     */
88
    public function getRoute($route)
89
    {
90
        return __::get($this->mapping, $route);
91
    }
92
93
    /**
94
     * Retrieve all of the URL routes registered.
95
     *
96
     * @return string[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array<integer|string>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
97
     */
98
    public function getRoutes()
99
    {
100
        return array_keys($this->mapping);
101
    }
102
103
    public function &getRouteMapping()
104
    {
105
        return $this->mapping;
106
    }
107
108
    public static function extractUrlParameters($permalink)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
109
    {
110
        $matches = [];
111
112
        preg_match_all(FrontMatterParser::ANY_VARIABLE, $permalink, $matches);
113
114
        return $matches[1];
115
    }
116
}
117