1 | <?php |
||
2 | |||
3 | namespace Pmochine\LaravelTongue\Accent; |
||
4 | |||
5 | use Pmochine\LaravelTongue\Misc\Url; |
||
6 | |||
7 | class Accent |
||
8 | { |
||
9 | /** |
||
10 | * Get url using array data from parse_url. |
||
11 | * |
||
12 | * @param array|false $parsed_url Array of data from parse_url function |
||
13 | * @return string Returns URL as string. |
||
14 | */ |
||
15 | public static function unparseUrl($parsed_url) |
||
16 | { |
||
17 | if (empty($parsed_url)) { |
||
18 | return ''; |
||
19 | } |
||
20 | $url = ''; |
||
21 | $url .= isset($parsed_url['scheme']) ? $parsed_url['scheme'].'://' : ''; |
||
22 | $url .= isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
||
23 | $url .= isset($parsed_url['port']) ? ':'.$parsed_url['port'] : ''; |
||
24 | $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; |
||
25 | $pass = isset($parsed_url['pass']) ? ':'.$parsed_url['pass'] : ''; |
||
26 | $url .= $user.(($user || $pass) ? "$pass@" : ''); |
||
27 | if (! empty($url)) { |
||
28 | $url .= isset($parsed_url['path']) ? '/'.ltrim($parsed_url['path'], '/') : ''; |
||
29 | } elseif (empty($url)) { |
||
30 | $url .= isset($parsed_url['path']) ? $parsed_url['path'] : ''; |
||
31 | } |
||
32 | $url .= isset($parsed_url['query']) ? '?'.$parsed_url['query'] : ''; |
||
33 | $url .= isset($parsed_url['fragment']) ? '#'.$parsed_url['fragment'] : ''; |
||
34 | |||
35 | return $url; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Get the current route name. |
||
40 | * |
||
41 | * @return bool|array |
||
42 | */ |
||
43 | public static function currentRouteAttributes() |
||
44 | { |
||
45 | if (app('router')->current()) { |
||
46 | return app('router')->current()->parametersWithoutNulls(); |
||
47 | } |
||
48 | |||
49 | return false; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Find the route path matching the given route name. |
||
54 | * Important: Translator can give you an array as well. |
||
55 | * |
||
56 | * @param string $routeName |
||
57 | * @param string|null $locale |
||
58 | * @return string|false |
||
59 | */ |
||
60 | public static function findRoutePathByName($routeName, $locale = null) |
||
61 | { |
||
62 | if (app('translator')->has($routeName, $locale)) { |
||
63 | $name = app('translator')->get($routeName, [], $locale); |
||
64 | |||
65 | return is_string($name) ? $name : false; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
66 | } |
||
67 | |||
68 | return false; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Change route attributes for the ones in the $attributes array. |
||
73 | * |
||
74 | * @param array $attributes Array of attributes |
||
75 | * @param string $route route to substitute |
||
76 | * @return string route with attributes changed |
||
77 | */ |
||
78 | public static function substituteAttributesInRoute($attributes, $route) |
||
79 | { |
||
80 | foreach ($attributes as $key => $value) { |
||
81 | $route = str_replace('{'.$key.'}', $value, $route); |
||
82 | $route = str_replace('{'.$key.'?}', $value, $route); |
||
83 | } |
||
84 | |||
85 | // delete empty optional arguments that are not in the $attributes array |
||
86 | $route = preg_replace('/\/{[^)]+\?}/', '', $route); |
||
87 | |||
88 | return $route; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Stores the parsed url array after a few modifications. |
||
93 | * |
||
94 | * @return array |
||
95 | */ |
||
96 | public static function parseCurrentUrl() |
||
97 | { |
||
98 | $parsed_url = parse_url(app()['request']->fullUrl()); |
||
99 | |||
100 | // Don't store path, query and fragment |
||
101 | unset($parsed_url['query']); |
||
102 | unset($parsed_url['fragment']); |
||
103 | |||
104 | $parsed_url['host'] = Url::domain(); |
||
105 | |||
106 | return $parsed_url; |
||
107 | } |
||
108 | } |
||
109 |