Completed
Push — dev ( a62e57...4efd8c )
by Yan
02:03
created

UrlCompiler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lincable;
4
5
use Lincable\Parsers\Parser;
6
use Lincable\Contracts\Compilers\Compiler;
7
8
class UrlCompiler implements Compiler
9
{
10
    /**
11
     * The parser instance.
12
     *
13
     * @var \Lincable\Parsers\Parser
14
     */
15
    protected $parser;
16
17
    /**
18
     * Create a new class instance.
19
     *
20
     * @param  \Lincable\Parsers\Parser $parser
21
     * @return void
22
     */
23
    public function __construct(Parser $parser)
24
    {
25
        $this->parser = $parser;
26
    }
27
28
    /**
29
     * Get all dynamic parameters on url.
30
     *
31
     * @param  string $url
32
     * @return array
33
     */
34
    public function compile(string $url): array
35
    {
36
        $parameters = $this->parseUrlFragments($url);
37
38
        $dynamicParameters = array_filter($parameters, function ($parameter) {
39
40
            // Determine wheter the parameter is dynamic on parser
41
            // and should be kept.
42
            return $this->parser->isParameterDynamic($parameter);
43
        });
44
45
        return array_flatten(array_map(function ($parameter) {
46
47
            // Return the matches for the dynamic parameter.
48
            return $this->parser->getMatches($parameter);
49
        }, $dynamicParameters));
50
    }
51
52
    /**
53
     * Return all url fragments.
54
     *
55
     * @param  string $url
56
     * @return array
57
     */
58
    public function parseUrlFragments(string $url): array
59
    {
60
        return explode('/', $url);
61
    }
62
}
63