Completed
Pull Request — master (#75)
by Vladimir
02:16
created

PageViewRouter::setBaseUrl()   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 1
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 PageViewRouter
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::ARRAY_DEF, '{$1}', $permalink);
71
72
                // Replace `.` in complex FM variables because they don't work in routes
73
                $permalink = preg_replace('/\./', '_', $permalink);
74
75
                $this->mapping[$permalink] = $pageView;
76
                break;
77
        }
78
    }
79
80
    /**
81
     * Get the PageView used for a specified route.
82
     *
83
     * @param string $route
84
     *
85
     * @return StaticPageView|DynamicPageView|RepeaterPageView|null
86
     */
87
    public function getRoute($route)
88
    {
89
        return __::get($this->mapping, $route);
90
    }
91
92
    /**
93
     * Retrieve all of the URL routes registered.
94
     *
95
     * @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...
96
     */
97
    public function getRoutes()
98
    {
99
        return array_keys($this->mapping);
100
    }
101
102
    public function &getRouteMapping()
103
    {
104
        return $this->mapping;
105
    }
106
107
    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...
108
    {
109
        $matches = [];
110
111
        preg_match_all(FrontMatterParser::ANY_VARIABLE, $permalink, $matches);
112
113
        return $matches[1];
114
    }
115
}
116