1
|
|
|
<?php |
2
|
|
|
use Nip\Dispatcher\Dispatcher; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Nip_Helper_Url |
6
|
|
|
*/ |
7
|
|
|
class Nip_Helper_Url extends Nip\Helpers\AbstractHelper |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
use \Nip\Router\RouterAwareTrait; |
10
|
|
|
|
11
|
|
|
protected $pieces = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Singleton |
15
|
|
|
* @return Nip_Helper_URL |
16
|
|
|
*/ |
17
|
|
|
static public function instance() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
static $instance; |
20
|
|
|
if (!($instance instanceof self)) { |
21
|
|
|
$instance = new self(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $instance; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param $name |
29
|
|
|
* @param $arguments |
30
|
|
|
* @return $this|mixed |
31
|
|
|
*/ |
32
|
|
|
public function __call($name, $arguments) |
33
|
|
|
{ |
34
|
|
|
if ($name == ucfirst($name)) { |
35
|
|
|
$this->pieces[] = Dispatcher::reverseControllerName($name); |
36
|
|
|
return $this; |
37
|
|
|
} else { |
38
|
|
|
$this->pieces[] = $name; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$name = $this->pieces ? implode(".", $this->pieces) : ''; |
42
|
|
|
$this->pieces = []; |
43
|
|
|
return $this->assemble($name, isset($arguments[0]) ? $arguments[0] : null); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $name |
48
|
|
|
* @param bool $params |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function assemble($name, $params = false) |
52
|
|
|
{ |
53
|
|
|
return $this->getRouter()->assembleFull($name, $params); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $name |
58
|
|
|
* @param bool $params |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function get($name, $params = false) |
62
|
|
|
{ |
63
|
|
|
return $this->assemble($name, $params); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $name |
68
|
|
|
* @param bool $params |
69
|
|
|
* @return mixed|string |
70
|
|
|
*/ |
71
|
|
|
public function route($name, $params = false) |
72
|
|
|
{ |
73
|
|
|
return $this->getRouter()->assemble($name, $params); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $params |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function base($params = []) |
81
|
|
|
{ |
82
|
|
|
$currentRoute = $this->getRouter()->getCurrent(); |
83
|
|
|
$base = $currentRoute ? $currentRoute->getBase($params) : request()->root(); |
84
|
|
|
|
85
|
|
|
return $base.($params ? "?".http_build_query($params) : ''); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Reverse of the PHP built-in function parse_url |
90
|
|
|
* |
91
|
|
|
* @see http://php.net/parse_url |
92
|
|
|
* @param array $params |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function build($params) |
96
|
|
|
{ |
97
|
|
|
return ((isset($params['scheme'])) ? $params['scheme'] . '://' : '') |
98
|
|
|
. ((isset($params['user'])) ? $params['user'] . ((isset($params['pass'])) ? ':' . $params['pass'] : '') . '@' : '') |
99
|
|
|
. ((isset($params['host'])) ? $params['host'] : '') |
100
|
|
|
. ((isset($params['port'])) ? ':' . $params['port'] : '') |
101
|
|
|
. ((isset($params['path'])) ? $params['path'] : '') |
102
|
|
|
. ((isset($params['query'])) ? '?' . $params['query'] : '') |
103
|
|
|
. ((isset($params['fragment'])) ? '#' . $params['fragment'] : ''); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Replaces all non-alphanumeric characters and returns dash-separated string |
108
|
|
|
* |
109
|
|
|
* @param string $input |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function encode($input) |
113
|
|
|
{ |
114
|
|
|
$chars = [ |
115
|
|
|
'Ă' => 'a', |
116
|
|
|
'ă' => 'a', |
117
|
|
|
'Â' => 'A', |
118
|
|
|
'â' => 'a', |
119
|
|
|
'Î' => 'I', |
120
|
|
|
'î' => 'i', |
121
|
|
|
'Ș' => 'S', |
122
|
|
|
'ș' => 's', |
123
|
|
|
'Ş' => 'S', |
124
|
|
|
'ş' => 's', |
125
|
|
|
'Ț' => 'T', |
126
|
|
|
'ț' => 't', |
127
|
|
|
'Ţ' => 'T', |
128
|
|
|
'ţ' => 't' |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
$change = $with = []; |
132
|
|
|
foreach ($chars as $i => $v) { |
133
|
|
|
$change[] = html_entity_decode($i, ENT_QUOTES, 'UTF-8'); |
134
|
|
|
$with[] = $v; |
135
|
|
|
} |
136
|
|
|
$input = str_replace($change, $with, $input); |
137
|
|
|
|
138
|
|
|
preg_match_all("/[a-z0-9]+/i", $input, $sections); |
139
|
|
|
return strtolower(implode("-", $sections[0])); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getRequest() |
143
|
|
|
{ |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.