UrlCompiler::getParser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lincable;
4
5
use League\Flysystem\Util;
6
use Lincable\Parsers\Parser;
7
use Lincable\Contracts\Compilers\Compiler;
8
9
class UrlCompiler implements Compiler
10
{
11
    /**
12
     * The parser instance.
13
     *
14
     * @var \Lincable\Parsers\Parser
15
     */
16
    protected $parser;
17
18
    /**
19
     * Create a new class instance.
20
     *
21
     * @param  \Lincable\Parsers\Parser|null $parser
22
     * @return void
23
     */
24 57
    public function __construct(Parser $parser = null)
25
    {
26 57
        $this->parser = $parser;
27 57
    }
28
29
    /**
30
     * Compile a given url through the parser.
31
     *
32
     * @param  string $url
33
     * @return string
34
     */
35 36
    public function compile(string $url): string
36
    {
37
        // Parse each fragment on url.
38
        $fragments = array_map(function ($fragment) {
39 36
            if ($this->getParser()->isParameterDynamic($fragment)) {
40
41
                // We assume the parameter fragment is dynamic for
42
                // parser, then we can parse it without receiving an exception
43
                // in case the fragment is not dynamic.
44 35
                return $this->getParser()->parse($fragment);
45
            }
46
47 36
            return $fragment;
48 36
        }, $this->parseUrlFragments($url));
49
50 35
        return $this->buildUrlFragments($fragments);
51
    }
52
53
    /**
54
     * Get all dynamic parameters on url based on parser.
55
     *
56
     * @return array
57
     */
58 36
    public function parseDynamics(string $url): array
59
    {
60 36
        $fragments = $this->parseUrlFragments($url);
61
62
        $dynamicParameters = array_filter($fragments, function ($parameter) {
63
64
            // Determine wheter the parameter is dynamic on parser
65
            // and should be kept.
66 36
            return $this->getParser()->isParameterDynamic($parameter);
67 36
        });
68
69
        return array_map(function ($parameter) {
70
71
            // Return the matches for the dynamic parameter.
72 35
            return $this->getParser()->getMatches($parameter);
73 36
        }, array_values($dynamicParameters));
74
    }
75
76
    /**
77
     * Determine wheter the url has dynamic parameters.
78
     *
79
     * @param  string $url
80
     * @return bool
81
     */
82 35
    public function hasDynamics(string $url): bool
83
    {
84 35
        return ! empty(array_values($this->parseDynamics($url)));
85
    }
86
87
    /**
88
     * Return all url fragments.
89
     *
90
     * @param  string $url
91
     * @return array
92
     */
93 40
    public function parseUrlFragments(string $url): array
94
    {
95 40
        return explode('/', $url);
96
    }
97
98
    /**
99
     * Build an url from array fragments.
100
     *
101
     * @param  array $fragments
102
     * @return string
103
     */
104 35
    public function buildUrlFragments(array $fragments): string
105
    {
106 35
        return Util::normalizeRelativePath(implode('/', $fragments));
107
    }
108
109
    /**
110
     * Set the parser used on compiler.
111
     *
112
     * @param  \Lincable\Parsers\Parser $parser
113
     * @return void
114
     */
115 34
    public function setParser(Parser $parser)
116
    {
117 34
        $this->parser = $parser;
118 34
    }
119
120
    /**
121
     * Get the current parser used on compiler.
122
     *
123
     * @return \Lincable\Parsers\Parser
124
     * 
125
     * @throws \Exception
126
     */
127 41
    public function getParser(): Parser
128
    {
129 41
        if ($this->parser) {
130 40
            return $this->parser;
131
        }
132
133 1
        throw new \Exception('No parser provided for compiler');
134
    }
135
}
136