string = $string; } /** * Establece el string a manipular */ public function setString(string $string): self { $this->string = $string; return $this; } /** * Obtiene el string actual */ public function getString(): string { return $this->string; } /** * Convierte el string a mayúsculas */ public function toUpper(): self { $this->string = mb_strtoupper($this->string); return $this; } /** * Convierte el string a minúsculas */ public function toLower(): self { $this->string = mb_strtolower($this->string); return $this; } /** * Capitaliza la primera letra */ public function capitalize(): self { $this->string = ucfirst($this->string); return $this; } /** * Capitaliza cada palabra */ public function capitalizeWords(): self { $this->string = ucwords($this->string); return $this; } /** * Elimina espacios en blanco al inicio y final */ public function trim(): self { $this->string = trim($this->string); return $this; } /** * Remueve caracteres especiales y acentos */ public function removeSpecialChars(): self { $unwanted_array = [ "Š" => "S", "š" => "s", "Ž" => "Z", "ž" => "z", "À" => "A", "Á" => "A", "Â" => "A", "Ã" => "A", "Ä" => "A", "Å" => "A", "Æ" => "A", "Ç" => "C", "È" => "E", "É" => "E", "Ê" => "E", "Ë" => "E", "Ì" => "I", "Í" => "I", "Î" => "I", "Ï" => "I", "Ñ" => "N", "Ò" => "O", "Ó" => "O", "Ô" => "O", "Õ" => "O", "Ö" => "O", "Ø" => "O", "Ù" => "U", "Ú" => "U", "Û" => "U", "Ü" => "U", "Ý" => "Y", "Þ" => "B", "ß" => "ss", "à" => "a", "á" => "a", "â" => "a", "ã" => "a", "ä" => "a", "å" => "a", "æ" => "a", "ç" => "c", "è" => "e", "é" => "e", "ê" => "e", "ë" => "e", "ì" => "i", "í" => "i", "î" => "i", "ï" => "i", "ð" => "o", "ñ" => "n", "ò" => "o", "ó" => "o", "ô" => "o", "õ" => "o", "ö" => "o", "ø" => "o", "ù" => "u", "ú" => "u", "û" => "u", "ý" => "y", "ý" => "y", "þ" => "b", "ÿ" => "y", ]; $this->string = strtr($this->string, $unwanted_array); return $this; } /** * Genera un slug a partir del string */ public function slug(): self { $this->removeSpecialChars(); $this->string = strtolower( trim(preg_replace("/[^A-Za-z0-9-]+/", "-", $this->string)) ); return $this; } /** * Trunca el string a una longitud específica */ public function truncate(int $length, string $append = "..."): self { if (strlen($this->string) > $length) { $this->string = substr($this->string, 0, $length) . $append; } return $this; } /** * Cuenta las palabras en el string */ public function wordCount(): int { return str_word_count($this->string); } /** * Revierte el string */ public function reverse(): self { $this->string = strrev($this->string); return $this; } /** * Reemplaza todas las ocurrencias de un string por otro */ public function replace(string $search, string $replace): self { $this->string = str_replace($search, $replace, $this->string); return $this; } /** * Extrae una substring */ public function substring(int $start, ?int $length = null): self { $this->string = substr($this->string, $start, $length); return $this; } /** * Convierte el string a camelCase */ public function toCamelCase(): self { $this->string = lcfirst( str_replace( " ", "", ucwords(str_replace(["_", "-"], " ", $this->string)) ) ); return $this; } /** * Convierte el string a snake_case */ public function toSnakeCase(): self { $this->string = strtolower( preg_replace( ["/([a-z\d])([A-Z])/", "/([^_])([A-Z][a-z])/"], '$1_$2', $this->string ) ); return $this; } /** * Verifica si el string contiene una substring */ public function contains(string $needle): bool { return strpos($this->string, $needle) !== false; } /** * Verifica si el string empieza con una substring */ public function startsWith(string $needle): bool { return strpos($this->string, $needle) === 0; } /** * Verifica si el string termina con una substring */ public function endsWith(string $needle): bool { return substr($this->string, -strlen($needle)) === $needle; } /** * Limpia el string de HTML y PHP tags */ public function stripTags(): self { $this->string = strip_tags($this->string); return $this; } /** * Convierte caracteres especiales a entidades HTML */ public function htmlEntities(): self { $this->string = htmlentities($this->string, ENT_QUOTES, "UTF-8"); return $this; } /** * Genera un hash MD5 del string */ public function toMd5(): self { $this->string = md5($this->string); return $this; } /** * Genera un string aleatorio */ public function random(int $length = 10): self { $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $this->string = ""; for ($i = 0; $i < $length; $i++) { $this->string .= $characters[rand(0, strlen($characters) - 1)]; } return $this; } }