Completed
Push — master ( 922528...99faf5 )
by Ben
04:00
created

RouteParser   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 143
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A set() 0 6 1
A get() 0 10 2
A localize() 0 7 1
A parameters() 0 6 1
A secure() 0 6 1
A replaceParameters() 0 9 1
A replaceRouteParameters() 0 12 3
A replaceNamedParameters() 0 6 2
1
<?php
2
3
namespace Thinktomorrow\Locale\Parsers;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Illuminate\Translation\Translator;
8
9
class RouteParser implements Parser
10
{
11
    /**
12
     * @var UrlParser
13
     */
14
    private $parser;
15
16
    /**
17
     * @var Translator
18
     */
19
    private $translator;
20
21
    private $name;
22
    private $parameters = [];
23
    private $localeslug;
24
25 141
    public function __construct(UrlParser $parser,Translator $translator)
26
    {
27 141
        $this->parser = $parser;
28 141
        $this->translator = $translator;
29 141
    }
30
31
    /**
32
     * Set the routename
33
     *
34
     * @param $name
35
     * @return mixed
36
     */
37 90
    public function set($name)
38
    {
39 90
        $this->name = $name;
40
41 90
        return $this;
42
    }
43
44
    /**
45
     * Retrieve the generated / altered url
46
     * If no translated routekey is found, it means the route itself does not need to be
47
     * translated and we allow the native url generator to deal with the route generation
48
     *
49
     * @return mixed
50
     */
51 90
    public function get()
52
    {
53 90
        $routekey = $this->translator->get('routes.'.$this->name,[],$this->localeslug);
54
55 90
        $uri = ($routekey === 'routes.'.$this->name)
56 90
                ? $this->parser->resolveRoute($this->name,$this->parameters)
57 90
                : $this->replaceParameters($routekey,$this->parameters);
58
59 90
        return $this->parser->set($uri)->get();
60
    }
61
62
    /**
63
     * Place locale segment in front of url path
64
     * e.g. /foo/bar is transformed into /en/foo/bar
65
     *
66
     * @param null $localeslug
67
     * @return string
68
     */
69 87
    public function localize($localeslug = null)
70
    {
71 87
        $this->localeslug = $localeslug;
72 87
        $this->parser->localize($localeslug);
73
74 87
        return $this;
75
    }
76
77
    /**
78
     * @param array $parameters
79
     * @return $this
80
     */
81 90
    public function parameters(array $parameters = [])
82
    {
83 90
        $this->parameters = $parameters;
84
85 90
        return $this;
86
    }
87
88
    /**
89
     * @param bool $secure
90
     * @return $this
91
     */
92 3
    public function secure($secure = true)
93
    {
94 3
        $this->parser->secure($secure);
95
96 3
        return $this;
97
    }
98
99
    /**
100
     * Replace route parameters
101
     *
102
     * @param $uri
103
     * @param array $parameters
104
     * @return mixed|string
105
     */
106 12
    protected function replaceParameters($uri, $parameters = [])
107
    {
108 12
        $parameters = (array)$parameters;
109
110 12
        $uri = $this->replaceRouteParameters($uri,$parameters);
111 12
        $uri = str_replace('//','/',$uri);
112
113 12
        return $uri;
114
    }
115
116
    /**
117
     * Replace all of the wildcard parameters for a route path.
118
     * @note: based on the Illuminate\Routing\UrlGenerator code
119
     *
120
     * @param  string  $path
121
     * @param  array  $parameters
122
     * @return string
123
     */
124 12
    protected function replaceRouteParameters($path, array $parameters)
125
    {
126 12
        $path = $this->replaceNamedParameters($path, $parameters);
127
128
        $path = preg_replace_callback('/\{.*?\}/', function ($match) use (&$parameters) {
129 12
            return (empty($parameters) && ! Str::endsWith($match[0], '?}'))
130 12
                ? $match[0]
131 12
                : array_shift($parameters);
132 12
        }, $path);
133
134 12
        return trim(preg_replace('/\{.*?\?\}/', '', $path), '/');
135
    }
136
137
    /**
138
     * Replace all of the named parameters in the path.
139
     * @note: based on the Illuminate\Routing\UrlGenerator code
140
     *
141
     * @param  string  $path
142
     * @param  array  $parameters
143
     * @return string
144
     */
145
    protected function replaceNamedParameters($path, &$parameters)
146
    {
147 12
        return preg_replace_callback('/\{(.*?)\??\}/', function ($m) use (&$parameters) {
148 12
            return isset($parameters[$m[1]]) ? Arr::pull($parameters, $m[1]) : $m[0];
149 12
        }, $path);
150
    }
151
}