|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * [URL] - Classe responsável por manipular a URL informada na barra de endereço. |
| 5 | + * |
| 6 | + * 16/04/2020 -> Documentação Revisada |
| 7 | + * 16/01/2021 -> Feitos ajustes em métodos |
| 8 | + * 16/01/2021 -> __construct (com/sem a barra no final) |
| 9 | + * 16/01/2021 -> getId (para trabalhar melhor com SEO e URLs amigáveis); |
| 10 | + * 16/01/2021 -> getApos (refatorado método) |
| 11 | + * 16/01/2021 -> getURLApos (refatorado método) |
| 12 | + * 16/01/2021 -> getIdApos (refatorado método) |
| 13 | + * 16/01/2021 -> getAntes (refatorado método) |
| 14 | + * 16/01/2021 -> getURLAntes (refatorado método) |
| 15 | + * 16/01/2021 -> getIdAntes (refatorado método) |
| 16 | + * 16/01/2021 -> Renomeado método gerarLinkWebsite para gerarLinkInterno |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +class url { |
| 21 | + |
| 22 | + private $site = null; // [ATRIBUTO] - Nome do Website onde está a classe; |
| 23 | + private $url = null; // [ATRIBUTO] - URL informada na Barra de Endereço. |
| 24 | + private $url_agora = null; // [ATRIBUTO] - URL atual. |
| 25 | + private $partes = null; // [ATRIBUTO] - Partes da URL informada na Barra de Endereço. |
| 26 | + private $regras = array(); // [ATRIBUTO] - Regras da adicionadas pelo usuário. |
| 27 | + private $get = false; // [ATRIBUTO] - Filtra o $_GET da URL para interação com a classe. Pode ser retirado. |
| 28 | + |
| 29 | + /** |
| 30 | + * __construct Monta a URL assim que a página é aberta. |
| 31 | + */ |
| 32 | + public function __construct($url = false, $get = false) { |
| 33 | + $this->setGet($get); |
| 34 | + $this->url = ($url ? $url : ($this->get ? preg_replace("/\?.*/", "", preg_replace("/http(s)?\:\/\//", "", $_SERVER['REQUEST_URI'])) : preg_replace("/http(s)?\:\/\//", "", $_SERVER['REQUEST_URI']))); |
| 35 | + $lastChar = substr($this->url, -1); |
| 36 | + if($lastChar!=="/"){ |
| 37 | + $this->url = $this->url."/"; |
| 38 | + } |
| 39 | + $this->site = $_SERVER['HTTP_HOST']; |
| 40 | + $this->url_agora = $this->site . $this->url; |
| 41 | + $partes = explode("/", $this->url); |
| 42 | + if ($url) { |
| 43 | + if (preg_match("/\//", $url)) { |
| 44 | + $informada = explode("/", $url); |
| 45 | + foreach ($informada as $key => $value) { |
| 46 | + if (in_array($value, $partes)) { |
| 47 | + unset($partes[array_search($value, $partes)]); |
| 48 | + } |
| 49 | + } |
| 50 | + } else { |
| 51 | + unset($partes[array_search($url, $partes)]); |
| 52 | + } |
| 53 | + } |
| 54 | + unset($partes[0]); |
| 55 | + unset($partes[count($partes)]); |
| 56 | + $partes = explode("/", implode("/", $partes)); |
| 57 | + $this->partes = $partes; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param int $parte Recebe a posição desejada. |
| 62 | + * @return string $posicao Retorna o texto da url na posicao especificada. |
| 63 | + */ |
| 64 | + public function get($parte) { |
| 65 | + if (array_key_exists($parte, $this->partes)) { |
| 66 | + return $this->partes[$parte]; |
| 67 | + } |
| 68 | + if (array_key_exists($parte, $this->regras)) { |
| 69 | + return $this->partes[$this->regras[$parte]]; |
| 70 | + } |
| 71 | + return ""; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return string $string Retorna o $host do site. |
| 76 | + */ |
| 77 | + public function getSite() { |
| 78 | + return $this->site; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return array $array Retorna todas as partes da URL. |
| 83 | + */ |
| 84 | + public function getPartes() { |
| 85 | + return $this->partes; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return string Retorna a URL no momento |
| 90 | + */ |
| 91 | + public function agora() { |
| 92 | + return $this->url_agora; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param string $palavra Recebe a palavra alvo para pesquisar. |
| 97 | + * @return bool Verifica se possui o o que for informado aqui na url. Se tiver, retorna true. |
| 98 | + */ |
| 99 | + public function contem($palavra) { |
| 100 | + if ((in_array($palavra, $this->partes)) || ( in_array($this->tratar($palavra), $this->partes)) || preg_match("/$palavra/", implode('/',$this->partes))) { |
| 101 | + return true; |
| 102 | + } else { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * |
| 109 | + * @param bool $get Define se a url irá retornar o endereço com ou sem valores de $_GET |
| 110 | + */ |
| 111 | + public function setGet($get = false) { |
| 112 | + $this->get = ($get); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * |
| 117 | + * @param string $nome Recebe o nome da regra. |
| 118 | + * @param int $posicao Recebe a posição da regra. |
| 119 | + */ |
| 120 | + public function addRegra($nome, $posicao) { |
| 121 | + $this->regras[$nome] = $posicao; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @param string $posicao = null Recebe a URL para procurar por uma ID. |
| 126 | + * @return int $id Retorna a Id que pode estar na URL |
| 127 | + */ |
| 128 | + public function getId($posicao = false) { |
| 129 | + $id = null; |
| 130 | + if ($posicao === false) { |
| 131 | + $last = (count($this->partes))-1; |
| 132 | + $aux = array_reverse(explode("-",$this->partes[$last])); |
| 133 | + return (int) $aux[0]; |
| 134 | + } else { |
| 135 | + if (is_int($posicao) and $this->get($posicao)!=="") { |
| 136 | + return array_reverse(explode('-',$this->get($posicao)))[0]; |
| 137 | + } |
| 138 | + if (is_string($posicao) and $this->get($posicao)!=="") { |
| 139 | + return array_reverse(explode('-',$this->get($posicao)))[0]; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @param string $palavra Recebe a palavra para pegar o que há depois dela. |
| 146 | + * @return string Retorna toda a url Depois da Palavra informada. |
| 147 | + */ |
| 148 | + public function getApos($palavra,$onlyKey = false) { |
| 149 | + if ($this->contem($palavra)) { |
| 150 | + $wordKey = null; |
| 151 | + foreach($this->partes as $key =>$value){ |
| 152 | + if($value==$palavra){ |
| 153 | + $wordKey = $key; |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + return ($onlyKey?(isset($this->partes[$wordKey+1])?$wordKey+1:""):(isset($this->partes[$wordKey+1])?$this->partes[$wordKey+1]:"")); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @param string $palavra Recebe a palavra para retornar o que há antes dela. |
| 163 | + * @return string Retorna toda a url Antes da Palavra informada. |
| 164 | + */ |
| 165 | + public function getURLApos($palavra) { |
| 166 | + if ($this->contem($palavra)) { |
| 167 | + $wordKey = $this->getApos($palavra,true); |
| 168 | + $contar = false; |
| 169 | + $url = array(); |
| 170 | + foreach($this->partes as $key => $value){ |
| 171 | + if($key==$wordKey){ |
| 172 | + $contar = true; |
| 173 | + } |
| 174 | + if($contar){ |
| 175 | + $url[] = $value; |
| 176 | + } |
| 177 | + } |
| 178 | + return implode("/",$url); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @param string $palavra Recebe a palavra para procurar o que há depois dela. |
| 184 | + * @return int $id Retorna a ID que está depois da palavra informada |
| 185 | + */ |
| 186 | + public function getIdApos($palavra) { |
| 187 | + return $this->getId($this->getApos($this->tratar($palavra),true)); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * @param string $palavra Recebe a palavra para retornar o que há antes dela. |
| 192 | + * @return string Retorna toda a url Antes da Palavra informada. |
| 193 | + */ |
| 194 | + public function getAntes($palavra,$onlyKey = false) { |
| 195 | + if ($this->contem($palavra)) { |
| 196 | + $wordKey = null; |
| 197 | + foreach($this->partes as $key =>$value){ |
| 198 | + if($value==$palavra){ |
| 199 | + $wordKey = $key; |
| 200 | + break; |
| 201 | + } |
| 202 | + } |
| 203 | + return ($onlyKey?(isset($this->partes[$wordKey-1])?$wordKey-1:""):(isset($this->partes[$wordKey-1])?$this->partes[$wordKey-1]:"")); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @param string $palavra Recebe a palavra para retornar o que há antes dela. |
| 209 | + * @return string Retorna toda a url Antes da Palavra informada. |
| 210 | + */ |
| 211 | + public function getURLAntes($palavra) { |
| 212 | + if ($this->contem($palavra)) { |
| 213 | + $wordKey = $this->getAntes($palavra,true); |
| 214 | + if($wordKey!==""){ |
| 215 | + $quebrar = null; |
| 216 | + $url = array(); |
| 217 | + $url[] = $this->site; |
| 218 | + foreach($this->partes as $key => $value){ |
| 219 | + if($quebrar!==null){ |
| 220 | + break; |
| 221 | + } |
| 222 | + if($key ==$wordKey){ |
| 223 | + $quebrar = true; |
| 224 | + } |
| 225 | + $url[] = $value; |
| 226 | + } |
| 227 | + return implode("/",$url); |
| 228 | + }else{ |
| 229 | + return false; |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + |
| 235 | + /** |
| 236 | + * @param string $palavra Recebe a palavra para retornar a ID que há antes dela. |
| 237 | + * @return int $id Retorna a ID que está antes da palavra informada. |
| 238 | + */ |
| 239 | + public function getIdAntes($palavra) { |
| 240 | + return $this->getId($this->getAntes($this->tratar($palavra),true)); |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * @param string $string Recebe um texto para converter em URL. |
| 245 | + * @return string $novaUrl Esta função gera uma url para um link. Só deve ser usada para links diretos (sem "/") |
| 246 | + */ |
| 247 | + public function tratar($string) { |
| 248 | + $string = preg_replace('/[áàãâä]/ui', 'a', $string); |
| 249 | + $string = preg_replace('/[éèêë]/ui', 'e', $string); |
| 250 | + $string = preg_replace('/[íìîï]/ui', 'i', $string); |
| 251 | + $string = preg_replace('/[óòõôö]/ui', 'o', $string); |
| 252 | + $string = preg_replace('/[úùûü]/ui', 'u', $string); |
| 253 | + $string = preg_replace('/[ç]/ui', 'c', $string); |
| 254 | + $string = preg_replace('/[^a-z0-9]/i', '_', $string); |
| 255 | + $string = preg_replace('/_+/', '-', $string); |
| 256 | + return $string; |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * |
| 261 | + * @param string $texto Recebe o texto a ser tornado link. |
| 262 | + * @param int $id Recebe uma Id, caso utilize $_GET com a página destino. |
| 263 | + * @return string Retorna o Link gerado. |
| 264 | + */ |
| 265 | + public function gerarLink($texto, $id = false) { |
| 266 | + $texto = $this->tratar($texto); |
| 267 | + if (strlen($texto) > 50) { |
| 268 | + $texto = substr($texto, 0, 50); |
| 269 | + } |
| 270 | + return $texto . ($id ? "-". $id: ""); |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * |
| 275 | + * @param string $texto Recebe o texto a ser tornado link. |
| 276 | + * @param int $id Recebe uma Id, caso utilize $_GET com a página destino. |
| 277 | + * @return string Retorna o Link gerado de forma que seja possível acessar externamente. |
| 278 | + */ |
| 279 | + public function gerarLinkInterno($texto, $id = false) { |
| 280 | + return $this->site . $this->url . $this->gerarLink($texto, $id); |
| 281 | + } |
| 282 | + |
| 283 | +} |
0 commit comments